* Split permissions for reading documents from updating collection * fix: Admins should have collection read permission, tests * tsc * Add admin option to permission selector * Combine publish and create permissions, update -> createDocuments where appropriate * Plural -> singular * wip * Quick version of collection structure loading, will revisit * Remove documentIds method * stash * fixing tests to account for admin creation * Add self-hosted migration * fix: Allow groups to have admin permission * Prefetch collection documents * fix: Document explorer (move/publish) not working with async documents * fix: Cannot re-parent document to collection by drag and drop * fix: Cannot drag to import into collection item without admin permission * Remove unused isEditor getter
151 lines
3.3 KiB
TypeScript
151 lines
3.3 KiB
TypeScript
import invariant from "invariant";
|
|
import { some } from "lodash";
|
|
import { CollectionPermission } from "@shared/types";
|
|
import { Collection, User, Team } from "@server/models";
|
|
import { AdminRequiredError } from "../errors";
|
|
import { allow } from "./cancan";
|
|
|
|
allow(User, "createCollection", Team, (user, team) => {
|
|
if (!team || user.isViewer || user.teamId !== team.id) {
|
|
return false;
|
|
}
|
|
if (user.isAdmin || team.memberCollectionCreate) {
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
allow(User, "importCollection", Team, (actor, team) => {
|
|
if (!team || actor.teamId !== team.id) {
|
|
return false;
|
|
}
|
|
if (actor.isAdmin) {
|
|
return true;
|
|
}
|
|
|
|
throw AdminRequiredError();
|
|
});
|
|
|
|
allow(User, "move", Collection, (user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
if (collection.deletedAt) {
|
|
return false;
|
|
}
|
|
if (user.isAdmin) {
|
|
return true;
|
|
}
|
|
|
|
throw AdminRequiredError();
|
|
});
|
|
|
|
allow(User, "read", Collection, (user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
|
|
if (user.isAdmin) {
|
|
return true;
|
|
}
|
|
|
|
if (collection.isPrivate) {
|
|
return includesMembership(collection, Object.values(CollectionPermission));
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
allow(User, ["star", "unstar"], Collection, (user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
|
|
if (collection.isPrivate) {
|
|
return includesMembership(collection, Object.values(CollectionPermission));
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
allow(User, "share", Collection, (user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
if (!collection.sharing) {
|
|
return false;
|
|
}
|
|
|
|
if (
|
|
collection.permission !== CollectionPermission.ReadWrite ||
|
|
user.isViewer
|
|
) {
|
|
return includesMembership(collection, [
|
|
CollectionPermission.ReadWrite,
|
|
CollectionPermission.Admin,
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
allow(User, "readDocument", Collection, (user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
|
|
if (collection.isPrivate) {
|
|
return includesMembership(collection, Object.values(CollectionPermission));
|
|
}
|
|
|
|
return true;
|
|
});
|
|
|
|
allow(
|
|
User,
|
|
["updateDocument", "createDocument", "deleteDocument"],
|
|
Collection,
|
|
(user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
|
|
if (
|
|
collection.permission !== CollectionPermission.ReadWrite ||
|
|
user.isViewer
|
|
) {
|
|
return includesMembership(collection, [
|
|
CollectionPermission.ReadWrite,
|
|
CollectionPermission.Admin,
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
);
|
|
|
|
allow(User, ["update", "delete"], Collection, (user, collection) => {
|
|
if (!collection || user.teamId !== collection.teamId) {
|
|
return false;
|
|
}
|
|
if (user.isAdmin) {
|
|
return true;
|
|
}
|
|
|
|
return includesMembership(collection, [CollectionPermission.Admin]);
|
|
});
|
|
|
|
function includesMembership(
|
|
collection: Collection,
|
|
memberships: CollectionPermission[]
|
|
) {
|
|
invariant(
|
|
collection.memberships,
|
|
"memberships should be preloaded, did you forget withMembership scope?"
|
|
);
|
|
return some(
|
|
[...collection.memberships, ...collection.collectionGroupMemberships],
|
|
(m) => memberships.includes(m.permission)
|
|
);
|
|
}
|