Files
outline/server/policies/utils.ts
T
Tom Moor f8496ea2f7 feat: Personal collections (#14062)
* feat: Personal collections backend

Adds an owner to collections. A personal collection is private, one per user
per team, created lazily through collections.personal, and only its owner can
see or manage it. Admin bypasses in policies, lists, exports, events, webhooks
and file operations exclude the personal collections of other users. Turning
off the PersonalDocs preference makes the collection read-only.

* test: Access matrix for personal collections

Checks that collection and document endpoints, list and search results, MCP
tools, collaboration and websocket rooms agree with the read policy for the
owner, collaborators, members, admins, guests and other teams.

* feat: Personal collection in the sidebar

Adds a Personal sidebar section that renders the personal collection with the
collection document tree, creates the collection on the first new document,
and keeps it out of the Collections section. Adds the workspace setting, the
personal sidebar context, and hides collection sharing and membership UI for
personal collections.

* fix: Drag and drop in shared with me trees

Anchors the reorder drop cursor inside the row, and prefetches documents on
hover so that nested rows that were never opened can be dragged.

* feat: Personal collection owner for collaborators, no collection page

Presents the owner of the personal collection on documents, so that
collaborators see the owner in the share popover. Root rows in the Personal
section show the default document icon. A personal collection has no page:
the scene shows not found, and the breadcrumb, command bar and delete redirect
no longer link to it.

* fix: Personal sidebar indent and breadcrumb items without a link

Children of rows at depth 0 skip a level so that they start beyond the row
icon. Breadcrumb items without a destination, such as the personal and deleted
collection, are shown as plain text.

* test: Reduce personal collection tests to list paths

Endpoints that authorize a single model are already covered by the policy
tests. Keeps the lists and searches, which filter in queries, and the
presenter.

* test: Move personal collection list tests into collections tests

* feat: Create documents in the personal collection by alias

documents.create, documents.import and the MCP create_document tool accept
the collectionId "personal", which refers to the personal collection of the
user and creates it when it does not exist yet.

* refactor: Remove collections.personal route

The sidebar creates its first personal document with the personal
collectionId alias, so documents.create is the only path that creates the
personal collection.

* feat: Personal collection name, starring, templates and default

Personal collections cannot be starred, renamed or hold templates, and the
PersonalDocs preference is on by default. The app shows collections through a
displayName getter, which is the translated "Personal" for a personal
collection.

* fix: Use the same policy for starring and unstarring collections

* fix: No pins or viewer insights for personal documents

* revert: Show personal collection events in the audit log

Admins with audit access should see events in personal collections, even when
they cannot open the documents.

* fix: Load collection owner in share scopes

* fix: Keep personal collections out of workspace exports and webhooks

Workspace exports skip all personal collections, while an explicit export of
a personal collection stays available to its owner. Events in personal
collections, including attachments and documents moved after queueing, are
never delivered to workspace webhooks. Personal collections cannot be
renamed or made public through any path, cannot hold pins, and are not
offered as mentions.

* Change icon to avatar

* fix: Personal documents cannot be unpublished

* feat: Personal collection context for MCP

Collections are presented with an isPersonal flag, the server instructions
explain the personal collection, and move_document and update_document accept
the personal collectionId alias.

* fix: Keep moved personal exports private and remove pins on move

Document exports record the owner of the personal collection they came from,
so they stay private after the document moves. Moving documents into a
personal collection removes their pins, including pins to home.

* fix: Only create the personal collection in MCP when it is used
2026-10-13 07:53:06 -04:00

204 lines
4.9 KiB
TypeScript

import env from "@server/env";
import type { Collection, User } from "@server/models";
import { Team, type Group } from "@server/models";
import type Model from "@server/models/base/Model";
import {
GroupPermission,
TeamPreference,
type PlanFeature,
} from "@shared/types";
import invariant from "invariant";
type Args = boolean | string | Args[];
export function and(...args: Args[]) {
for (const arg of args) {
if (!arg) {
return false;
}
}
return args;
}
export function or(...args: Args[]) {
return args.find(Boolean) || false;
}
/**
* Check if the actor is present in the same team as the model.
*
* @param actor The actor to check
* @param model The model to check
* @returns True if the actor is in the same team as the model
*/
export function isTeamModel(
actor: User,
model: Model | null | undefined
): model is Model {
if (!model) {
return false;
}
if (model instanceof Team) {
return actor.teamId === model.id;
}
if ("teamId" in model) {
return actor.teamId === model.teamId;
}
return false;
}
/**
* Check if the actor is the owner of the model.
*
* @param actor The actor to check
* @param model The model to check
* @returns True if the actor is the owner of the model
*/
export function isOwner(
actor: User,
model: Model | null | undefined
): model is Model {
if (!model) {
return false;
}
if ("userId" in model) {
return actor.id === model.userId;
}
if ("createdById" in model) {
return actor.id === model.createdById;
}
return false;
}
/**
* Check if the actor is an admin of the team.
*
* @param actor The actor to check
* @param mode The model to check
* @returns True if the actor is an admin of the team the model belongs to
*/
export function isTeamAdmin(
actor: User,
model: Model | null | undefined
): model is Model {
return !!and(isTeamModel(actor, model), actor.isAdmin);
}
/**
* Check if the actor owns the personal collection.
*
* @param actor The actor to check
* @param collection The collection to check
* @returns True if the collection is personal and owned by the actor
*/
export function isCollectionOwner(
actor: User,
collection: Collection | null | undefined
): boolean {
return (
!!collection?.ownerId &&
isTeamModel(actor, collection) &&
collection.ownerId === actor.id
);
}
/**
* Check if documents in the collection can be changed. A personal collection
* is read-only when the team turns personal documents off.
*
* @param actor The actor to check
* @param collection The collection to check
* @returns True if documents in the collection can be changed
*/
export function isCollectionWritable(
actor: User,
collection: Collection | null | undefined
): boolean {
return (
!collection?.ownerId ||
!!actor.team?.getPreference(TeamPreference.PersonalDocs)
);
}
/**
* Check if the actor is a member of the team.
*
* @param actor The actor to check
* @param model The model to check
* @returns True if the actor is a member of the team the model belongs to
*/
export function isTeamMember(actor: User, model: Model | null | undefined) {
return !!and(isTeamModel(actor, model), actor.isMember);
}
/**
* Check the actors team is mutable, meaning the team models can be modified.
*
* @param actor The actor to check
* @returns True if the actor's team is mutable
*/
export function isTeamMutable(_actor: User, _model?: Model | null) {
return true;
}
/**
* Check if the team is entitled to use a feature.
*
* IMPORTANT NOTICE: Bypassing or modifying license controls is a breach of
* the license agreement, and will automatically terminate your rights under
* the license for the current and all other versions.
*
* @param team the team to check.
* @param feature the feature to check for.
* @returns true if the team is entitled to the feature.
*/
export function teamHasEntitlement(
team: Team | null | undefined,
feature: PlanFeature
): boolean {
return !!team?.entitlements.includes(feature);
}
/**
* Check if this instance is running in the cloud-hosted environment.
*/
export function isCloudHosted() {
if (!env.isCloudHosted) {
return false;
}
return true;
}
/**
* Check if the actor is an admin of the group.
*
* @param actor The actor to check
* @param model The group model to check
* @returns True if the actor is an admin of the group
*/
export function isGroupAdmin(actor: User, model: Group | null): boolean {
if (!model || !("id" in model)) {
return false;
}
invariant(
model.groupUsers,
"Group users relationship not loaded, ensure to include groupUsers when fetching the group"
);
// Check if the user is a group admin
const membership = model.groupUsers.find(
(gu) => gu.userId === actor.id && gu.permission === GroupPermission.Admin
);
if (membership) {
return true;
}
// Team admins are always group admins
if (isTeamAdmin(actor, model)) {
return true;
}
return false;
}