allow only admin and owner to access detached archived docs

This commit is contained in:
hmacr committed 2024-12-05 22:01:06 +05:30
1 parent a215b5cd9e
commit c447f4dbc4
4 files changed
+88 -15

No files matched your search

+10 -2
View File
@@ -426,6 +426,7 @@ class WebsocketProvider extends React.Component<Props> {
const collectionId = event.modelId;
const collection = collections.get(collectionId);
const deletedAt = new Date().toISOString();
const user = currentUserId ? users.get(currentUserId) : undefined;
// All non-deleted documents including drafts and archived.
const nonDeletedDocuments = uniqBy(
@@ -436,9 +437,16 @@ class WebsocketProvider extends React.Component<Props> {
"id"
);
nonDeletedDocuments.forEach((doc) => {
// Detach drafts and archived documents when an unarchived collection is deleted; otherwise detach drafts only.
if (doc.isDraft || (doc.isArchived && !collection?.isArchived)) {
// Detach drafts when a collection is deleted.
if (doc.isDraft) {
doc.collectionId = null;
} else if (doc.isArchived && !collection?.isArchived) {
// Detach archived documents when an unarchived collection is deleted when the user is an admin (or) doc owner. Otherwise, remove it from the store.
if (!!user?.isAdmin || doc.createdBy?.id === currentUserId) {
doc.collectionId = null;
} else {
documents.remove(doc.id);
}
} else {
doc.deletedAt = deletedAt;
}
+5 -1
View File
@@ -50,7 +50,11 @@ allow(User, "createTemplate", Team, (actor, team) =>
)
);
allow(User, ["readTemplate", "readDocument"], Team, (actor, team) =>
allow(User, "readDocument", Team, (actor, team) =>
and(actor.isAdmin, isTeamModel(actor, team))
);
allow(User, "readTemplate", Team, (actor, team) =>
and(!actor.isViewer, isTeamModel(actor, team))
);
+54 -7
View File
@@ -2246,17 +2246,63 @@ describe("#documents.archived", () => {
expect(body.data.length).toEqual(1);
});
it("should return detached archived documents when containing collection is deleted", async () => {
const user = await buildUser();
const collection = await buildCollection({ teamId: user.teamId });
const document = await buildDocument({
it("should return all detached archived documents for admin when containing collection is deleted", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const adminCollection = await buildCollection({ teamId: admin.teamId });
const userCollection = await buildCollection({ teamId: user.teamId });
const adminDocument = await buildDocument({
userId: admin.id,
teamId: admin.teamId,
collectionId: adminCollection.id,
});
const userDocument = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: collection.id,
collectionId: userCollection.id,
});
await collection.destroy({ hooks: false });
await document.update({ archivedAt: new Date(), collectionId: null });
await Promise.all([
adminCollection.destroy({ hooks: false }),
userCollection.destroy({ hooks: false }),
adminDocument.update({ archivedAt: new Date(), collectionId: null }),
userDocument.update({ archivedAt: new Date(), collectionId: null }),
]);
const res = await server.post("/api/documents.archived", {
body: {
token: admin.getJwtToken(),
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data).toHaveLength(2);
});
it("should return only the detached archived documents belonging to a non-admin user when containing collection is deleted", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const adminCollection = await buildCollection({ teamId: admin.teamId });
const userCollection = await buildCollection({ teamId: user.teamId });
const adminDocument = await buildDocument({
userId: admin.id,
teamId: admin.teamId,
collectionId: adminCollection.id,
});
const userDocument = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: userCollection.id,
});
await Promise.all([
adminCollection.destroy({ hooks: false }),
userCollection.destroy({ hooks: false }),
adminDocument.update({ archivedAt: new Date(), collectionId: null }),
userDocument.update({ archivedAt: new Date(), collectionId: null }),
]);
const res = await server.post("/api/documents.archived", {
body: {
@@ -2266,6 +2312,7 @@ describe("#documents.archived", () => {
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data).toHaveLength(1);
expect(body.data[0].id).toEqual(userDocument.id);
});
it("should not return deleted documents", async () => {
+19 -5
View File
@@ -343,11 +343,25 @@ router.post(
const collectionIds = await user.collectionIds();
where = {
...where,
collectionId: can(user, "readDocument", user.team)
? {
[Op.or]: [{ [Op.in]: collectionIds }, { [Op.is]: null }],
}
: collectionIds,
[Op.or]: [
{
collectionId: {
[Op.in]: collectionIds,
},
},
can(user, "readDocument", user.team)
? {
collectionId: {
[Op.is]: null,
},
}
: {
createdById: user.id,
collectionId: {
[Op.is]: null,
},
},
],
};
}