Align RBAC API contract

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Jelaletdin12andCopilot App committed 2026-09-28 13:14:11 +05:00
1 parent 8dc61d7786
commit 4c4d037a1e
39 files changed
+410 -1071

No files matched your search

+42 -96
View File
@@ -1,46 +1,19 @@
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). */
/** True when the role is the super role with unrestricted access. */
export function isAdmin(user?: AuthUser | null): boolean {
if (!user?.role) return false;
return user.role.guardName === ADMIN_GUARD || user.role.code === "admin";
return user?.role?.isSuper === true;
}
/** 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";
/** 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 list of permission codes the user holds (empty for admin since it is unrestricted). */
/** Returns the permission codes exposed by GET /profile. */
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;
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. */
@@ -63,18 +36,17 @@ export function hasAnyPermission(user: AuthUser | null | undefined, codes: reado
// ─── Permission constants used to gate nav items / pages ──────────────────────
export const PERM = {
DASHBOARD: "dashboard",
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__",
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];
@@ -84,10 +56,7 @@ type PermValue = (typeof PERM)[keyof typeof PERM];
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]],
["/dashboard", [PERM.DASHBOARD]],
// Loan department
["/loan-orders", [PERM.LOANS]],
["/loan-order-mobiles", [PERM.LOANS]],
@@ -99,20 +68,28 @@ const ROUTE_RULES: RouteRule[] = [
["/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]],
// 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_VIEW, PERM.USERS_MANAGE]],
["/all-users", [PERM.USERS_VIEW, PERM.USERS_MANAGE]],
["/clients", [PERM.USERS_VIEW, PERM.USERS_MANAGE]],
["/operators", [PERM.USERS]],
["/all-users", [PERM.USERS]],
["/clients", [PERM.USERS]],
// Currencies
["/currency-rates", [PERM.CURRENCIES_VIEW]],
["/visa-master-sber-settings", [PERM.CURRENCIES_VIEW]],
["/currency-rates", [PERM.CURRENCIES]],
["/visa-master-sber-settings", [PERM.CURRENCIES]],
// System pages
["/backups", [PERM.BACKUPS_VIEW]],
["/logs", [PERM.ACTIVITY_LOGS_VIEW]],
["/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). */
@@ -138,57 +115,26 @@ export function permissionForUrl(url: string | undefined): readonly PermValue[]
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"],
[[PERM.USERS], "/operators"],
[[PERM.CURRENCIES], "/currency-rates"],
[[PERM.BACKUPS], "/backups"],
[[PERM.ACTIVITY_LOGS], "/logs"],
];
/** Returns the landing page for the user: admin → dashboard, web → orders, else the first page they may open. */
/** 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";
if (isWebUser(user)) return "/loan-orders";
for (const [codes, path] of HOME_PRIORITY) {
if (hasAnyPermission(user, codes)) return path;
}
if (hasPermission(user, PERM.DASHBOARD)) return "/dashboard";
return "/profile";
}