Scheduling infrastructure for absolutely everyone.
chore: PBAC routingform entity permissions to favour pbac (#24130)
sean-brydon928d6f3821a9
diff --git a/packages/lib/entityPermissionUtils.server.ts b/packages/lib/entityPermissionUtils.server.tsindex 395c02e2e8f..be56524ed4e 100644--- a/packages/lib/entityPermissionUtils.server.ts+++ b/packages/lib/entityPermissionUtils.server.ts@@ -1,102 +1,41 @@+import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";import { MembershipRole } from "@calcom/prisma/enums";-export const enum ENTITY_PERMISSION_LEVEL {- NONE,- // It is owned by user and user has write access to it- USER_ONLY_WRITE,- // All members of the team has access to it and user has read access to it- TEAM_READ_ONLY,- // All members of the team has access to it and user has write access to it- TEAM_WRITE,-}--export async function canEditEntity(- entity: Parameters<typeof getEntityPermissionLevel>[0],- userId: Parameters<typeof getEntityPermissionLevel>[1]-) {- const permissionLevel = await getEntityPermissionLevel(entity, userId);- return (- permissionLevel === ENTITY_PERMISSION_LEVEL.TEAM_WRITE ||- permissionLevel === ENTITY_PERMISSION_LEVEL.USER_ONLY_WRITE- );-}--export async function canAccessEntity(- entity: Parameters<typeof getEntityPermissionLevel>[0],- userId: Parameters<typeof getEntityPermissionLevel>[1]-) {- const permissionLevel = await getEntityPermissionLevel(entity, userId);- return (- permissionLevel === ENTITY_PERMISSION_LEVEL.TEAM_WRITE ||- permissionLevel === ENTITY_PERMISSION_LEVEL.USER_ONLY_WRITE ||- permissionLevel === ENTITY_PERMISSION_LEVEL.TEAM_READ_ONLY- );-}+export type Entity = {+ userId: number | null;+ teamId: number | null;+};-export async function getEntityPermissionLevel(- entity: {- userId: number | null;- teamId: number | null;- },- userId: number-) {+export async function canEditEntity(entity: Entity, userId: number) {if (entity.teamId) {- const { default: prisma } = await import("@calcom/prisma");- const membership = await prisma.membership.findFirst({- where: {- teamId: entity.teamId,- userId,- accepted: true,- },+ const permissionService = new PermissionCheckService();+ const hasEditPermission = await permissionService.checkPermission({+ teamId: entity.teamId,+ userId,+ permission: "routingForm.update",+ fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],});- const roleForTeamMember = membership?.role;- if (roleForTeamMember) {- //TODO: Remove type assertion- const hasWriteAccessToTeam = (- [MembershipRole.ADMIN, MembershipRole.OWNER] as unknown as MembershipRole- ).includes(roleForTeamMember);- if (hasWriteAccessToTeam) {- return ENTITY_PERMISSION_LEVEL.TEAM_WRITE;- } else {- return ENTITY_PERMISSION_LEVEL.TEAM_READ_ONLY;- }- }- }+ return hasEditPermission;+ } else if (entity.userId === userId) return true;- const ownedByUser = entity.userId === userId;- if (ownedByUser) {- return ENTITY_PERMISSION_LEVEL.USER_ONLY_WRITE;- }-- return ENTITY_PERMISSION_LEVEL.NONE;+ return false;}-async function getMembership(teamId: number | null, userId: number) {- const { default: prisma } = await import("@calcom/prisma");+export async function canAccessEntity(entity: Entity, userId: number) {+ if (entity.teamId) {+ const permissionService = new PermissionCheckService();+ const hasReadPermission = await permissionService.checkPermission({+ teamId: entity.teamId,+ userId,+ permission: "routingForm.read",+ fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER, MembershipRole.MEMBER],+ });++ return hasReadPermission;+ } else if (entity.userId === userId) return true;- const team = teamId- ? await prisma.team.findFirst({- where: {- id: teamId,- members: {- some: {- userId,- accepted: true,- },- },- },- include: {- members: {- select: {- userId: true,- role: true,- },- },- },- })- : null;- return team?.members.find((membership) => membership.userId === userId);+ return false;}export async function canCreateEntity({@@ -107,19 +46,19 @@ export async function canCreateEntity({userId: number;}) {if (targetTeamId) {- // If it doesn't exist and it is being created for a team. Check if user is the member of the team- const membership = await getMembership(targetTeamId, userId);- return membership ? withRoleCanCreateEntity(membership.role) : false;+ const permissionService = new PermissionCheckService();+ const hasEditPermission = await permissionService.checkPermission({+ teamId: targetTeamId,+ userId,+ permission: "routingForm.create",+ fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],+ });++ return hasEditPermission;}return true;}-//TODO: Find a better convention to create different functions for different needs(withRoleCanCreateEntity vs canCreateEntity)-// e.g. if role is already available we don't need to refetch membership.role. We can directly call `withRoleCanCreateEntity`-export function withRoleCanCreateEntity(role: MembershipRole) {- return role === "ADMIN" || role === "OWNER";-}-/*** Whenever the entity is fetched this clause should be there to ensure that* 1. No item that doesn't belong to the user or the team is fetcheddiff --git a/packages/trpc/server/routers/loggedInViewer/teamsAndUserProfilesQuery.handler.ts b/packages/trpc/server/routers/loggedInViewer/teamsAndUserProfilesQuery.handler.tsindex 9f557359459..77ed8f1dc8c 100644--- a/packages/trpc/server/routers/loggedInViewer/teamsAndUserProfilesQuery.handler.ts+++ b/packages/trpc/server/routers/loggedInViewer/teamsAndUserProfilesQuery.handler.ts@@ -1,10 +1,9 @@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 { MembershipRole } from "@calcom/prisma/enums";import { teamMetadataSchema } from "@calcom/prisma/zod-utils";import type { TrpcSessionUser } from "@calcom/trpc/server/types";@@ -114,6 +113,8 @@ export const teamsAndUserProfilesQuery = async ({ ctx, input }: TeamsAndUserProfteamsData = teamsData.filter((_, index) => permissionChecks[index]);}+ const rolesWithWriteAccess = [MembershipRole.ADMIN, MembershipRole.OWNER] as MembershipRole[];+return [{teamId: null,@@ -134,7 +135,7 @@ export const teamsAndUserProfilesQuery = async ({ ctx, input }: TeamsAndUserProfrole: membership.role,readOnly: input?.withPermission? !hasPermissionForFiltered[index]- : !withRoleCanCreateEntity(membership.role),+ : !rolesWithWriteAccess.includes(membership.role),})),];};