Files
outline/server/routes/api/userMemberships/userMemberships.ts
T
Tom Moor 754672e9dc Centralize document access rules for list queries (#14783)
* wip

* Route document list queries through findAllWithCtx

Add findAllWithCtx and countWithCtx on Document and Template, unsafe* methods
for system reads, and a lint rule that bans direct list queries. Remove the
cached membership list, accessibleSql and findByIds in favour of one gate.

* Remove test

* Include drafts when loading starred documents

* Scope the access gate to the user's team, move whereToSql to QueryHelper

* Move collection access to Collection.findAllWithCtx

Collection.accessibleIds keeps the cached id list inside the collection gate, and user.collectionIds() is a shortcut to it. List queries use findAllWithCtx and countWithCtx, system reads use the shared unsafe methods, and the admin listing in collections.list is explicit.

* Check permanentDelete for each document in empty_trash
2026-11-27 15:30:43 -05:00

129 lines
3.4 KiB
TypeScript

import Router from "@koa/router";
import { Op, Sequelize } from "sequelize";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import { Document, Event, UserMembership } from "@server/models";
import { authorize } from "@server/policies";
import {
presentDocuments,
presentMembership,
presentPolicies,
} from "@server/presenters";
import type { Context } from "@server/types";
import pagination from "../middlewares/pagination";
import * as T from "./schema";
const router = new Router();
router.post(
"userMemberships.list",
auth(),
pagination(),
validate(T.UserMembershipsListSchema),
async (ctx: Context<T.UserMembershipsListReq>) => {
const { user } = ctx.auth;
const memberships = await UserMembership.scope("withUser").findAll({
where: {
userId: user.id,
documentId: {
[Op.ne]: null,
},
sourceId: {
[Op.eq]: null,
},
// Documents that inherit access from a shared ancestor are shown
// nested under that ancestor rather than as separate roots.
[Op.and]: Sequelize.literal(`NOT EXISTS (
SELECT 1 FROM user_permissions AS sourced
WHERE sourced."documentId" = "user_permission"."documentId"
AND sourced."userId" = "user_permission"."userId"
AND sourced."sourceId" IS NOT NULL
)`),
},
include: [
{
model: Document.unscoped(),
as: "document",
required: true,
attributes: [],
where: {
archivedAt: {
[Op.eq]: null,
},
},
},
],
order: [
Sequelize.literal('"user_permission"."index" collate "C"'),
["updatedAt", "DESC"],
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const documentIds = memberships
.map((p) => p.documentId)
.filter(Boolean) as string[];
const documents = await Document.findAllWithCtx(ctx, {
includeDrafts: true,
where: { id: documentIds },
});
const policies = presentPolicies(user, [...documents, ...memberships]);
ctx.body = {
pagination: ctx.state.pagination,
data: {
memberships: memberships.map(presentMembership),
documents: await presentDocuments(ctx, documents),
},
policies,
};
}
);
router.post(
"userMemberships.update",
auth(),
validate(T.UserMembershipsUpdateSchema),
transaction(),
async (ctx: Context<T.UserMembershipsUpdateReq>) => {
const { id, index } = ctx.input.body;
const { transaction } = ctx;
const { user } = ctx.auth;
const membership = await UserMembership.scope("withUser").findByPk(id, {
transaction,
lock: {
level: transaction.LOCK.NO_KEY_UPDATE,
of: UserMembership,
},
rejectOnEmpty: true,
});
authorize(user, "update", membership);
membership.index = index;
await membership.save({ transaction });
await Event.createFromContext(ctx, {
name: "userMemberships.update",
modelId: membership.id,
userId: membership.userId,
documentId: membership.documentId,
data: {
index: membership.index,
},
});
ctx.body = {
data: presentMembership(membership),
policies: presentPolicies(user, [membership]),
};
}
);
export default router;