86 lines
3.8 KiB
TypeScript
86 lines
3.8 KiB
TypeScript
// AK-P16a · shared loader/action guard for the calendar module (mirrors
|
|
// integrationsAccess / taskAccess). Calendar is an Apps-Engine integration, so the
|
|
// MODULE gate reuses the `integrations` feature (tenantHasModule) — there is no
|
|
// separate plan-gated "calendar" feature (M25 Block I: connecting your own
|
|
// calendar is a default tenant-user capability). The PER-USER capability is gated
|
|
// by the `calendar.connect_own` permission; `calendar.admin` is tenant-wide
|
|
// oversight. Module-off renders a locked empty VM on GET and 404s on write
|
|
// (Bauprinzip A.5.3); a permission miss is also a 404, never a 403.
|
|
|
|
import { CatalogError, foundationErrors } from "@hivegrid/foundation";
|
|
import { requireAuth } from "~/utils/loaders.middleware";
|
|
import { getTenantIdFromUrl } from "~/utils/services/.server/urlService";
|
|
import { getUserInfo } from "~/utils/session.server";
|
|
import { tenantHasModule } from "~/utils/connector/tenantConnector.server";
|
|
import { getPermissionsByUser } from "~/utils/db/permissions/userRoles.db.server";
|
|
import { INTEGRATIONS_MODULE_KEY } from "~/utils/integrations/integrationsRoutePaths";
|
|
|
|
/** Calendar atomic permissions (M25 Block I). */
|
|
export const CALENDAR_PERMISSIONS = {
|
|
/** Connect/manage your OWN calendar connection (default tenant-user capability). */
|
|
connectOwn: "calendar.connect_own",
|
|
/** Tenant-wide oversight + force-disconnect on user offboarding. */
|
|
admin: "calendar.admin",
|
|
} as const;
|
|
|
|
export type CalendarRouteContext = {
|
|
tenantId: string;
|
|
userId: string;
|
|
permissions: readonly string[];
|
|
hasModule: boolean;
|
|
};
|
|
|
|
/**
|
|
* Resolve the calendar route context: auth + tenant + the integrations module gate
|
|
* + the user's permissions. With `requireModule` a module-off tenant 404s (write
|
|
* paths); GET paths can render a locked empty VM from `hasModule === false`.
|
|
*/
|
|
export async function loadCalendarContext({
|
|
request,
|
|
params,
|
|
requireModule = false,
|
|
}: {
|
|
request: Request;
|
|
params: Record<string, string | undefined>;
|
|
requireModule?: boolean;
|
|
}): Promise<CalendarRouteContext> {
|
|
await requireAuth({ request, params });
|
|
const [tenantId, userInfo] = await Promise.all([getTenantIdFromUrl(params), getUserInfo(request)]);
|
|
if (!tenantId || !userInfo.userId) {
|
|
throw new CatalogError(foundationErrors.notFound);
|
|
}
|
|
const [hasModule, permissions] = await Promise.all([
|
|
tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: INTEGRATIONS_MODULE_KEY }),
|
|
getPermissionsByUser(userInfo.userId, tenantId),
|
|
]);
|
|
if (requireModule && !hasModule) {
|
|
throw new CatalogError(foundationErrors.notFound, { module: INTEGRATIONS_MODULE_KEY });
|
|
}
|
|
return { tenantId, userId: userInfo.userId, permissions, hasModule };
|
|
}
|
|
|
|
/** True if the user holds `permission` or the broader `calendar.admin` bundle. */
|
|
export function calendarHasPermission(ctx: CalendarRouteContext, permission: string): boolean {
|
|
return ctx.permissions.includes(permission) || ctx.permissions.includes(CALENDAR_PERMISSIONS.admin);
|
|
}
|
|
|
|
/** Require a calendar.* permission (admin-aware); 404 if missing (A.5.3). */
|
|
export function requireCalendarPermission(ctx: CalendarRouteContext, permission: string): CalendarRouteContext {
|
|
if (!calendarHasPermission(ctx, permission)) {
|
|
throw new CatalogError(foundationErrors.notFound, { permission });
|
|
}
|
|
return ctx;
|
|
}
|
|
|
|
/**
|
|
* Owner-check for connection-scoped routes (leak-protection, DoD 3): only the
|
|
* owning user may see/manage a connection's external event details — UNLESS the
|
|
* caller holds `calendar.admin` (tenant oversight). A miss is a 404, not a 403.
|
|
*/
|
|
export function requireConnectionOwner(ctx: CalendarRouteContext, connectionUserId: string): CalendarRouteContext {
|
|
if (ctx.userId === connectionUserId || ctx.permissions.includes(CALENDAR_PERMISSIONS.admin)) {
|
|
return ctx;
|
|
}
|
|
throw new CatalogError(foundationErrors.notFound, { owner: "mismatch" });
|
|
}
|