mirror of
https://github.com/outline/outline.git
synced 2026-11-30 00:50:41 +03:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
8c0cb69522
commit
45f78d3398
3 files changed
+111
-84
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -576,6 +576,30 @@ class Document extends ArchivableModel<
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeUpdate
|
||||
static async validateIsPrivateChange(
|
||||
model: Document,
|
||||
options: SaveOptions<InferAttributes<Document>>
|
||||
) {
|
||||
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,
|
||||
{
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user