mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
45f78d3398
commit
71bde15a2f
6 files changed
+33
-38
No files matched your search
@@ -146,7 +146,7 @@ export const AccessControlList = observer(
|
||||
maxHeight: maxHeight ? maxHeight - publicAccessHeight : undefined,
|
||||
}}
|
||||
>
|
||||
{!document.isDraft && can.restrict && (
|
||||
{!document.isDraft && can.manageUsers && (
|
||||
<>
|
||||
<SectionHeading>General access</SectionHeading>
|
||||
<ListItem
|
||||
@@ -170,7 +170,7 @@ export const AccessControlList = observer(
|
||||
label={t("Access")}
|
||||
hideLabel
|
||||
short
|
||||
disabled={!can.restrict || parentIsPrivate}
|
||||
disabled={!can.manageUsers || parentIsPrivate}
|
||||
nude
|
||||
/>
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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[]
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user