chore: Consistent team checks in policies (#14235)

This commit is contained in:
Tom Moor authored and GitHub committed 2026-10-22 08:10:46 -04:00
1 parent d102e4223e
commit 18cd750d75
9 files changed
+124 -8

No files matched your search

+3 -3
View File
@@ -254,7 +254,7 @@ allow(
allow(User, "update", Collection, (user, collection) =>
and(
!!collection,
isTeamModel(user, collection),
!!collection?.isActive,
isOwnerIfPersonal(user, collection),
or(
@@ -311,7 +311,7 @@ allow(User, "export", Collection, (user, collection) =>
allow(User, "delete", Collection, (user, collection) =>
and(
!!collection,
isTeamModel(user, collection),
!collection?.deletedAt,
!collection?.isPersonal,
or(
@@ -323,7 +323,7 @@ allow(User, "delete", Collection, (user, collection) =>
allow(User, "restore", Collection, (user, collection) =>
and(
!!collection,
isTeamModel(user, collection),
!collection?.isActive,
isOwnerIfPersonal(user, collection),
or(
+22
View File
@@ -0,0 +1,22 @@
import { buildEmoji, buildTeam, buildUser } from "@server/test/factories";
import { serialize } from "./index";
describe("policies/emoji", () => {
it("should allow the owner to update and delete", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const emoji = await buildEmoji({ teamId: team.id, createdById: user.id });
const abilities = serialize(user, emoji);
expect(abilities.update).toBeTruthy();
expect(abilities.delete).toBeTruthy();
});
it("should not allow an owner from another team to update or delete", async () => {
const user = await buildUser();
const team = await buildTeam();
const emoji = await buildEmoji({ teamId: team.id, createdById: user.id });
const abilities = serialize(user, emoji);
expect(abilities.update).toEqual(false);
expect(abilities.delete).toEqual(false);
});
});
+5 -2
View File
@@ -1,11 +1,14 @@
import { User, Emoji, Team } from "@server/models";
import { allow } from "./cancan";
import { isOwner, isTeamAdmin, isTeamModel, or } from "./utils";
import { and, isOwner, isTeamAdmin, isTeamModel, or } from "./utils";
allow(User, "createEmoji", Team, isTeamModel);
allow(User, "read", Emoji, isTeamModel);
allow(User, ["update", "delete"], Emoji, (actor, emoji) =>
or(isOwner(actor, emoji), isTeamAdmin(actor, emoji))
and(
isTeamModel(actor, emoji),
or(isOwner(actor, emoji), isTeamAdmin(actor, emoji))
)
);
+51
View File
@@ -0,0 +1,51 @@
import { GroupPermission } from "@shared/types";
import { Group, GroupUser } from "@server/models";
import {
buildAdmin,
buildGroup,
buildTeam,
buildUser,
} from "@server/test/factories";
import { serialize } from "./index";
const loadGroup = (id: string, userId: string) =>
Group.findByPk(id, {
include: [
{
model: GroupUser,
as: "groupUsers",
required: false,
where: { userId },
},
],
});
describe("policies/group", () => {
it("should allow a group admin to update", async () => {
const team = await buildTeam();
const admin = await buildAdmin({ teamId: team.id });
const user = await buildUser({ teamId: team.id });
const group = await buildGroup({ teamId: team.id });
await GroupUser.create({
groupId: group.id,
userId: user.id,
createdById: admin.id,
permission: GroupPermission.Admin,
});
const reloaded = await loadGroup(group.id, user.id);
expect(serialize(user, reloaded).update).toBeTruthy();
});
it("should not allow a group admin from another team to update", async () => {
const user = await buildUser();
const group = await buildGroup();
await GroupUser.create({
groupId: group.id,
userId: user.id,
createdById: user.id,
permission: GroupPermission.Admin,
});
const reloaded = await loadGroup(group.id, user.id);
expect(serialize(user, reloaded).update).toEqual(false);
});
});
+31
View File
@@ -0,0 +1,31 @@
import {
buildNotification,
buildTeam,
buildUser,
} from "@server/test/factories";
import { serialize } from "./index";
describe("policies/notification", () => {
it("should allow the recipient to read and update", async () => {
const user = await buildUser();
const notification = await buildNotification({
teamId: user.teamId,
userId: user.id,
});
const abilities = serialize(user, notification);
expect(abilities.read).toBeTruthy();
expect(abilities.update).toBeTruthy();
});
it("should not allow a recipient from another team to read or update", async () => {
const user = await buildUser();
const team = await buildTeam();
const notification = await buildNotification({
teamId: team.id,
userId: user.id,
});
const abilities = serialize(user, notification);
expect(abilities.read).toEqual(false);
expect(abilities.update).toEqual(false);
});
});
+4 -2
View File
@@ -1,5 +1,7 @@
import { Notification, User } from "@server/models";
import { allow } from "./cancan";
import { isOwner } from "./utils";
import { and, isOwner, isTeamModel } from "./utils";
allow(User, ["read", "update"], Notification, isOwner);
allow(User, ["read", "update"], Notification, (actor, notification) =>
and(isTeamModel(actor, notification), isOwner(actor, notification))
);
+6
View File
@@ -53,6 +53,12 @@ describe("policies/team", () => {
expect(abilities.createIntegration).toBeTruthy();
});
it("should not allow creation from another team", async () => {
const team = await buildTeam({ memberTeamCreate: true });
const admin = await buildAdmin();
expect(serialize(admin, team).createTeam).toEqual(false);
});
describe("read template", () => {
const permissions = new Map<UserRole, boolean>([
[UserRole.Admin, true],
+1
View File
@@ -35,6 +35,7 @@ allow(User, "createTeam", Team, (actor, team) =>
and(
//
isCloudHosted(),
isTeamModel(actor, team),
!actor.isGuest,
!actor.isViewer,
or(actor.isAdmin, !!team?.memberTeamCreate)
+1 -1
View File
@@ -191,7 +191,7 @@ export function isCloudHosted() {
* @returns True if the actor is an admin of the group
*/
export function isGroupAdmin(actor: User, model: Group | null): boolean {
if (!model || !("id" in model)) {
if (!model || !("id" in model) || !isTeamModel(actor, model)) {
return false;
}