195 lines
7.5 KiB
TypeScript
195 lines
7.5 KiB
TypeScript
import type { AuthUser, AuthUserPermission } from "@/app/store/authStore";
|
|
|
|
/**
|
|
* Access model:
|
|
* - "admin" guard has full access to everything regardless of its permission list.
|
|
* - "web" guard (public user role) can open the Orders section (loan / card / international payments).
|
|
* - Any other guard (operator, ...) is limited to the permission codes it holds.
|
|
*/
|
|
|
|
// ─── Guard names ──────────────────────────────────────────────────────────────
|
|
|
|
export const ADMIN_GUARD = "admin";
|
|
export const OPERATOR_GUARD = "operator";
|
|
export const WEB_GUARD = "web";
|
|
|
|
/** True when the user's role is the admin guard (full access). */
|
|
export function isAdmin(user?: AuthUser | null): boolean {
|
|
if (!user?.role) return false;
|
|
return user.role.guardName === ADMIN_GUARD || user.role.code === "admin";
|
|
}
|
|
|
|
/** True when the user's role is the web guard (the public "user" role). */
|
|
export function isWebUser(user?: AuthUser | null): boolean {
|
|
if (!user?.role) return false;
|
|
return user.role.guardName === WEB_GUARD || user.role.code === "user";
|
|
}
|
|
|
|
/** Returns the list of permission codes the user holds (empty for admin since it is unrestricted). */
|
|
export function getPermissionCodes(user?: AuthUser | null): string[] {
|
|
if (isAdmin(user)) return [];
|
|
const seen = new Set<string>();
|
|
const codes: string[] = [];
|
|
const push = (p?: Pick<AuthUserPermission, "code"> | { code?: string }) => {
|
|
if (p?.code && !seen.has(p.code)) {
|
|
seen.add(p.code);
|
|
codes.push(p.code);
|
|
}
|
|
};
|
|
for (const p of user?.permissions ?? []) push(p);
|
|
for (const m of user?.modules ?? []) {
|
|
for (const p of m.permissions ?? []) push(p);
|
|
}
|
|
return codes;
|
|
}
|
|
|
|
/** 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 = {
|
|
LOANS: "loans",
|
|
CARDS: "cards",
|
|
USERS_VIEW: "users.view",
|
|
USERS_MANAGE: "users.manage",
|
|
ROLES_VIEW: "roles.view",
|
|
BRANCHES_VIEW: "branches.view",
|
|
PROVINCES_VIEW: "provinces.view",
|
|
BACKUPS_VIEW: "backups.view",
|
|
ACTIVITY_LOGS_VIEW: "activity-logs.view",
|
|
CURRENCIES_VIEW: "currencies.view",
|
|
/** Sentinel for pages that only the admin guard may access (no permission code exists). */
|
|
ADMIN_ONLY: "__admin_only__",
|
|
} 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[] = [
|
|
// System / admin-only
|
|
["/dashboard", [PERM.ADMIN_ONLY]],
|
|
// Settings (admin-only)
|
|
["/settings", [PERM.ADMIN_ONLY]],
|
|
// 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]],
|
|
["/card-transactions", [PERM.CARDS]],
|
|
["/card-requisites", [PERM.CARDS]],
|
|
["/card-balances", [PERM.CARDS]],
|
|
["/card-pins", [PERM.CARDS]],
|
|
// International payments + online payment history (no permission yet → admin-only)
|
|
["/visa-master", [PERM.ADMIN_ONLY]],
|
|
["/sber-payments", [PERM.ADMIN_ONLY]],
|
|
["/online-payments-history", [PERM.ADMIN_ONLY]],
|
|
// Users (operators / clients / all users)
|
|
["/operators", [PERM.USERS_VIEW, PERM.USERS_MANAGE]],
|
|
["/all-users", [PERM.USERS_VIEW, PERM.USERS_MANAGE]],
|
|
["/clients", [PERM.USERS_VIEW, PERM.USERS_MANAGE]],
|
|
// Currencies
|
|
["/currency-rates", [PERM.CURRENCIES_VIEW]],
|
|
["/visa-master-sber-settings", [PERM.CURRENCIES_VIEW]],
|
|
// System pages
|
|
["/backups", [PERM.BACKUPS_VIEW]],
|
|
["/logs", [PERM.ACTIVITY_LOGS_VIEW]],
|
|
];
|
|
|
|
/** 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;
|
|
if (isWebUser(user) && isWebAccessiblePath(url)) return true;
|
|
return hasAnyPermission(user, permissionForUrl(url));
|
|
}
|
|
|
|
// ─── Web (public user) section ────────────────────────────────────────────────
|
|
|
|
/**
|
|
* Paths that the "web" guard (public user role) may open regardless of its
|
|
* permission list. Covers the whole Orders section: loan department,
|
|
* card department and international payments.
|
|
*/
|
|
const WEB_ACCESS_PREFIXES: readonly string[] = [
|
|
// Loan department
|
|
"/loan-orders",
|
|
"/loan-order-mobiles",
|
|
"/loan-remaining",
|
|
"/loan-paid-off-letters",
|
|
// Card department
|
|
"/order-new-card",
|
|
"/card-transactions",
|
|
"/card-requisites",
|
|
"/card-balances",
|
|
"/card-pins",
|
|
// International payments
|
|
"/visa-master",
|
|
"/sber-payments",
|
|
];
|
|
|
|
/** True when the path is inside the Orders section available to the web guard. */
|
|
function isWebAccessiblePath(pathname: string): boolean {
|
|
const clean = pathname.split("?")[0];
|
|
return WEB_ACCESS_PREFIXES.some((prefix) => clean === prefix || clean.startsWith(`${prefix}/`));
|
|
}
|
|
|
|
// ─── Home path ────────────────────────────────────────────────────────────────
|
|
|
|
const HOME_PRIORITY: readonly (readonly [codes: readonly PermValue[], path: string])[] = [
|
|
[[PERM.LOANS], "/loan-orders"],
|
|
[[PERM.CARDS], "/order-new-card"],
|
|
[[PERM.USERS_VIEW, PERM.USERS_MANAGE], "/operators"],
|
|
[[PERM.CURRENCIES_VIEW], "/currency-rates"],
|
|
[[PERM.BACKUPS_VIEW], "/backups"],
|
|
[[PERM.ACTIVITY_LOGS_VIEW], "/logs"],
|
|
];
|
|
|
|
/** Returns the landing page for the user: admin → dashboard, web → orders, else the first page they may open. */
|
|
export function getDefaultHomePath(user: AuthUser | null | undefined): string {
|
|
if (isAdmin(user)) return "/dashboard";
|
|
if (isWebUser(user)) return "/loan-orders";
|
|
for (const [codes, path] of HOME_PRIORITY) {
|
|
if (hasAnyPermission(user, codes)) return path;
|
|
}
|
|
return "/profile";
|
|
}
|