From 71bde15a2fd3024ca5e337a5cbffc8b40e68fb82 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 29 Mar 2026 10:44:32 -0400 Subject: [PATCH] Remove restrict policy, use manageUsers for isPrivate toggle The restrict policy duplicated the manageUsers authorization logic. Replace all references with manageUsers and auto-cascade isPrivate changes via an @AfterUpdate hook on Document. Co-Authored-By: Claude Opus 4.6 --- .../Sharing/Document/AccessControlList.tsx | 4 ++-- server/commands/documentMover.ts | 4 ++-- server/models/Document.ts | 19 +++++++++++++++++++ server/policies/document.test.ts | 12 ++++++------ server/policies/document.ts | 18 ------------------ server/routes/api/documents/documents.ts | 14 ++++---------- 6 files changed, 33 insertions(+), 38 deletions(-) diff --git a/app/components/Sharing/Document/AccessControlList.tsx b/app/components/Sharing/Document/AccessControlList.tsx index c2cf549d2c..083d2d5ed2 100644 --- a/app/components/Sharing/Document/AccessControlList.tsx +++ b/app/components/Sharing/Document/AccessControlList.tsx @@ -146,7 +146,7 @@ export const AccessControlList = observer( maxHeight: maxHeight ? maxHeight - publicAccessHeight : undefined, }} > - {!document.isDraft && can.restrict && ( + {!document.isDraft && can.manageUsers && ( <> General access } diff --git a/server/commands/documentMover.ts b/server/commands/documentMover.ts index 08dece56c5..8e6a9809e4 100644 --- a/server/commands/documentMover.ts +++ b/server/commands/documentMover.ts @@ -94,7 +94,8 @@ async function documentMover( document.lastModifiedById = user.id; document.updatedBy = user; - // Auto-restrict when moving into a restricted parent + // Auto-restrict when moving into a restricted parent — descendant + // cascade is handled by the @AfterUpdate hook on Document.save() if (parentDocumentId && !document.isPrivate) { const parentDocument = await Document.unscoped().findOne({ attributes: ["id", "isPrivate"], @@ -104,7 +105,6 @@ async function documentMover( if (parentDocument?.isPrivate) { document.isPrivate = true; - await document.cascadeRestrict({ transaction }); // Update the document structure nodes to reflect the restriction if (documentJson) { diff --git a/server/models/Document.ts b/server/models/Document.ts index ac26529ed5..f94772e86f 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -600,6 +600,25 @@ class Document extends ArchivableModel< } } + @AfterUpdate + static async cascadeIsPrivateChange(model: Document, ctx: HookContext) { + if (!model.changed("isPrivate")) { + return; + } + + // Skip cascade during publish — publish handles its own membership setup + if (model.changed("publishedAt")) { + return; + } + + const transaction = ctx.transaction; + if (model.isPrivate) { + await model.cascadeRestrict({ transaction }); + } else { + await model.cascadeUnrestrict({ transaction }); + } + } + @AfterUpdate static async publishTitleChangeEvent( model: Document, diff --git a/server/policies/document.test.ts b/server/policies/document.test.ts index cab9e0fa85..c7af6df971 100644 --- a/server/policies/document.test.ts +++ b/server/policies/document.test.ts @@ -536,7 +536,7 @@ describe("restricted document", () => { expect(abilities.read).toBeTruthy(); }); - it("should allow restrict permission for document admin members", async () => { + it("should allow manageUsers permission for document admin members", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); const collection = await buildCollection({ @@ -555,10 +555,10 @@ describe("restricted document", () => { }); const document = await Document.findByPk(doc.id, { userId: user.id }); const abilities = serialize(user, document); - expect(abilities.restrict).toBeTruthy(); + expect(abilities.manageUsers).toBeTruthy(); }); - it("should allow restrict permission for team admins", async () => { + it("should allow manageUsers permission for team admins", async () => { const team = await buildTeam(); const admin = await buildAdmin({ teamId: team.id }); const collection = await buildCollection({ @@ -571,10 +571,10 @@ describe("restricted document", () => { }); const document = await Document.findByPk(doc.id, { userId: admin.id }); const abilities = serialize(admin, document); - expect(abilities.restrict).toBeTruthy(); + expect(abilities.manageUsers).toBeTruthy(); }); - it("should not allow restrict permission for regular members via collection", async () => { + it("should not allow manageUsers permission for regular members via collection", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); const collection = await buildCollection({ @@ -587,6 +587,6 @@ describe("restricted document", () => { }); const document = await Document.findByPk(doc.id, { userId: user.id }); const abilities = serialize(user, document); - expect(abilities.restrict).toEqual(false); + expect(abilities.manageUsers).toEqual(false); }); }); diff --git a/server/policies/document.ts b/server/policies/document.ts index 9d662f7b5d..974c6942cc 100644 --- a/server/policies/document.ts +++ b/server/policies/document.ts @@ -291,24 +291,6 @@ allow(User, "unpublish", Document, (user, document) => { return user.teamId === document.teamId; }); -allow(User, "restrict", Document, (actor, document) => - and( - !!document?.isActive, - !document?.isDraft, - isTeamMutable(actor), - isTeamModel(actor, document), - or( - includesMembership(document, [DocumentPermission.Admin]), - isTeamAdmin(actor, document), - and( - !document?.isPrivate, - can(actor, "updateDocument", document?.collection) - ), - and(!!document?.isDraft, actor.id === document?.createdById) - ) - ) -); - function includesMembership( document: Document | null, permissions: DocumentPermission[] diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index fcb7492840..7d4fe8ceb4 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -1309,14 +1309,13 @@ router.post( authorize(user, "updateInsights", document); } - // Handle restrict/unrestrict toggle + // Handle restrict/unrestrict toggle — validation and descendant cascade + // are enforced by Document model hooks (@BeforeUpdate and @AfterUpdate) if (isPrivate !== undefined && isPrivate !== document.isPrivate) { - authorize(user, "restrict", document); + authorize(user, "manageUsers", document); + document.isPrivate = isPrivate; if (isPrivate) { - document.isPrivate = true; - await document.cascadeRestrict({ transaction }); - // Ensure the acting user has direct admin access const existingMembership = await UserMembership.findOne({ where: { @@ -1337,11 +1336,6 @@ router.post( { transaction } ); } - } else { - // Validation (cannot unrestrict child of restricted parent) is - // enforced by the @BeforeUpdate hook on Document - document.isPrivate = false; - await document.cascadeUnrestrict({ transaction }); } }