Files
calendar/packages/features/bookings/services/BookingAccessService.ts
T
Eunjae LeeGitHubClaude Opus 4.6Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
7ff2eaf293 fix: consolidate booking access checks into doesUserIdHaveAccessToBooking (#28071)
* fix: align checkBookingAccessWithPBAC with listing logic for personal event types

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* fix: align permission strings with doesUserIdHaveAccessToBooking granular permissions

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* change implementation

* remove unused code

* test: add getBookingDetails tests for personal event type booking access

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* fix: remove unused MembershipRole import

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* test: convert BookingDetailsService tests to integration tests

Replace unit tests with mocks by integration tests using real database.
Tests verify org/team admin access to personal event type bookings.

- Org admin viewing team member's personal event booking (fails on main, passes on PR)
- Team admin viewing team member's personal event booking (fails on main, passes on PR)
- Non-admin denied access (passes on both)
- Owner viewing own booking (passes on both)
- Non-existent booking returns Forbidden (passes on both)

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* test: refactor integration tests to use repositories instead of direct prisma calls

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* test: replace direct prisma usage with TestXXXRepository pattern in integration tests

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* test: use production repositories (OrganizationRepository, BookingRepository, UserRepository) instead of test repos for setup

Co-Authored-By: eunjae@cal.com <hey@eunjae.dev>

* test: inline test repository logic into integration test file

The TestXXXRepository classes were thin wrappers around single prisma
calls with barely any logic, so the indirection wasn't justified.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 14:46:57 -03:00

134 lines
4.6 KiB
TypeScript

import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { UserRepository } from "@calcom/features/users/repositories/UserRepository";
import type { PrismaClient } from "@calcom/prisma";
import { MembershipRole } from "@calcom/prisma/enums";
import { BookingRepository } from "../repositories/BookingRepository";
type BookingForAccessCheck = NonNullable<Awaited<ReturnType<BookingRepository["findByUidIncludeEventType"]>>>;
export class BookingAccessService {
private permissionCheckService: PermissionCheckService;
constructor(private prismaClient: PrismaClient) {
this.permissionCheckService = new PermissionCheckService();
}
private isUserAHost(userId: number, booking: BookingForAccessCheck): boolean {
const hostMap = new Map<number, { id: number; email: string }>();
const addHost = (id: number, email: string) => {
if (!hostMap.has(id)) {
hostMap.set(id, { id, email });
}
};
booking?.eventType?.hosts?.forEach((host: { userId: number; user: { email: string } }) =>
addHost(host.userId, host.user.email)
);
booking?.eventType?.users?.forEach((user: { id: number; email: string }) => addHost(user.id, user.email));
if (booking?.user?.id && booking?.user?.email) {
addHost(booking.user.id, booking.user.email);
}
const attendeeEmails = new Set(booking.attendees?.map((attendee: { email: string }) => attendee.email));
const filteredHosts = Array.from(hostMap.values()).filter(
(host) => attendeeEmails.has(host.email) || host.id === booking.user?.id
);
return filteredHosts.some((host) => host.id === userId);
}
/**
* Determines if a user has access to a booking based on:
* 1. Being the booking organizer
* 2. Being one of the hosts in a multi-host booking
* 3. Being a team/org admin where the event type belongs (uses PBAC if enabled)
* 4. Being an org admin where the booking organizer belongs (uses PBAC if enabled, for personal bookings)
* 5. Being a team admin of any team the booking organizer belongs to (uses PBAC if enabled, for personal bookings)
*/
async doesUserIdHaveAccessToBooking({
userId,
bookingUid,
bookingId,
}: {
userId: number;
bookingUid?: string;
bookingId?: number;
}): Promise<boolean> {
const bookingRepo = new BookingRepository(this.prismaClient);
const userRepo = new UserRepository(this.prismaClient);
// Fetch booking by UID or ID
const booking = bookingUid
? await bookingRepo.findByUidIncludeEventType({ bookingUid })
: bookingId
? await bookingRepo.findByIdIncludeEventType({ bookingId })
: null;
if (!booking) return false;
// Case 1: User is the booking organizer
if (userId === booking.userId) return true;
// Case 2: User is one of the hosts
if (this.isUserAHost(userId, booking)) return true;
// Case 3: If booking has a teamId, check if user has access to team bookings
if (booking.eventType?.teamId) {
const teamId = booking.eventType.teamId;
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId,
permission: "booking.readTeamBookings",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
return hasAccess;
}
// For managed events (child event types), check the parent's teamId
if (booking.eventType?.parent?.teamId) {
const isAdminOrUser = await userRepo.isAdminOfTeamOrParentOrg({
userId,
teamId: booking.eventType.parent.teamId,
});
return isAdminOrUser;
}
if (!booking.userId) return false;
const bookingOwner = await userRepo.getUserOrganizationAndTeams({ userId: booking.userId });
if (!bookingOwner) return false;
// Case 4: Check if user is admin of booking organizer's organization
if (bookingOwner.organizationId) {
const orgId = bookingOwner.organizationId;
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId: orgId,
permission: "booking.readOrgBookings",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (hasAccess) return true;
}
// Case 5: Check if user is admin of any team the booking organizer belongs to
for (const membership of bookingOwner.teams) {
const teamId = membership.teamId;
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId,
permission: "booking.readTeamBookings",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (hasAccess) return true;
}
return false;
}
}