* feat: add Webhook resource to PBAC system with permission enforcement - Add Webhook resource to PBAC permission registry with CRUD actions - Implement PBAC permission checks in webhook handlers (create, edit, delete) - Add webhook permission translations to common.json - Use PermissionCheckService with fallback roles [ADMIN, OWNER] for team webhooks - Maintain backward compatibility when PBAC is disabled - Follow same pattern as workflow PBAC implementation from PR #22845 Co-Authored-By: sean@cal.com <Sean@brydon.io> * fix: implement PBAC permission filtering in webhook list handler - Add PermissionCheckService to filter team webhooks by webhook.read permission - Only show webhooks from teams where user has proper permissions - Maintain backward compatibility with fallback to all team memberships Co-Authored-By: sean@cal.com <Sean@brydon.io> * add migration for default roles * new forUserMethod * update webhook repository * fix UI showing/hiding webhooks for webhoo.create teams * WIP pbac procedure migratoin + tests * add more roles to get fallback * permissions in cmponents instead of readOnly * passPermissions to list item * push instant events logic * Git merge * wip teamId accessable refactor * fix delete handler --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
93 lines
2.6 KiB
TypeScript
93 lines
2.6 KiB
TypeScript
import { v4 } from "uuid";
|
|
|
|
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
|
|
import { updateTriggerForExistingBookings } from "@calcom/features/webhooks/lib/scheduleTrigger";
|
|
import { prisma } from "@calcom/prisma";
|
|
import type { Webhook } from "@calcom/prisma/client";
|
|
import type { Prisma } from "@calcom/prisma/client";
|
|
import { MembershipRole } from "@calcom/prisma/enums";
|
|
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
|
|
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
import type { TCreateInputSchema } from "./create.schema";
|
|
|
|
type CreateOptions = {
|
|
ctx: {
|
|
user: NonNullable<TrpcSessionUser>;
|
|
};
|
|
input: TCreateInputSchema;
|
|
};
|
|
|
|
export const createHandler = async ({ ctx, input }: CreateOptions) => {
|
|
const { user } = ctx;
|
|
|
|
const webhookData: Prisma.WebhookCreateInput = {
|
|
id: v4(),
|
|
...input,
|
|
};
|
|
if (input.platform && user.role !== "ADMIN") {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
|
|
if (input.teamId) {
|
|
const permissionService = new PermissionCheckService();
|
|
|
|
const hasPermission = await permissionService.checkPermission({
|
|
userId: user.id,
|
|
teamId: input.teamId,
|
|
permission: "webhook.create",
|
|
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
|
|
});
|
|
|
|
if (!hasPermission) {
|
|
throw new TRPCError({
|
|
code: "UNAUTHORIZED",
|
|
});
|
|
}
|
|
}
|
|
|
|
// Add userId if platform, eventTypeId, and teamId are not provided
|
|
if (!input.platform && !input.eventTypeId && !input.teamId) {
|
|
webhookData.user = { connect: { id: user.id } };
|
|
}
|
|
|
|
if (input.eventTypeId) {
|
|
const parentManagedEvt = await prisma.eventType.findFirst({
|
|
where: {
|
|
id: input.eventTypeId,
|
|
parentId: {
|
|
not: null,
|
|
},
|
|
},
|
|
select: {
|
|
parentId: true,
|
|
metadata: true,
|
|
},
|
|
});
|
|
|
|
if (parentManagedEvt?.parentId) {
|
|
const isLocked = !EventTypeMetaDataSchema.parse(parentManagedEvt.metadata)?.managedEventConfig
|
|
?.unlockedFields?.webhooks;
|
|
if (isLocked) {
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
}
|
|
}
|
|
}
|
|
|
|
let newWebhook: Webhook;
|
|
try {
|
|
newWebhook = await prisma.webhook.create({
|
|
data: webhookData,
|
|
});
|
|
} catch (error) {
|
|
// Avoid printing raw prisma error on frontend
|
|
throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: "Failed to create webhook" });
|
|
}
|
|
|
|
await updateTriggerForExistingBookings(newWebhook, [], newWebhook.eventTriggers);
|
|
|
|
return newWebhook;
|
|
};
|