Allow non-admin team members also to reroute (#17234)

This commit is contained in:
Hariom Balhara authored and GitHub committed 2024-10-22 13:46:30 +05:30
1 parent 2f4dd328c3
commit 86af6bd626
4 files changed
+35 -11

No files matched your search

@@ -1,5 +1,7 @@
import type { z } from "zod";
import { canAccessEntity } from "@calcom/lib/entityPermissionUtils";
import { getTranslation } from "@calcom/lib/server/i18n";
import { prisma } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
@@ -9,7 +11,6 @@ import { enrichFormWithMigrationData } from "../enrichFormWithMigrationData";
import { getSerializableForm } from "../lib/getSerializableForm";
import type { FormResponse } from "../types/types";
import type { ZFormByResponseIdInputSchema } from "./_router";
import { canEditEntity } from "@calcom/lib/entityPermissionUtils";
type GetResponseWithFormFieldsOptions = {
ctx: {
@@ -21,8 +22,8 @@ type GetResponseWithFormFieldsOptions = {
async function getResponseWithFormFieldsHandler({ ctx, input }: GetResponseWithFormFieldsOptions) {
const { user } = ctx;
const { formResponseId } = input;
const translate = await getTranslation(user.locale ?? "en", "common");
const formResponse = await prisma.app_RoutingForms_FormResponse.findUnique({
where: {
id: formResponseId,
@@ -63,18 +64,20 @@ async function getResponseWithFormFieldsHandler({ ctx, input }: GetResponseWithF
});
if (!formResponse) {
throw new TRPCError({
code: "NOT_FOUND",
message: "Form response not found",
});
throw new TRPCError({
code: "NOT_FOUND",
message: translate("form_response_not_found"),
});
}
const form = formResponse.form;
if (!canEditEntity(form, user.id)) {
// TODO: To make the check stricter, we could check if the user is admin/owner of the team or a member that is the organizer.
// But the exact criteria of showing a booking to the user could be trickier. Maybe we allow hosts as well to access the booking and thus should allow them as well to reroute
if (!canAccessEntity(form, user.id)) {
throw new TRPCError({
code: "FORBIDDEN",
message: "You don't have access to this form",
message: translate("you_dont_have_access_to_reroute_this_booking"),
});
}
+15
View File
@@ -3,8 +3,11 @@ 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,
}
@@ -19,6 +22,18 @@ export function canEditEntity(
);
}
export function canAccessEntity(
entity: Parameters<typeof getEntityPermissionLevel>[0],
userId: Parameters<typeof getEntityPermissionLevel>[1]
) {
const permissionLevel = 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 function getEntityPermissionLevel(
entity: {
userId: number | null;