Files
cal.diy/packages/features/booking-audit/lib/service/BookingAuditAccessService.ts
T
Hariom BalharaandDevin AI f1011ddd08 chore: Improves the core booking audit architecture by adding new capabilities and simplifying the existing interfaces. (#25872)
* feat(booking-audit): extract core audit system changes from PR 25125

This PR extracts core audit infrastructure changes without integration changes:

1. New action services introduced:
   - SeatBookedAuditActionService
   - SeatRescheduledAuditActionService

2. Simplification of ActionService interface:
   - Streamlined IAuditActionService interface
   - Reduced TypeScript burden with cleaner type definitions

3. ActionSource support:
   - Added BookingAuditSource enum (API_V1, API_V2, WEBAPP, WEBHOOK, UNKNOWN)
   - Added source and operationId fields to BookingAudit model

4. New AuditAction types:
   - SEAT_BOOKED
   - SEAT_RESCHEDULED
   - APP actor type

5. New BookingAuditAccessService:
   - Permission-based access control for audit logs
   - Added readTeamAuditLogs and readOrgAuditLogs permissions

6. Fixes in the logs viewer flow:
   - Enhanced BookingAuditViewerService with improved filtering
   - Local AttendeeRepository for actor enrichment

Changes are contained within packages/features/booking-audit with minimal
outside changes (permission registry only).

Co-Authored-By: [email protected] <[email protected]>

* refactor(booking-audit): streamline action handling and enhance localization

- Replaced action icon retrieval with a mapping object for improved clarity and performance.
- Introduced constants for actor role labels to simplify role retrieval.
- Added new localization strings for audit log permission errors and organization requirements.
- Updated various service and repository interfaces to enhance type safety and clarity.
- Removed deprecated architecture documentation and adjusted related imports for consistency.

These changes aim to improve code maintainability and user experience in the booking audit system.

* fix(booking-audit): enhance actor role localization and operation ID tracking

- Updated actor role labels in the booking logs view to use lowercase for consistency.
- Improved localization by wrapping actor role display in a translation function.
- Added operationId field to audit logs for better correlation of actions across multiple bookings.
- Enhanced BookingAuditViewerService to include operationId in enriched audit logs.
- Updated integration tests to verify consistent operationId across related audit logs.

These changes aim to improve localization accuracy and facilitate better tracking of user actions in the booking audit system.

* feat: integrate credential repository and enhance app actor handling

- Added CredentialRepository to manage app credentials, including a method to find credentials by ID.
- Updated BookingAudit system to support app actors identified by credential ID, improving actor attribution and audit clarity.
- Introduced a new utility function to map app slugs to display names, enhancing the user experience in audit logs.
- Modified relevant interfaces and types to accommodate the new credential handling and app actor structure.
- Enhanced BookingAuditViewerService to display app names based on credentials, ensuring accurate representation in audit logs.

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-12-18 12:46:24 +00:00

96 lines
3.9 KiB
TypeScript

import type { BookingRepository } from "@calcom/features/bookings/repositories/BookingRepository";
import type { MembershipRepository } from "@calcom/features/membership/repositories/MembershipRepository";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { MembershipRole } from "@calcom/prisma/enums";
export enum BookingAuditErrorCode {
ORGANIZATION_ID_REQUIRED = "ORGANIZATION_ID_REQUIRED",
BOOKING_NOT_FOUND_OR_PERMISSION_DENIED = "BOOKING_NOT_FOUND_OR_PERMISSION_DENIED",
BOOKING_HAS_NO_OWNER = "BOOKING_HAS_NO_OWNER",
OWNER_NOT_IN_ORGANIZATION = "OWNER_NOT_IN_ORGANIZATION",
PERMISSION_DENIED = "PERMISSION_DENIED",
}
export class BookingAuditPermissionError extends Error {
constructor(public readonly code: BookingAuditErrorCode) {
super(code);
this.name = "BookingAuditPermissionError";
}
}
interface BookingAuditAccessServiceDeps {
bookingRepository: BookingRepository;
membershipRepository: MembershipRepository;
}
/**
* BookingAuditAccessService - Service for checking access permissions to booking audit logs
* Audit logs are admin-only for compliance and security purposes.
* Regular users (including booking organizers and hosts) cannot view audit logs.
*/
export class BookingAuditAccessService {
private readonly bookingRepository: BookingRepository;
private readonly membershipRepository: MembershipRepository;
private readonly permissionCheckService: PermissionCheckService;
constructor(deps: BookingAuditAccessServiceDeps) {
this.bookingRepository = deps.bookingRepository;
this.membershipRepository = deps.membershipRepository;
this.permissionCheckService = new PermissionCheckService();
}
/**
* Check if user has permission to view audit logs for a booking
* Throws BookingAuditPermissionError if access is denied
*/
async assertPermissions({ bookingUid, userId, organizationId }: { bookingUid: string, userId: number, organizationId: number | null }): Promise<void> {
if (!organizationId) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.ORGANIZATION_ID_REQUIRED);
}
const booking = await this.bookingRepository.findByUidIncludeEventType({ bookingUid });
if (!booking) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.BOOKING_NOT_FOUND_OR_PERMISSION_DENIED);
}
const bookingEventType = booking.eventType;
const bookingEventTypeTeamId = bookingEventType?.teamId ?? bookingEventType?.parent?.teamId;
if (bookingEventTypeTeamId) {
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId: bookingEventTypeTeamId,
permission: "booking.readTeamAuditLogs",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (hasAccess) {
return;
}
}
const bookingOwnerId = booking.userId;
if (!bookingOwnerId) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.BOOKING_HAS_NO_OWNER);
}
const isBookingOwnerMemberOfOrganization = await this.membershipRepository.hasMembership({ userId: bookingOwnerId, teamId: organizationId });
if (!isBookingOwnerMemberOfOrganization) {
throw new BookingAuditPermissionError(BookingAuditErrorCode.OWNER_NOT_IN_ORGANIZATION);
}
const hasAccess = await this.permissionCheckService.checkPermission({
userId,
teamId: organizationId,
permission: "booking.readOrgAuditLogs",
fallbackRoles: [MembershipRole.OWNER, MembershipRole.ADMIN],
});
if (hasAccess) {
return;
}
throw new BookingAuditPermissionError(BookingAuditErrorCode.PERMISSION_DENIED);
}
}