Source inheritence logic

This commit is contained in:
Tom Moor committed 2026-03-29 23:09:35 -04:00
1 parent e093207d6d
commit e26698f967
7 files changed
+238 -37

No files matched your search

@@ -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(
<>
<ListItem
image={
document.isPrivate || collection?.isPrivate ? (
collection?.isPrivate ? (
<Squircle color={collection.color} size={AvatarSize.Medium}>
<CollectionIcon
collection={collection}
color="white"
size={16}
/>
</Squircle>
) : document.isPrivate ? (
<Squircle
color={theme.textTertiary}
size={AvatarSize.Medium}
@@ -174,15 +183,15 @@ export const AccessControlList = observer(
/>
}
subtitle={
document.isPrivate ? (
t("Only invited users can access")
) : parentIsPrivate ? (
parentIsPrivate ? (
<Trans>
Required by{" "}
Access managed by{" "}
<StyledLink to={parentDocument?.path ?? ""}>
parent
</StyledLink>
</Trans>
) : document.isPrivate ? (
t("Only invited users can access")
) : collection?.isPrivate ? (
t("Members of {{ itemName }} can access", {
itemName: collection?.name,
@@ -114,7 +114,7 @@ const DocumentMemberListItem = ({
}
value={membership?.permission}
onChange={handleChange}
disabled={!onUpdate && !onLeave}
disabled={(!onUpdate && !onLeave) || !!membership.source}
/>
</div>
}
+163 -4
View File
@@ -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 });
+33 -2
View File
@@ -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,
});
}
};
/**
+13 -12
View File
@@ -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<string, unknown> = {
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) {
+13 -12
View File
@@ -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<string, unknown> = {
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) {
+1 -1
View File
@@ -463,7 +463,7 @@
"Suggestions for invitation": "Suggestions for invitation",
"No matches": "No matches",
"Collection members": "Collection members",
"Required by <2>parent</2>": "Required by <2>parent</2>",
"Access managed by <2>parent</2>": "Access managed by <2>parent</2>",
"Members of {{ itemName }} can access": "Members of {{ itemName }} can access",
"Inherited from collection": "Inherited from collection",
"Created the document": "Created the document",