Files
outline/server/models/Template.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

372 lines
8.9 KiB
TypeScript

import isUUID from "validator/lib/isUUID";
import type {
CountOptions,
Identifier,
InferAttributes,
InferCreationAttributes,
NonNullFindOptions,
FindOptions,
WhereOptions,
} from "sequelize";
import { EmptyResultError, Op } from "sequelize";
import {
Column,
DataType,
BelongsTo,
ForeignKey,
Table,
Length as SimpleLength,
DefaultScope,
Default,
HasMany,
Unique,
Scopes,
BeforeValidate,
IsDate,
} from "sequelize-typescript";
import slugify from "@server/utils/slugify";
import type { ProsemirrorData } from "@shared/types";
import { UrlHelper } from "@shared/utils/UrlHelper";
import { DocumentValidation } from "@shared/validations";
import { generateUrlId } from "@server/utils/url";
import type { Context } from "@server/types";
import Collection from "./Collection";
import Document from "./Document";
import Revision from "./Revision";
import Team from "./Team";
import User from "./User";
import ParanoidModel from "./base/ParanoidModel";
import IsHexColor from "./validators/IsHexColor";
import Length from "./validators/Length";
type AdditionalFindOptions = {
userId?: string;
includeState?: boolean;
rejectOnEmpty?: boolean | Error;
};
@DefaultScope(() => ({
include: [
{
association: "createdBy",
paranoid: false,
},
{
association: "updatedBy",
paranoid: false,
},
],
where: {
template: true,
},
attributes: {
exclude: ["state"],
},
}))
@Scopes(() => ({
withMembership: (userId: string, paranoid = true) => ({
include: [
{
model: userId
? Collection.scope([
"defaultScope",
{
method: ["withMembership", userId],
},
])
: Collection,
as: "collection",
paranoid,
},
],
}),
withCollection: {
include: [
{
model: Collection,
as: "collection",
},
],
},
}))
@Table({ tableName: "documents", modelName: "template" })
class Template extends ParanoidModel<
InferAttributes<Template>,
Partial<InferCreationAttributes<Template>>
> {
/** The namespace to use for events. */
static eventNamespace = "templates";
@SimpleLength({
min: 10,
max: 10,
msg: `urlId must be 10 characters`,
})
@Unique
@Column(DataType.STRING)
urlId: string;
@Length({
max: DocumentValidation.maxTitleLength,
msg: `Template title must be ${DocumentValidation.maxTitleLength} characters or less`,
})
@Column(DataType.STRING)
title: string;
@Default(false)
@Column(DataType.BOOLEAN)
fullWidth: boolean;
@Default(true)
@Column(DataType.BOOLEAN)
template: boolean;
/** The version of the editor last used to edit this template. */
@SimpleLength({
max: 255,
msg: `editorVersion must be 255 characters or less`,
})
@Column(DataType.STRING)
editorVersion: string | null;
/** An icon to use as the template icon. */
@Length({
max: 50,
msg: `icon must be 50 characters or less`,
})
@Column(DataType.STRING)
icon: string | null;
/** The color of the icon. */
@IsHexColor
@Column(DataType.STRING)
color: string | null;
/** The likely language of the template, in ISO 639-1 format. */
@Column(DataType.STRING)
language: string | null;
/**
* The content of the template as JSON, this is a snapshot at the last time the state was saved.
*/
@Column(DataType.JSONB)
content: ProsemirrorData | null;
// associations
@BelongsTo(() => User, "lastModifiedById")
updatedBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
lastModifiedById: string;
@BelongsTo(() => User, "createdById")
createdBy: User;
@ForeignKey(() => User)
@Column(DataType.UUID)
createdById: string;
@BelongsTo(() => Team, "teamId")
team: Team;
@ForeignKey(() => Team)
@Column(DataType.UUID)
teamId: string;
@BelongsTo(() => Collection, "collectionId")
collection: Collection | null;
@ForeignKey(() => Collection)
@Column(DataType.UUID)
collectionId?: string | null;
@HasMany(() => Revision, "documentId")
revisions: Revision[];
/** Whether the template is published, and if so when. */
@IsDate
@Column(DataType.DATE)
publishedAt: Date | null;
// getters
/** The frontend path to this template. */
get path() {
if (!this.title) {
return `/settings/templates/untitled-${this.urlId}`;
}
const slugifiedTitle = slugify(this.title);
return `/settings/templates/${slugifiedTitle}-${this.urlId}`;
}
/**
* Returns whether this is a workspace template.
*
* @returns boolean
*/
get isWorkspaceTemplate() {
return !this.collectionId;
}
/**
* Returns whether this template is an unpublished draft, only visible to the
* user that created it.
*
* @returns boolean
*/
get isDraft() {
return !this.publishedAt;
}
@BeforeValidate
static createUrlId(model: Template) {
return (model.urlId = model.urlId || generateUrlId());
}
/**
* Finds the templates that match the options and that the user in the
* context can read. The memberships of the user are loaded, so that
* policies can be presented for the templates.
*
* @param ctx the context with the user and the transaction.
* @param options the find options.
* @returns a promise that resolves to the templates.
*/
static async findAllWithCtx(
ctx: Context,
options: FindOptions<Template> = {}
): Promise<Template[]> {
return this.scope([
"defaultScope",
{ method: ["withMembership", ctx.auth.user.id, options.paranoid] },
]).findAll({
transaction: ctx.transaction,
...options,
where: {
[Op.and]: [options.where ?? {}, await this.accessibleWhere(ctx)],
},
});
}
/**
* Counts the templates that match the options and that the user in the
* context can read.
*
* @param ctx the context with the user and the transaction.
* @param options the count options.
* @returns a promise that resolves to the number of templates.
*/
static async countWithCtx(
ctx: Context,
options: Omit<CountOptions<Template>, "group"> = {}
): Promise<number> {
return this.count({
transaction: ctx.transaction,
...options,
where: {
[Op.and]: [options.where ?? {}, await this.accessibleWhere(ctx)],
},
});
}
/**
* Overrides the standard findByPk behavior to allow also querying by urlId
*
* @param id uuid or urlId
* @param options FindOptions
* @returns A promise resolving to a template instance or null
*/
static async findByPk(
id: Identifier,
options?: NonNullFindOptions<Template> & AdditionalFindOptions
): Promise<Template>;
static async findByPk(
id: Identifier,
options?: FindOptions<Template> & AdditionalFindOptions
): Promise<Template | null>;
static async findByPk(
id: Identifier,
options: (NonNullFindOptions<Template> | FindOptions<Template>) &
AdditionalFindOptions = {}
): Promise<Template | null> {
if (typeof id !== "string") {
if (options.rejectOnEmpty) {
throw options.rejectOnEmpty instanceof Error
? options.rejectOnEmpty
: new EmptyResultError(
`Template doesn't exist with id: ${String(id)}`
);
}
return null;
}
const { includeState, userId, ...rest } = options;
// allow default preloading of collection membership if `userId` is passed in find options
// almost every endpoint needs the collection membership to determine policy permissions.
const scope = this.scope([
"defaultScope",
{
method: ["withMembership", userId, rest.paranoid],
},
]);
let template: Template | null = null;
const match = id.match(UrlHelper.SLUG_URL_REGEX);
if (isUUID(id)) {
template = await scope.findOne({
where: {
id,
},
...rest,
rejectOnEmpty: false,
});
} else if (match) {
template = await scope.findOne({
where: {
urlId: match[1],
},
...rest,
rejectOnEmpty: false,
});
}
if (!template && rest.rejectOnEmpty) {
throw rest.rejectOnEmpty instanceof Error
? rest.rejectOnEmpty
: new EmptyResultError(`Template doesn't exist with id: ${id}`);
}
return template;
}
/**
* Returns the condition that limits a template query to the templates that
* the user in the context can read.
*
* @param ctx the context with the user and the transaction.
* @returns a promise that resolves to the where condition.
*/
private static async accessibleWhere(
ctx: Context
): Promise<WhereOptions<Template>> {
return {
teamId: ctx.auth.user.teamId,
[Op.or]: [
// Workspace templates have no collection and every user can read them.
{ collectionId: { [Op.is]: null } },
await Document.accessibleWhere(ctx.auth.user, {
transaction: ctx.transaction,
}),
],
};
}
}
export default Template;