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 }); } }