mirror of
https://github.com/outline/outline.git
synced 2026-11-23 06:11:04 +03:00
only users with "manage" permissions can edit page permissions (#12597)
* only users with "manage" permissions can edit page permissions * Fix document permission test authentication * allow manage perms on archived docs
This commit is contained in:
3 files changed
+286
-6
No files matched your search
@@ -33,7 +33,7 @@ describe("read_write collection", () => {
|
||||
expect(abilities.download).toBeTruthy();
|
||||
expect(abilities.update).toBeTruthy();
|
||||
expect(abilities.createChildDocument).toBeTruthy();
|
||||
expect(abilities.manageUsers).toBeTruthy();
|
||||
expect(abilities.manageUsers).toEqual(false);
|
||||
expect(abilities.archive).toBeTruthy();
|
||||
expect(abilities.delete).toBeTruthy();
|
||||
expect(abilities.share).toBeTruthy();
|
||||
@@ -41,6 +41,53 @@ describe("read_write collection", () => {
|
||||
expect(abilities.comment).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should allow user management for collection admins", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
permission: null,
|
||||
});
|
||||
const doc = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
await collection.$add("user", user, {
|
||||
through: {
|
||||
permission: CollectionPermission.Admin,
|
||||
createdById: admin.id,
|
||||
},
|
||||
});
|
||||
|
||||
// reload to get membership
|
||||
const document = await Document.findByPk(doc.id, { userId: user.id });
|
||||
const abilities = serialize(user, document);
|
||||
expect(abilities.read).toBeTruthy();
|
||||
expect(abilities.update).toBeTruthy();
|
||||
expect(abilities.manageUsers).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should allow user management for workspace admins with read access", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildAdmin({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
permission: CollectionPermission.Read,
|
||||
});
|
||||
const doc = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
|
||||
// reload to get membership
|
||||
const document = await Document.findByPk(doc.id, { userId: user.id });
|
||||
const abilities = serialize(user, document);
|
||||
expect(abilities.read).toBeTruthy();
|
||||
expect(abilities.update).toEqual(false);
|
||||
expect(abilities.manageUsers).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should allow read permissions for viewer", async () => {
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({
|
||||
@@ -308,7 +355,7 @@ describe("archived document", () => {
|
||||
expect(abilities.unarchive).toBeTruthy();
|
||||
expect(abilities.update).toEqual(false);
|
||||
expect(abilities.createChildDocument).toEqual(false);
|
||||
expect(abilities.manageUsers).toEqual(false);
|
||||
expect(abilities.manageUsers).toBeTruthy();
|
||||
expect(abilities.archive).toEqual(false);
|
||||
expect(abilities.share).toEqual(false);
|
||||
expect(abilities.move).toEqual(false);
|
||||
|
||||
@@ -107,11 +107,12 @@ allow(User, "publish", Document, (actor, document) =>
|
||||
|
||||
allow(User, "manageUsers", Document, (actor, document) =>
|
||||
and(
|
||||
can(actor, "update", document),
|
||||
isTeamMutable(actor),
|
||||
can(actor, "read", document),
|
||||
or(
|
||||
includesMembership(document, [DocumentPermission.Admin]),
|
||||
and(isTeamAdmin(actor, document), can(actor, "read", document)),
|
||||
can(actor, "updateDocument", document?.collection),
|
||||
isTeamAdmin(actor, document),
|
||||
can(actor, "update", document?.collection),
|
||||
!!document?.isDraft && actor.id === document?.createdById
|
||||
)
|
||||
)
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
import { TextHelper } from "@shared/utils/TextHelper";
|
||||
import { createContext } from "@server/context";
|
||||
import { parser } from "@server/editor";
|
||||
import type { User } from "@server/models";
|
||||
import type { Group, User } from "@server/models";
|
||||
import {
|
||||
Document,
|
||||
View,
|
||||
@@ -47,6 +47,106 @@ beforeEach(async () => {
|
||||
await buildDocument();
|
||||
});
|
||||
|
||||
type PermissionManagementSubject = {
|
||||
actor: User;
|
||||
document: Document;
|
||||
member: User;
|
||||
group: Group;
|
||||
};
|
||||
|
||||
async function expectCanManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
}: PermissionManagementSubject) {
|
||||
const addUser = await server.post("/api/documents.add_user", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
userId: member.id,
|
||||
},
|
||||
});
|
||||
expect(addUser.status).toEqual(200);
|
||||
|
||||
const removeUser = await server.post("/api/documents.remove_user", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
userId: member.id,
|
||||
},
|
||||
});
|
||||
expect(removeUser.status).toEqual(200);
|
||||
|
||||
const addGroup = await server.post("/api/documents.add_group", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
groupId: group.id,
|
||||
},
|
||||
});
|
||||
expect(addGroup.status).toEqual(200);
|
||||
|
||||
const removeGroup = await server.post("/api/documents.remove_group", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
groupId: group.id,
|
||||
},
|
||||
});
|
||||
expect(removeGroup.status).toEqual(200);
|
||||
}
|
||||
|
||||
async function expectCannotManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
}: PermissionManagementSubject) {
|
||||
await UserMembership.create({
|
||||
userId: member.id,
|
||||
documentId: document.id,
|
||||
permission: DocumentPermission.Read,
|
||||
createdById: actor.id,
|
||||
});
|
||||
await GroupMembership.create({
|
||||
groupId: group.id,
|
||||
documentId: document.id,
|
||||
permission: DocumentPermission.Read,
|
||||
createdById: actor.id,
|
||||
});
|
||||
|
||||
const addUser = await server.post("/api/documents.add_user", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
userId: member.id,
|
||||
permission: DocumentPermission.ReadWrite,
|
||||
},
|
||||
});
|
||||
expect(addUser.status).toEqual(403);
|
||||
|
||||
const removeUser = await server.post("/api/documents.remove_user", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
userId: member.id,
|
||||
},
|
||||
});
|
||||
expect(removeUser.status).toEqual(403);
|
||||
|
||||
const addGroup = await server.post("/api/documents.add_group", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
groupId: group.id,
|
||||
permission: DocumentPermission.ReadWrite,
|
||||
},
|
||||
});
|
||||
expect(addGroup.status).toEqual(403);
|
||||
|
||||
const removeGroup = await server.post("/api/documents.remove_group", actor, {
|
||||
body: {
|
||||
id: document.id,
|
||||
groupId: group.id,
|
||||
},
|
||||
});
|
||||
expect(removeGroup.status).toEqual(403);
|
||||
}
|
||||
|
||||
describe("#documents.info", () => {
|
||||
it("should fail if both id and shareId are absent", async () => {
|
||||
const res = await server.post("/api/documents.info", {
|
||||
@@ -4916,6 +5016,138 @@ describe("#documents.users", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("document permission management authorization", () => {
|
||||
it("should prevent collection read_write members from managing document users and groups", async () => {
|
||||
const team = await buildTeam();
|
||||
const actor = await buildUser({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
permission: CollectionPermission.ReadWrite,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const member = await buildUser({ teamId: team.id });
|
||||
const group = await buildGroup({ teamId: team.id });
|
||||
|
||||
await expectCannotManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow collection admins to manage document users and groups", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const actor = await buildUser({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
userId: admin.id,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const member = await buildUser({ teamId: team.id });
|
||||
const group = await buildGroup({ teamId: team.id });
|
||||
await collection.$add("user", actor, {
|
||||
through: {
|
||||
permission: CollectionPermission.Admin,
|
||||
createdById: admin.id,
|
||||
},
|
||||
});
|
||||
|
||||
await expectCanManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow workspace admins with read access to manage document users and groups", async () => {
|
||||
const team = await buildTeam();
|
||||
const actor = await buildAdmin({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
permission: CollectionPermission.Read,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const member = await buildUser({ teamId: team.id });
|
||||
const group = await buildGroup({ teamId: team.id });
|
||||
|
||||
await expectCanManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
});
|
||||
});
|
||||
|
||||
it("should allow direct document admins to manage users and groups", async () => {
|
||||
const team = await buildTeam();
|
||||
const actor = await buildUser({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const member = await buildUser({ teamId: team.id });
|
||||
const group = await buildGroup({ teamId: team.id });
|
||||
await UserMembership.create({
|
||||
userId: actor.id,
|
||||
documentId: document.id,
|
||||
permission: DocumentPermission.Admin,
|
||||
createdById: actor.id,
|
||||
});
|
||||
|
||||
await expectCanManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
});
|
||||
});
|
||||
|
||||
it("should prevent direct document read_write members from managing users and groups", async () => {
|
||||
const team = await buildTeam();
|
||||
const actor = await buildUser({ teamId: team.id });
|
||||
const collection = await buildCollection({
|
||||
teamId: team.id,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: team.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const member = await buildUser({ teamId: team.id });
|
||||
const group = await buildGroup({ teamId: team.id });
|
||||
await UserMembership.create({
|
||||
userId: actor.id,
|
||||
documentId: document.id,
|
||||
permission: DocumentPermission.ReadWrite,
|
||||
createdById: actor.id,
|
||||
});
|
||||
|
||||
await expectCannotManageDocumentPermissions({
|
||||
actor,
|
||||
document,
|
||||
member,
|
||||
group,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#documents.add_user", () => {
|
||||
it("should require id", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
Reference in new issue
Block a user