142 lines
5.3 KiB
TypeScript
142 lines
5.3 KiB
TypeScript
import type { AuthUser, AuthUserPermission } from "@/app/store/authStore";
|
|
|
|
/** True when the role is the super role with unrestricted access. */
|
|
export function isAdmin(user?: AuthUser | null): boolean {
|
|
return user?.role?.isSuper === true;
|
|
}
|
|
|
|
/** True when the role is limited to the user's own records. */
|
|
export function hasOwnScope(user?: AuthUser | null): boolean {
|
|
return user?.role?.scope === "own";
|
|
}
|
|
|
|
/** Returns the permission codes exposed by GET /profile. */
|
|
export function getPermissionCodes(user?: AuthUser | null): string[] {
|
|
if (isAdmin(user)) return [];
|
|
return [...new Set((user?.permissions ?? []).map((permission: AuthUserPermission) => permission.code))];
|
|
}
|
|
|
|
/** Returns true when the user may access the page protected by the given permission code. */
|
|
export function hasPermission(user: AuthUser | null | undefined, code: string): boolean {
|
|
if (isAdmin(user)) return true;
|
|
return getPermissionCodes(user).includes(code);
|
|
}
|
|
|
|
/**
|
|
* True when the user holds at least one of the given permission codes
|
|
* (or is an admin / there are no required codes).
|
|
*/
|
|
export function hasAnyPermission(user: AuthUser | null | undefined, codes: readonly string[]): boolean {
|
|
if (codes.length === 0) return true;
|
|
if (isAdmin(user)) return true;
|
|
const held = new Set(getPermissionCodes(user));
|
|
return codes.some((c) => held.has(c));
|
|
}
|
|
|
|
// ─── Permission constants used to gate nav items / pages ──────────────────────
|
|
|
|
export const PERM = {
|
|
DASHBOARD: "dashboard",
|
|
LOANS: "loans",
|
|
CARDS: "cards",
|
|
USERS: "users",
|
|
ROLES: "roles",
|
|
BRANCHES: "branches",
|
|
PROVINCES: "provinces",
|
|
ACTIVITY_LOGS: "activity-logs",
|
|
BACKUPS: "backups",
|
|
CURRENCIES: "currencies",
|
|
PAYMENT_SETTINGS: "payment-settings",
|
|
} as const;
|
|
|
|
type PermValue = (typeof PERM)[keyof typeof PERM];
|
|
|
|
// ─── Route → required permission(s) ───────────────────────────────────────────
|
|
|
|
type RouteRule = readonly [prefix: string, codes: readonly PermValue[]];
|
|
|
|
const ROUTE_RULES: RouteRule[] = [
|
|
["/dashboard", [PERM.DASHBOARD]],
|
|
// Loan department
|
|
["/loan-orders", [PERM.LOANS]],
|
|
["/loan-order-mobiles", [PERM.LOANS]],
|
|
["/loan-remaining", [PERM.LOANS]],
|
|
["/loan-paid-off-letters", [PERM.LOANS]],
|
|
// Card department + international payments + online payment history
|
|
["/order-new-card", [PERM.CARDS]],
|
|
["/order-new-deposit", [PERM.CARDS]],
|
|
["/card-transactions", [PERM.CARDS]],
|
|
["/card-requisites", [PERM.CARDS]],
|
|
["/card-balances", [PERM.CARDS]],
|
|
["/card-pins", [PERM.CARDS]],
|
|
// Payment operations are covered by the existing cards module permission.
|
|
["/visa-master", [PERM.CARDS]],
|
|
["/sber-payments", [PERM.CARDS]],
|
|
["/online-payments-history", [PERM.CARDS]],
|
|
// Users (operators / clients / all users)
|
|
["/operators", [PERM.USERS]],
|
|
["/all-users", [PERM.USERS]],
|
|
["/clients", [PERM.USERS]],
|
|
// Currencies
|
|
["/currency-rates", [PERM.CURRENCIES]],
|
|
["/visa-master-sber-settings", [PERM.CURRENCIES]],
|
|
// System pages
|
|
["/backups", [PERM.BACKUPS]],
|
|
["/logs", [PERM.ACTIVITY_LOGS]],
|
|
["/settings/users/roles", [PERM.ROLES]],
|
|
["/settings/users/permissions", [PERM.ROLES]],
|
|
["/settings/location/branches", [PERM.BRANCHES]],
|
|
["/settings/location/districts", [PERM.PROVINCES]],
|
|
["/settings/payment", [PERM.PAYMENT_SETTINGS]],
|
|
["/settings/loan", [PERM.LOANS]],
|
|
["/settings/card", [PERM.CARDS]],
|
|
["/settings/language", [PERM.ROLES]],
|
|
];
|
|
|
|
/** Match a path against the rule prefixes (exact or sub-path). */
|
|
function matchRule(pathname: string): readonly PermValue[] {
|
|
const clean = pathname.split("?")[0];
|
|
for (const [prefix, codes] of ROUTE_RULES) {
|
|
if (clean === prefix || clean.startsWith(`${prefix}/`)) return codes;
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/** Permission codes required to access a route path. Empty = unrestricted (any authenticated user). */
|
|
export function getRequiredPermissions(pathname: string): readonly PermValue[] {
|
|
return matchRule(pathname);
|
|
}
|
|
|
|
/** Resolve the permission(s) required to show a sidebar leaf link. */
|
|
export function permissionForUrl(url: string | undefined): readonly PermValue[] {
|
|
return url ? matchRule(url) : [];
|
|
}
|
|
|
|
/** True when the sidebar leaf for `url` is accessible to the user. */
|
|
export function canAccessUrl(user: AuthUser | null | undefined, url: string | undefined): boolean {
|
|
if (!url) return false;
|
|
if (isAdmin(user)) return true;
|
|
return hasAnyPermission(user, permissionForUrl(url));
|
|
}
|
|
|
|
// ─── Home path ────────────────────────────────────────────────────────────────
|
|
|
|
const HOME_PRIORITY: readonly (readonly [codes: readonly PermValue[], path: string])[] = [
|
|
[[PERM.LOANS], "/loan-orders"],
|
|
[[PERM.CARDS], "/order-new-card"],
|
|
[[PERM.USERS], "/operators"],
|
|
[[PERM.CURRENCIES], "/currency-rates"],
|
|
[[PERM.BACKUPS], "/backups"],
|
|
[[PERM.ACTIVITY_LOGS], "/logs"],
|
|
];
|
|
|
|
/** Returns the landing page for the user based on their role and permissions. */
|
|
export function getDefaultHomePath(user: AuthUser | null | undefined): string {
|
|
if (isAdmin(user)) return "/dashboard";
|
|
for (const [codes, path] of HOME_PRIORITY) {
|
|
if (hasAnyPermission(user, codes)) return path;
|
|
}
|
|
if (hasPermission(user, PERM.DASHBOARD)) return "/dashboard";
|
|
return "/profile";
|
|
}
|