chore: PBAC routingform entity permissions to favour pbac (#24130)
* Remove routingform entity permissions to favour pbac * push typo * fix types * update returns * Remove unused function * nits * fix hariom feedback * fix correct write permissions
This commit is contained in:
1 parent
b2ade568c8
commit
928d6f3821
2 files changed
+41
-101
No files matched your search
@@ -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 type Entity = {
|
||||
userId: number | null;
|
||||
teamId: number | null;
|
||||
};
|
||||
|
||||
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 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],
|
||||
});
|
||||
|
||||
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 hasReadPermission;
|
||||
} else if (entity.userId === userId) return true;
|
||||
|
||||
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 fetched
|
||||
|
||||
@@ -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 }: TeamsAndUserProf
|
||||
teamsData = 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 }: TeamsAndUserProf
|
||||
role: membership.role,
|
||||
readOnly: input?.withPermission
|
||||
? !hasPermissionForFiltered[index]
|
||||
: !withRoleCanCreateEntity(membership.role),
|
||||
: !rolesWithWriteAccess.includes(membership.role),
|
||||
})),
|
||||
];
|
||||
};
|
||||
Reference in new issue
Block a user