From e26698f967e4bccbf785933dc37afe6de706f573 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 29 Mar 2026 23:09:35 -0400 Subject: [PATCH] Source inheritence logic --- .../Sharing/Document/AccessControlList.tsx | 19 +- .../Document/DocumentMemberListItem.tsx | 2 +- server/models/Document.test.ts | 167 +++++++++++++++++- server/models/Document.ts | 35 +++- server/models/GroupMembership.ts | 25 +-- server/models/UserMembership.ts | 25 +-- shared/i18n/locales/en_US/translation.json | 2 +- 7 files changed, 238 insertions(+), 37 deletions(-) diff --git a/app/components/Sharing/Document/AccessControlList.tsx b/app/components/Sharing/Document/AccessControlList.tsx index 9725988ac1..21dddf964d 100644 --- a/app/components/Sharing/Document/AccessControlList.tsx +++ b/app/components/Sharing/Document/AccessControlList.tsx @@ -12,6 +12,7 @@ import type { Option } from "~/components/InputSelect"; import type Document from "~/models/Document"; import type Share from "~/models/Share"; import Flex from "~/components/Flex"; +import CollectionIcon from "~/components/Icons/CollectionIcon"; import NudeButton from "~/components/NudeButton"; import TeamLogo from "~/components/TeamLogo"; import Scrollable from "~/components/Scrollable"; @@ -150,7 +151,15 @@ export const AccessControlList = observer( <> + + + ) : document.isPrivate ? ( } subtitle={ - document.isPrivate ? ( - t("Only invited users can access") - ) : parentIsPrivate ? ( + parentIsPrivate ? ( - Required by{" "} + Access managed by{" "} parent + ) : document.isPrivate ? ( + t("Only invited users can access") ) : collection?.isPrivate ? ( t("Members of {{ itemName }} can access", { itemName: collection?.name, diff --git a/app/components/Sharing/Document/DocumentMemberListItem.tsx b/app/components/Sharing/Document/DocumentMemberListItem.tsx index 077352c170..fd0a7032e6 100644 --- a/app/components/Sharing/Document/DocumentMemberListItem.tsx +++ b/app/components/Sharing/Document/DocumentMemberListItem.tsx @@ -114,7 +114,7 @@ const DocumentMemberListItem = ({ } value={membership?.permission} onChange={handleChange} - disabled={!onUpdate && !onLeave} + disabled={(!onUpdate && !onLeave) || !!membership.source} /> } diff --git a/server/models/Document.test.ts b/server/models/Document.test.ts index a4eca99d41..91ceb5e22c 100644 --- a/server/models/Document.test.ts +++ b/server/models/Document.test.ts @@ -433,6 +433,95 @@ describe("isPrivate", () => { ).toBe(1); }); + test("should recreate sourced memberships from direct memberships after restrict", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const viewer = await buildUser({ teamId: team.id }); + const collection = await buildCollection({ + teamId: team.id, + userId: user.id, + }); + + // A -> B -> C + const docA = await buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection.id, + }); + const docB = await buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection.id, + parentDocumentId: docA.id, + }); + const docC = await buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection.id, + parentDocumentId: docB.id, + }); + + // Add direct membership on A for viewer — cascades to B and C + const directMembership = await UserMembership.create({ + userId: viewer.id, + documentId: docA.id, + permission: DocumentPermission.ReadWrite, + createdById: user.id, + }); + + // Verify sourced memberships exist on B and C + expect( + await UserMembership.count({ + where: { + documentId: docB.id, + userId: viewer.id, + sourceId: directMembership.id, + }, + }) + ).toBe(1); + expect( + await UserMembership.count({ + where: { + documentId: docC.id, + userId: viewer.id, + sourceId: directMembership.id, + }, + }) + ).toBe(1); + + // Make A private — B and C become private, sourced memberships are + // destroyed but should be immediately recreated from A's direct membership + docA.isPrivate = true; + await docA.save(); + + // Direct membership on A should remain + expect( + await UserMembership.count({ + where: { documentId: docA.id, userId: viewer.id, sourceId: null }, + }) + ).toBe(1); + + // Sourced memberships on B and C should be recreated + expect( + await UserMembership.count({ + where: { + documentId: docB.id, + userId: viewer.id, + sourceId: directMembership.id, + }, + }) + ).toBe(1); + expect( + await UserMembership.count({ + where: { + documentId: docC.id, + userId: viewer.id, + sourceId: directMembership.id, + }, + }) + ).toBe(1); + }); + test("should set children to private", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); @@ -732,7 +821,8 @@ describe("isPrivate", () => { docB.isPrivate = true; await docB.save(); - // Add direct membership to B for Bob — doesn't cascade to C since C is private + // Add direct membership to B for Bob — should cascade to C + // even though C is private (it's within B's private subtree) await UserMembership.create({ userId: bob.id, documentId: docB.id, @@ -742,15 +832,19 @@ describe("isPrivate", () => { expect( await UserMembership.count({ - where: { documentId: docC.id, userId: bob.id }, + where: { + documentId: docC.id, + userId: bob.id, + sourceId: { [Op.ne]: null }, + }, }) - ).toBe(0); + ).toBe(1); // Make B non-private docB.isPrivate = false; await docB.save(); - // Bob's membership should now cascade to C + // Bob's membership should still be on C expect( await UserMembership.count({ where: { @@ -766,6 +860,71 @@ describe("isPrivate", () => { expect(docC.isPrivate).toBe(false); }); + test("should cascade memberships to private children when added to private doc", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const viewer = await buildUser({ teamId: team.id }); + const collection = await buildCollection({ + teamId: team.id, + userId: user.id, + }); + + // A -> B -> C + const docA = await buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection.id, + }); + const docB = await buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection.id, + parentDocumentId: docA.id, + }); + const docC = await buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection.id, + parentDocumentId: docB.id, + }); + + // Make A private — B and C become private too + docA.isPrivate = true; + await docA.save(); + + await docB.reload(); + await docC.reload(); + expect(docB.isPrivate).toBe(true); + expect(docC.isPrivate).toBe(true); + + // Add membership on A — should cascade to private children B and C + const directMembership = await UserMembership.create({ + userId: viewer.id, + documentId: docA.id, + permission: DocumentPermission.ReadWrite, + createdById: user.id, + }); + + // Sourced memberships should exist on both B and C + const bMembership = await UserMembership.findOne({ + where: { + documentId: docB.id, + userId: viewer.id, + sourceId: directMembership.id, + }, + }); + expect(bMembership).not.toBeNull(); + + const cMembership = await UserMembership.findOne({ + where: { + documentId: docC.id, + userId: viewer.id, + sourceId: directMembership.id, + }, + }); + expect(cMembership).not.toBeNull(); + }); + test("should set children to non-private", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); diff --git a/server/models/Document.ts b/server/models/Document.ts index 7fb6f4fa86..f97cd6d1fc 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -1055,8 +1055,10 @@ class Document extends ArchivableModel< }; /** - * Cascade restriction to all descendant documents, destroying inherited - * (sourced) memberships on this document and its descendants. + * Cascade restriction to all descendant documents. Destroys sourced + * memberships inherited from outside the subtree, then rebuilds sourced + * memberships from direct memberships within the subtree so that users + * explicitly shared on this document (or its children) retain access. * * @param options - options including transaction. */ @@ -1075,6 +1077,8 @@ class Document extends ArchivableModel< } const allDocIds = [this.id, ...childDocumentIds]; + + // Destroy all sourced memberships in the subtree await UserMembership.destroy({ where: { documentId: { [Op.in]: allDocIds }, @@ -1089,6 +1093,33 @@ class Document extends ArchivableModel< }, transaction, }); + + // Rebuild sourced memberships from direct memberships within the subtree + const directUserMemberships = await UserMembership.findAll({ + where: { + documentId: { [Op.in]: allDocIds }, + sourceId: null, + }, + transaction, + }); + for (const membership of directUserMemberships) { + await UserMembership.recreateSourcedMemberships(membership, { + transaction, + }); + } + + const directGroupMemberships = await GroupMembership.findAll({ + where: { + documentId: { [Op.in]: allDocIds }, + sourceId: null, + }, + transaction, + }); + for (const membership of directGroupMemberships) { + await GroupMembership.recreateSourcedMemberships(membership, { + transaction, + }); + } }; /** diff --git a/server/models/GroupMembership.ts b/server/models/GroupMembership.ts index ee863733c6..e33b120278 100644 --- a/server/models/GroupMembership.ts +++ b/server/models/GroupMembership.ts @@ -336,7 +336,7 @@ class GroupMembership extends ParanoidModel< const document = await Document.unscoped() .scope("withoutState") .findOne({ - attributes: ["id"], + attributes: ["id", "isPrivate"], where: { id: documentId ?? model.documentId, }, @@ -346,19 +346,20 @@ class GroupMembership extends ParanoidModel< return; } + // When the document is private, cascade to all children (they are also + // private due to cascade). When non-private, stop at private boundaries. + const whereClause: Record = { + publishedAt: { [Op.ne]: null }, + }; + if (!document.isPrivate) { + whereClause.isPrivate = false; + } + const childDocumentIds = [ ...(documentId ? [documentId] : []), - ...(await document.findAllChildDocumentIds( - { - publishedAt: { - [Op.ne]: null, - }, - isPrivate: false, - }, - { - transaction, - } - )), + ...(await document.findAllChildDocumentIds(whereClause, { + transaction, + })), ]; if (childDocumentIds.length) { diff --git a/server/models/UserMembership.ts b/server/models/UserMembership.ts index f975ce59ea..e4e3447a5d 100644 --- a/server/models/UserMembership.ts +++ b/server/models/UserMembership.ts @@ -332,7 +332,7 @@ class UserMembership extends IdModel< const document = await Document.unscoped() .scope("withoutState") .findOne({ - attributes: ["id"], + attributes: ["id", "isPrivate"], where: { id: documentId ?? model.documentId, }, @@ -343,19 +343,20 @@ class UserMembership extends IdModel< return; } + // When the document is private, cascade to all children (they are also + // private due to cascade). When non-private, stop at private boundaries. + const whereClause: Record = { + publishedAt: { [Op.ne]: null }, + }; + if (!document.isPrivate) { + whereClause.isPrivate = false; + } + const childDocumentIds = [ ...(documentId ? [documentId] : []), - ...(await document.findAllChildDocumentIds( - { - publishedAt: { - [Op.ne]: null, - }, - isPrivate: false, - }, - { - transaction, - } - )), + ...(await document.findAllChildDocumentIds(whereClause, { + transaction, + })), ]; if (childDocumentIds.length) { diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 13dc0d63bf..900b7f7c10 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -463,7 +463,7 @@ "Suggestions for invitation": "Suggestions for invitation", "No matches": "No matches", "Collection members": "Collection members", - "Required by <2>parent": "Required by <2>parent", + "Access managed by <2>parent": "Access managed by <2>parent", "Members of {{ itemName }} can access": "Members of {{ itemName }} can access", "Inherited from collection": "Inherited from collection", "Created the document": "Created the document",