Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/app.$tenant/settings/hub-access.server.ts
T

50 lines
2.0 KiB
TypeScript

import type { DefaultPermission } from "~/application/dtos/shared/DefaultPermissions";
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
/**
* AK-SET-01 · shared loader guards for the settings hubs.
*
* A hub bundles tabs with different permissions, so it needs to ask "may this
* user see this tab?" without the answer aborting the whole request.
*/
/**
* Permission probe that reports instead of throwing.
*
* `verifyUserHasPermission` throws a redirect/404 Response on denial, which is
* right for a leaf route but wrong for a hub: one denied tab must not take the
* remaining tabs down with it. Anything that is not a denial still propagates.
*/
export async function hasSettingsPermission(request: Request, permission: DefaultPermission, tenantId: string): Promise<boolean> {
try {
await verifyUserHasPermission(request, permission, tenantId);
return true;
} catch (error) {
if (error instanceof Response && ((error.status >= 300 && error.status < 400) || error.status === 403 || error.status === 404)) return false;
throw error;
}
}
/**
* A hub with no reachable tab renders nothing useful, so it must not render at
* all. 404 rather than 403 — a user may not learn that the area exists
* (Bauprinzip A.5.3).
*/
export function settingsHubNotFound(): Response {
return new Response("Not Found", { status: 404 });
}
/** Redirect a hub base URL to the first tab the current user can actually open. */
export function settingsHubBaseRedirect(request: Request, baseHref: string, tabs: ReadonlyArray<{ href: string }>): Response | null {
const url = new URL(request.url);
const normalize = (value: string) => value.replace(/\/+$/, "") || "/";
if (normalize(url.pathname) !== normalize(baseHref)) return null;
const firstHref = tabs[0]?.href;
if (!firstHref || normalize(firstHref) === normalize(baseHref)) return null;
return new Response(null, {
status: 302,
headers: { Location: `${firstHref}${url.search}${url.hash}` },
});
}