From 45f78d3398725a2c83b271eafd544003cf002d5d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 29 Mar 2026 10:19:12 -0400 Subject: [PATCH] Refactor isPrivate restrict/unrestrict logic into Document model Move cascading and validation logic out of the API route into the Document model for safety, ensuring invariants are enforced regardless of how isPrivate is changed. - Add @BeforeUpdate hook to prevent unrestricting a child of a restricted parent - Add cascadeRestrict() and cascadeUnrestrict() instance methods for descendant propagation and membership cleanup/rebuild - Slim down API route to only handle authorization and acting-user admin membership - Update documentMover to use cascadeRestrict(), fixing a gap where sourced memberships weren't destroyed on auto-restrict during move Co-Authored-By: Claude Opus 4.6 --- server/commands/documentMover.ts | 10 +-- server/models/Document.ts | 106 +++++++++++++++++++++++ server/routes/api/documents/documents.ts | 79 +---------------- 3 files changed, 111 insertions(+), 84 deletions(-) diff --git a/server/commands/documentMover.ts b/server/commands/documentMover.ts index d4b710fb1b..08dece56c5 100644 --- a/server/commands/documentMover.ts +++ b/server/commands/documentMover.ts @@ -104,15 +104,7 @@ async function documentMover( if (parentDocument?.isPrivate) { document.isPrivate = true; - const childDocIds = await document.findAllChildDocumentIds(undefined, { - transaction, - }); - if (childDocIds.length) { - await Document.update( - { isPrivate: true }, - { where: { id: childDocIds }, transaction } - ); - } + 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 a3c51b95dd..ac26529ed5 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -576,6 +576,30 @@ class Document extends ArchivableModel< } } + @BeforeUpdate + static async validateIsPrivateChange( + model: Document, + options: SaveOptions> + ) { + if (!model.changed("isPrivate") || model.isPrivate) { + return; + } + + // Cannot unrestrict a child of a restricted parent + if (model.parentDocumentId) { + const parentDocument = await this.unscoped().findOne({ + attributes: ["id", "isPrivate"], + where: { id: model.parentDocumentId }, + transaction: options.transaction, + }); + if (parentDocument?.isPrivate) { + throw ValidationError( + "Cannot remove restriction from a document whose parent is restricted" + ); + } + } + } + @AfterUpdate static async publishTitleChangeEvent( model: Document, @@ -1011,6 +1035,88 @@ class Document extends ArchivableModel< return findAllChildDocumentIds(this.id); }; + /** + * Cascade restriction to all descendant documents, destroying inherited + * (sourced) memberships on this document and its descendants. + * + * @param options - options including transaction. + */ + cascadeRestrict = async (options: { transaction?: Transaction }) => { + const { transaction } = options; + const childDocumentIds = await this.findAllChildDocumentIds(undefined, { + transaction, + }); + + if (childDocumentIds.length) { + // Note: bulk update intentionally does not fire instance hooks + await (this.constructor as typeof Document).update( + { isPrivate: true }, + { where: { id: childDocumentIds }, transaction } + ); + } + + const allDocIds = [this.id, ...childDocumentIds]; + await UserMembership.destroy({ + where: { + documentId: { [Op.in]: allDocIds }, + sourceId: { [Op.ne]: null }, + }, + transaction, + }); + await GroupMembership.destroy({ + where: { + documentId: { [Op.in]: allDocIds }, + sourceId: { [Op.ne]: null }, + }, + transaction, + }); + }; + + /** + * Cascade unrestriction to all descendant documents, re-inheriting + * memberships from the parent document. + * + * @param options - options including transaction. + */ + cascadeUnrestrict = async (options: { transaction?: Transaction }) => { + const { transaction } = options; + const childDocumentIds = await this.findAllChildDocumentIds(undefined, { + transaction, + }); + + if (childDocumentIds.length) { + // Note: bulk update intentionally does not fire instance hooks + await (this.constructor as typeof Document).update( + { isPrivate: false }, + { where: { id: childDocumentIds }, transaction } + ); + } + + if (this.parentDocumentId) { + const parentUserMemberships = await UserMembership.findAll({ + where: { documentId: this.parentDocumentId }, + transaction, + }); + for (const membership of parentUserMemberships) { + await UserMembership.recreateSourcedMemberships(membership, { + transaction, + documentId: this.id, + }); + } + + const parentGroupMemberships = await GroupMembership.findAll({ + where: { documentId: this.parentDocumentId }, + transaction, + }); + for (const membership of parentGroupMemberships) { + await GroupMembership.recreateSourcedMemberships(membership, { + transaction, + documentId: this.id, + }); + } + } + }; + publish = async ( ctx: APIContext, { diff --git a/server/routes/api/documents/documents.ts b/server/routes/api/documents/documents.ts index c53674761a..fcb7492840 100644 --- a/server/routes/api/documents/documents.ts +++ b/server/routes/api/documents/documents.ts @@ -1313,37 +1313,9 @@ router.post( if (isPrivate !== undefined && isPrivate !== document.isPrivate) { authorize(user, "restrict", document); - const childDocumentIds = await document.findAllChildDocumentIds( - undefined, - { transaction } - ); - if (isPrivate) { - // Restrict: set flag on document and all descendants document.isPrivate = true; - if (childDocumentIds.length) { - await Document.update( - { isPrivate: true }, - { where: { id: childDocumentIds }, transaction } - ); - } - - // Remove all sourced memberships on this document and descendants - const allDocIds = [document.id, ...childDocumentIds]; - await UserMembership.destroy({ - where: { - documentId: { [Op.in]: allDocIds }, - sourceId: { [Op.ne]: null }, - }, - transaction, - }); - await GroupMembership.destroy({ - where: { - documentId: { [Op.in]: allDocIds }, - sourceId: { [Op.ne]: null }, - }, - transaction, - }); + await document.cascadeRestrict({ transaction }); // Ensure the acting user has direct admin access const existingMembership = await UserMembership.findOne({ @@ -1366,53 +1338,10 @@ router.post( ); } } else { - // Cannot unrestrict a child of a restricted parent - if (document.parentDocumentId) { - const parentDocument = await Document.unscoped().findOne({ - attributes: ["id", "isPrivate"], - where: { id: document.parentDocumentId }, - transaction, - }); - if (parentDocument?.isPrivate) { - throw ValidationError( - "Cannot remove restriction from a document whose parent is restricted" - ); - } - } - - // Unrestrict: clear flag on document and all descendants + // Validation (cannot unrestrict child of restricted parent) is + // enforced by the @BeforeUpdate hook on Document document.isPrivate = false; - if (childDocumentIds.length) { - await Document.update( - { isPrivate: false }, - { where: { id: childDocumentIds }, transaction } - ); - } - - // Re-inherit permissions from parent by rebuilding sourced memberships - if (document.parentDocumentId) { - const parentMemberships = await UserMembership.findAll({ - where: { documentId: document.parentDocumentId }, - transaction, - }); - for (const membership of parentMemberships) { - await UserMembership.recreateSourcedMemberships(membership, { - transaction, - documentId: document.id, - }); - } - - const parentGroupMemberships = await GroupMembership.findAll({ - where: { documentId: document.parentDocumentId }, - transaction, - }); - for (const membership of parentGroupMemberships) { - await GroupMembership.recreateSourcedMemberships(membership, { - transaction, - documentId: document.id, - }); - } - } + await document.cascadeUnrestrict({ transaction }); } }