mirror of
https://github.com/outline/outline.git
synced 2026-11-21 05:24:03 +03:00
check collection permission
This commit is contained in:
1 parent
3a2dbbc902
commit
39cd99da7e
3 files changed
+161
-16
No files matched your search
+105
-9
@@ -134,9 +134,41 @@ describe("user model", () => {
|
||||
});
|
||||
|
||||
describe("hasHigherDocumentPermission", () => {
|
||||
it("should return true when user has higher access level", async () => {
|
||||
it("should return true when user has higher access level through collection", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
await UserMembership.create({
|
||||
createdById: user.id,
|
||||
collectionId: collection.id,
|
||||
userId: user.id,
|
||||
permission: CollectionPermission.ReadWrite,
|
||||
});
|
||||
|
||||
const hasHigherPermission = await user.hasHigherDocumentPermission({
|
||||
documentId: document.id,
|
||||
permission: DocumentPermission.Read,
|
||||
});
|
||||
|
||||
expect(hasHigherPermission).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true when user has higher access level through document", async () => {
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const group = await buildGroup();
|
||||
await Promise.all([
|
||||
await buildGroupUser({
|
||||
@@ -168,7 +200,14 @@ describe("user model", () => {
|
||||
|
||||
it("should return true when user has the same access level", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const group = await buildGroup();
|
||||
await Promise.all([
|
||||
await buildGroupUser({
|
||||
@@ -200,7 +239,14 @@ describe("user model", () => {
|
||||
|
||||
it("should return false when user has lower access level", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const group = await buildGroup();
|
||||
await Promise.all([
|
||||
await buildGroupUser({
|
||||
@@ -232,7 +278,14 @@ describe("user model", () => {
|
||||
|
||||
it("should return false when user does not have access", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const hasHigherPermission = await user.hasHigherDocumentPermission({
|
||||
documentId: document.id,
|
||||
@@ -244,9 +297,38 @@ describe("user model", () => {
|
||||
});
|
||||
|
||||
describe("getDocumentPermission", () => {
|
||||
it("should return the highest provided permission", async () => {
|
||||
it("should return the highest provided permission through collection", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
await UserMembership.create({
|
||||
createdById: user.id,
|
||||
collectionId: collection.id,
|
||||
userId: user.id,
|
||||
permission: CollectionPermission.ReadWrite,
|
||||
});
|
||||
|
||||
const permission = await user.getDocumentPermission(document.id);
|
||||
|
||||
expect(permission).toEqual(DocumentPermission.ReadWrite);
|
||||
});
|
||||
|
||||
it("should return the highest provided permission through document", async () => {
|
||||
const user = await buildUser();
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const group = await buildGroup();
|
||||
await Promise.all([
|
||||
await buildGroupUser({
|
||||
@@ -275,7 +357,14 @@ describe("user model", () => {
|
||||
|
||||
it("should return the highest provided permission with skipped membership", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
const group = await buildGroup();
|
||||
const [, , groupMembership] = await Promise.all([
|
||||
await buildGroupUser({
|
||||
@@ -307,7 +396,14 @@ describe("user model", () => {
|
||||
|
||||
it("should return undefined when user does not have access", async () => {
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
collectionId: collection.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const permission = await user.getDocumentPermission(document.id);
|
||||
|
||||
|
||||
+52
-6
@@ -2,6 +2,7 @@ import crypto from "crypto";
|
||||
import { addHours, addMinutes, subMinutes } from "date-fns";
|
||||
import JWT from "jsonwebtoken";
|
||||
import { Context } from "koa";
|
||||
import compact from "lodash/compact";
|
||||
import orderBy from "lodash/orderBy";
|
||||
import {
|
||||
Transaction,
|
||||
@@ -56,6 +57,7 @@ import { ValidationError } from "../errors";
|
||||
import Attachment from "./Attachment";
|
||||
import AuthenticationProvider from "./AuthenticationProvider";
|
||||
import Collection from "./Collection";
|
||||
import Document from "./Document";
|
||||
import Group from "./Group";
|
||||
import GroupMembership from "./GroupMembership";
|
||||
import Team from "./Team";
|
||||
@@ -68,6 +70,13 @@ import IsUrlOrRelativePath from "./validators/IsUrlOrRelativePath";
|
||||
import Length from "./validators/Length";
|
||||
import NotContainsUrl from "./validators/NotContainsUrl";
|
||||
|
||||
// Higher value takes precedence
|
||||
const CollectionPermissionPriority: Record<CollectionPermission, number> = {
|
||||
[CollectionPermission.Admin]: 2,
|
||||
[CollectionPermission.ReadWrite]: 1,
|
||||
[CollectionPermission.Read]: 0,
|
||||
};
|
||||
|
||||
// Higher value takes precedence
|
||||
const DocumentPermissionPriority: Record<DocumentPermission, number> = {
|
||||
[DocumentPermission.Admin]: 2,
|
||||
@@ -670,6 +679,41 @@ class User extends ParanoidModel<
|
||||
documentId: string,
|
||||
skipMembershipId?: string
|
||||
): Promise<DocumentPermission | undefined> {
|
||||
const document = await Document.scope({
|
||||
method: ["withCollectionPermissions", this.id],
|
||||
}).findOne({ where: { id: documentId } });
|
||||
|
||||
const permissions: DocumentPermission[] = [];
|
||||
|
||||
const collection = document?.collection;
|
||||
if (collection) {
|
||||
const collectionPermissions = orderBy(
|
||||
compact([
|
||||
collection.permission,
|
||||
...compact(
|
||||
collection.memberships?.map(
|
||||
(m) => m.permission as CollectionPermission
|
||||
)
|
||||
),
|
||||
...compact(
|
||||
collection.groupMemberships?.map(
|
||||
(m) => m.permission as CollectionPermission
|
||||
)
|
||||
),
|
||||
]),
|
||||
(permission) => CollectionPermissionPriority[permission],
|
||||
"desc"
|
||||
);
|
||||
|
||||
if (collectionPermissions[0]) {
|
||||
permissions.push(
|
||||
collectionPermissions[0] === CollectionPermission.Read
|
||||
? DocumentPermission.Read
|
||||
: DocumentPermission.ReadWrite
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const userMembershipWhere: WhereOptions<UserMembership> = {
|
||||
userId: this.id,
|
||||
documentId,
|
||||
@@ -698,16 +742,18 @@ class User extends ParanoidModel<
|
||||
}),
|
||||
]);
|
||||
|
||||
const permissions = orderBy(
|
||||
[
|
||||
...userMemberships.map((m) => m.permission as DocumentPermission),
|
||||
...groupMemberships.map((m) => m.permission as DocumentPermission),
|
||||
],
|
||||
permissions.push(
|
||||
...userMemberships.map((m) => m.permission as DocumentPermission),
|
||||
...groupMemberships.map((m) => m.permission as DocumentPermission)
|
||||
);
|
||||
|
||||
const orderedPermissions = orderBy(
|
||||
permissions,
|
||||
(permission) => DocumentPermissionPriority[permission],
|
||||
"desc"
|
||||
);
|
||||
|
||||
return permissions[0];
|
||||
return orderedPermissions[0];
|
||||
}
|
||||
|
||||
// hooks
|
||||
|
||||
@@ -293,11 +293,14 @@ export async function buildCollection(
|
||||
overrides.archivedById = overrides.userId;
|
||||
}
|
||||
|
||||
if (overrides.permission === undefined) {
|
||||
overrides.permission = CollectionPermission.ReadWrite;
|
||||
}
|
||||
|
||||
return Collection.create({
|
||||
name: faker.lorem.words(2),
|
||||
description: faker.lorem.words(4),
|
||||
createdById: overrides.userId,
|
||||
permission: CollectionPermission.ReadWrite,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user