feat: add Workflow resource to PBAC system with permission enforcement (#22845)
Co-authored-by: [email protected] <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
18 files changed
+820
-139
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import type { PermissionString } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
||||
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
|
||||
import { withRoleCanCreateEntity } from "@calcom/lib/entityPermissionUtils.server";
|
||||
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
@@ -89,6 +92,29 @@ export const teamsAndUserProfilesQuery = async ({ ctx, input }: TeamsAndUserProf
|
||||
}));
|
||||
}
|
||||
|
||||
// Filter teams based on permission if provided
|
||||
let hasPermissionForFiltered: boolean[] = [];
|
||||
if (input?.withPermission) {
|
||||
const permissionService = new PermissionCheckService();
|
||||
const { permission, fallbackRoles } = input.withPermission;
|
||||
|
||||
const permissionChecks = await Promise.all(
|
||||
teamsData.map((membership) =>
|
||||
permissionService.checkPermission({
|
||||
userId: ctx.user.id,
|
||||
teamId: membership.team.id,
|
||||
permission: permission as PermissionString,
|
||||
fallbackRoles: fallbackRoles ? (fallbackRoles as MembershipRole[]) : [],
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
// Store permission results for teams that passed the filter
|
||||
hasPermissionForFiltered = permissionChecks.filter((hasPermission) => hasPermission);
|
||||
teamsData = teamsData.filter((_, index) => permissionChecks[index]);
|
||||
|
||||
}
|
||||
|
||||
return [
|
||||
{
|
||||
teamId: null,
|
||||
@@ -99,7 +125,7 @@ export const teamsAndUserProfilesQuery = async ({ ctx, input }: TeamsAndUserProf
|
||||
}),
|
||||
readOnly: false,
|
||||
},
|
||||
...teamsData.map((membership) => ({
|
||||
...teamsData.map((membership, index) => ({
|
||||
teamId: membership.team.id,
|
||||
name: membership.team.name,
|
||||
slug: membership.team.slug ? `team/${membership.team.slug}` : null,
|
||||
@@ -107,7 +133,9 @@ export const teamsAndUserProfilesQuery = async ({ ctx, input }: TeamsAndUserProf
|
||||
? getPlaceholderAvatar(membership.team.parent.logoUrl, membership.team.parent.name)
|
||||
: getPlaceholderAvatar(membership.team.logoUrl, membership.team.name),
|
||||
role: membership.role,
|
||||
readOnly: !withRoleCanCreateEntity(membership.role),
|
||||
readOnly: input?.withPermission
|
||||
? !hasPermissionForFiltered[index]
|
||||
: !withRoleCanCreateEntity(membership.role),
|
||||
})),
|
||||
];
|
||||
};
|
||||
@@ -3,6 +3,12 @@ import { z } from "zod";
|
||||
export const ZTeamsAndUserProfilesQueryInputSchema = z
|
||||
.object({
|
||||
includeOrg: z.boolean().optional(),
|
||||
withPermission: z
|
||||
.object({
|
||||
permission: z.string(),
|
||||
fallbackRoles: z.array(z.string()).optional(),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
.optional();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { Workflow } from "@prisma/client";
|
||||
|
||||
import emailReminderTemplate from "@calcom/ee/workflows/lib/reminders/templates/emailReminderTemplate";
|
||||
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
||||
import { SENDER_NAME } from "@calcom/lib/constants";
|
||||
import { getTranslation } from "@calcom/lib/server/i18n";
|
||||
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
|
||||
@@ -33,22 +34,16 @@ export const createHandler = async ({ ctx, input }: CreateOptions) => {
|
||||
const userId = ctx.user.id;
|
||||
|
||||
if (teamId) {
|
||||
const team = await prisma.team.findFirst({
|
||||
where: {
|
||||
id: teamId,
|
||||
members: {
|
||||
some: {
|
||||
userId: ctx.user.id,
|
||||
accepted: true,
|
||||
NOT: {
|
||||
role: MembershipRole.MEMBER,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
const permissionService = new PermissionCheckService();
|
||||
|
||||
const hasPermission = await permissionService.checkPermission({
|
||||
userId,
|
||||
teamId,
|
||||
permission: "workflow.create",
|
||||
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
||||
});
|
||||
|
||||
if (!team) {
|
||||
if (!hasPermission) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
});
|
||||
|
||||
@@ -32,7 +32,7 @@ export const deleteHandler = async ({ ctx, input }: DeleteOptions) => {
|
||||
},
|
||||
});
|
||||
|
||||
const isUserAuthorized = await isAuthorized(workflowToDelete, ctx.user.id, true);
|
||||
const isUserAuthorized = await isAuthorized(workflowToDelete, ctx.user.id, "workflow.delete");
|
||||
|
||||
if (!isUserAuthorized || !workflowToDelete) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { WorkflowRepository } from "@calcom/lib/server/repository/workflow";
|
||||
import { addPermissionsToWorkflows } from "@calcom/lib/server/repository/workflow-permissions";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import type { Prisma } from "@calcom/prisma/client";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
@@ -56,5 +57,19 @@ const { include: includedFields } = {
|
||||
} satisfies Prisma.WorkflowDefaultArgs;
|
||||
|
||||
export const filteredListHandler = async ({ ctx, input }: FilteredListOptions) => {
|
||||
return await WorkflowRepository.getFilteredList({ userId: ctx.user.id, input });
|
||||
const result = await WorkflowRepository.getFilteredList({ userId: ctx.user.id, input });
|
||||
|
||||
if (!result) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Add permissions to each workflow
|
||||
const workflowsWithPermissions = await addPermissionsToWorkflows(result.filtered, ctx.user.id);
|
||||
|
||||
const filteredWorkflows = workflowsWithPermissions.filter((workflow) => workflow.permissions.canView);
|
||||
|
||||
return {
|
||||
...result,
|
||||
filtered: filteredWorkflows,
|
||||
};
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
import { WorkflowRepository } from "@calcom/lib/server/repository/workflow";
|
||||
import { addPermissionsToWorkflow } from "@calcom/lib/server/repository/workflow-permissions";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
@@ -16,7 +17,7 @@ type GetOptions = {
|
||||
export const getHandler = async ({ ctx, input }: GetOptions) => {
|
||||
const workflow = await WorkflowRepository.getById({ id: input.id });
|
||||
|
||||
const isUserAuthorized = await isAuthorized(workflow, ctx.user.id);
|
||||
const isUserAuthorized = await isAuthorized(workflow, ctx.user.id, "workflow.read");
|
||||
|
||||
if (!isUserAuthorized) {
|
||||
throw new TRPCError({
|
||||
@@ -24,5 +25,12 @@ export const getHandler = async ({ ctx, input }: GetOptions) => {
|
||||
});
|
||||
}
|
||||
|
||||
return workflow;
|
||||
if (!workflow) {
|
||||
return workflow;
|
||||
}
|
||||
|
||||
// Add permissions to the workflow
|
||||
const workflowWithPermissions = await addPermissionsToWorkflow(workflow, ctx.user.id);
|
||||
|
||||
return workflowWithPermissions;
|
||||
};
|
||||
@@ -1,10 +1,11 @@
|
||||
import { isEmailAction } from "@calcom/features/ee/workflows/lib/actionHelperFunctions";
|
||||
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
||||
import tasker from "@calcom/features/tasker";
|
||||
import { IS_SELF_HOSTED, SCANNING_WORKFLOW_STEPS } from "@calcom/lib/constants";
|
||||
import hasKeyInMetadata from "@calcom/lib/hasKeyInMetadata";
|
||||
import { WorkflowRepository } from "@calcom/lib/server/repository/workflow";
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { WorkflowActions, WorkflowTemplates } from "@calcom/prisma/enums";
|
||||
import { WorkflowActions, WorkflowTemplates, MembershipRole } from "@calcom/prisma/enums";
|
||||
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
||||
|
||||
import { TRPCError } from "@trpc/server";
|
||||
@@ -66,7 +67,18 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
const isOrg = !!userWorkflow?.team?.isOrganization;
|
||||
|
||||
const isUserAuthorized = await isAuthorized(userWorkflow, ctx.user.id, true);
|
||||
let isUserAuthorized = false;
|
||||
if (userWorkflow?.teamId) {
|
||||
const permissionService = new PermissionCheckService();
|
||||
isUserAuthorized = await permissionService.checkPermission({
|
||||
userId: ctx.user.id,
|
||||
teamId: userWorkflow.teamId,
|
||||
permission: "workflow.update",
|
||||
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
||||
});
|
||||
} else {
|
||||
isUserAuthorized = await isAuthorized(userWorkflow, ctx.user.id, "workflow.update");
|
||||
}
|
||||
|
||||
if (!isUserAuthorized || !userWorkflow) {
|
||||
throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||
@@ -294,7 +306,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
let newStep;
|
||||
|
||||
if (foundStep) {
|
||||
const { senderName, ...rest } = {
|
||||
const { senderName: _senderName, ...rest } = {
|
||||
...foundStep,
|
||||
numberVerificationPending: false,
|
||||
sender: getSender({
|
||||
@@ -441,7 +453,7 @@ export const updateHandler = async ({ ctx, input }: UpdateOptions) => {
|
||||
|
||||
const {
|
||||
id: _stepId,
|
||||
senderName,
|
||||
senderName: _senderName,
|
||||
...stepToAdd
|
||||
} = {
|
||||
...newStep,
|
||||
|
||||
@@ -0,0 +1,393 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
||||
|
||||
import { isAuthorized } from "./util";
|
||||
|
||||
vi.mock("@calcom/features/pbac/services/permission-check.service");
|
||||
|
||||
describe("isAuthorized", () => {
|
||||
const mockPermissionCheckService = vi.mocked(PermissionCheckService);
|
||||
let mockCheckPermission: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockCheckPermission = vi.fn();
|
||||
mockPermissionCheckService.mockImplementation(
|
||||
() =>
|
||||
({
|
||||
checkPermission: mockCheckPermission,
|
||||
} as any)
|
||||
);
|
||||
});
|
||||
|
||||
describe("null workflow", () => {
|
||||
it("should return false when workflow is null", async () => {
|
||||
const result = await isAuthorized(null, 123);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("personal workflows (no teamId)", () => {
|
||||
it("should return true when user owns the personal workflow", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: null,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
const result = await isAuthorized(workflow, 123);
|
||||
expect(result).toBe(true);
|
||||
expect(mockPermissionCheckService).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should return false when user does not own the personal workflow", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: null,
|
||||
userId: 456,
|
||||
};
|
||||
|
||||
const result = await isAuthorized(workflow, 123);
|
||||
expect(result).toBe(false);
|
||||
expect(mockPermissionCheckService).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should ignore permission parameter for personal workflows", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: null,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
const readResult = await isAuthorized(workflow, 123, "workflow.read");
|
||||
const updateResult = await isAuthorized(workflow, 123, "workflow.update");
|
||||
const deleteResult = await isAuthorized(workflow, 123, "workflow.delete");
|
||||
|
||||
expect(readResult).toBe(true);
|
||||
expect(updateResult).toBe(true);
|
||||
expect(deleteResult).toBe(true);
|
||||
expect(mockPermissionCheckService).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("team workflows with PBAC", () => {
|
||||
describe("read operations", () => {
|
||||
it("should use workflow.read permission by default with all roles as fallback", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123);
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockPermissionCheckService).toHaveBeenCalledTimes(1);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.read",
|
||||
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should use workflow.read permission when explicitly passed", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.read");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.read",
|
||||
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should return false when PBAC denies read permission", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(false);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.read");
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.read",
|
||||
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("update operations", () => {
|
||||
it("should use workflow.update permission with admin/owner roles as fallback", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.update");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockPermissionCheckService).toHaveBeenCalledTimes(1);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.update",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should return false when PBAC denies update permission", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(false);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.update");
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.update",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("delete operations", () => {
|
||||
it("should use workflow.delete permission with admin/owner roles as fallback", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.delete");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.delete",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should return false when PBAC denies delete permission", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(false);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.delete");
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.delete",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("other permissions", () => {
|
||||
it("should use workflow.create permission with admin/owner roles as fallback", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.create");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.create",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should use workflow.manage permission with admin/owner roles as fallback", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.manage");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 456,
|
||||
permission: "workflow.manage",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission service integration", () => {
|
||||
it("should create a new PermissionCheckService instance for each call", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
await isAuthorized(workflow, 123, "workflow.read");
|
||||
await isAuthorized(workflow, 123, "workflow.update");
|
||||
|
||||
expect(mockPermissionCheckService).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it("should handle permission service errors gracefully", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockRejectedValue(new Error("Permission service error"));
|
||||
|
||||
await expect(isAuthorized(workflow, 123, "workflow.read")).rejects.toThrow(
|
||||
"Permission service error"
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("edge cases", () => {
|
||||
it("should handle workflow with teamId 0 as personal workflow", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 0,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.delete");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockPermissionCheckService).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should handle workflow with positive teamId as team workflow", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 1,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.read");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 123,
|
||||
teamId: 1,
|
||||
permission: "workflow.read",
|
||||
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle different user IDs correctly", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 789, "workflow.read");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockCheckPermission).toHaveBeenCalledWith({
|
||||
userId: 789,
|
||||
teamId: 456,
|
||||
permission: "workflow.read",
|
||||
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should handle workflow with undefined teamId as personal workflow", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: undefined as any,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.delete");
|
||||
|
||||
expect(result).toBe(true);
|
||||
expect(mockPermissionCheckService).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("type safety", () => {
|
||||
it("should work with minimal workflow object", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: null,
|
||||
userId: 123,
|
||||
};
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.read");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should work with workflow object containing extra properties", async () => {
|
||||
const workflow = {
|
||||
id: 1,
|
||||
teamId: 456,
|
||||
userId: 123,
|
||||
name: "Test Workflow",
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
mockCheckPermission.mockResolvedValue(true);
|
||||
|
||||
const result = await isAuthorized(workflow, 123, "workflow.update");
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -15,6 +15,8 @@ import {
|
||||
getSmsReminderNumberSource,
|
||||
} from "@calcom/features/bookings/lib/getBookingFields";
|
||||
import { removeBookingField, upsertBookingField } from "@calcom/features/eventtypes/lib/bookingFieldsManager";
|
||||
import type { PermissionString } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
||||
import { SENDER_ID, SENDER_NAME } from "@calcom/lib/constants";
|
||||
import { getBookerBaseUrl } from "@calcom/lib/getBookerUrl/server";
|
||||
import getOrgIdFromMemberOrTeamId from "@calcom/lib/getOrgIdFromMemberOrTeamId";
|
||||
@@ -229,62 +231,33 @@ export function getSender(
|
||||
export async function isAuthorized(
|
||||
workflow: Pick<Workflow, "id" | "teamId" | "userId"> | null,
|
||||
currentUserId: number,
|
||||
isWriteOperation?: boolean
|
||||
permission: PermissionString = "workflow.read"
|
||||
) {
|
||||
if (!workflow) {
|
||||
return false;
|
||||
}
|
||||
if (!isWriteOperation) {
|
||||
const userWorkflow = await prisma.workflow.findFirst({
|
||||
where: {
|
||||
id: workflow.id,
|
||||
OR: [
|
||||
{ userId: currentUserId },
|
||||
{
|
||||
// for read operation every team member has access
|
||||
team: {
|
||||
members: {
|
||||
some: {
|
||||
userId: currentUserId,
|
||||
accepted: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
if (userWorkflow) return true;
|
||||
|
||||
// For personal workflows (no teamId), check if user owns the workflow
|
||||
if (!workflow.teamId) {
|
||||
return workflow.userId === currentUserId;
|
||||
}
|
||||
|
||||
const userWorkflow = await prisma.workflow.findFirst({
|
||||
where: {
|
||||
id: workflow.id,
|
||||
OR: [
|
||||
{ userId: currentUserId },
|
||||
{
|
||||
team: {
|
||||
members: {
|
||||
some: {
|
||||
userId: currentUserId,
|
||||
accepted: true,
|
||||
//only admins can update team/org workflows
|
||||
NOT: {
|
||||
role: MembershipRole.MEMBER,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
// For team workflows, use PBAC
|
||||
const permissionService = new PermissionCheckService();
|
||||
|
||||
// Determine fallback roles based on permission type
|
||||
const fallbackRoles =
|
||||
permission === "workflow.read"
|
||||
? [MembershipRole.ADMIN, MembershipRole.OWNER, MembershipRole.MEMBER]
|
||||
: [MembershipRole.ADMIN, MembershipRole.OWNER];
|
||||
|
||||
return await permissionService.checkPermission({
|
||||
userId: currentUserId,
|
||||
teamId: workflow.teamId,
|
||||
permission,
|
||||
fallbackRoles,
|
||||
});
|
||||
|
||||
if (userWorkflow) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function upsertSmsReminderFieldForEventTypes({
|
||||
activeOn,
|
||||
workflowId,
|
||||
|
||||
Reference in new issue
Block a user