ifixkart-admin/lib/rbac.ts

142 lines
4.2 KiB
TypeScript

/**
* Role-Based Access Control (RBAC) definitions and helpers for iFixKart Admin.
*/
export type AppRole =
| 'super admin'
| 'admin'
| 'store manager'
| 'technician'
| 'sales'
| 'customer support'
| string;
export function normalizeRole(role?: string | null): string {
if (!role) return '';
return role.trim().toLowerCase();
}
/**
* Determines if a specific route pathname is authorized for the given user role.
*/
export function isRouteAllowed(pathname: string, userRole?: string | null): boolean {
const role = normalizeRole(userRole);
// Super Admin has unrestricted access to the entire platform
if (role === 'super admin' || role === 'super_admin' || role === 'superadmin') {
return true;
}
// Admin has access to everything except Super Admin exclusive system keys
if (role === 'admin') {
if (
pathname.startsWith('/security') ||
pathname.startsWith('/notifications-gateway')
) {
return false;
}
return true;
}
// 1. Store Manager
if (role === 'store manager') {
if (pathname === '/dashboard' || pathname.startsWith('/dashboard/store-manager') || pathname.startsWith('/products/dashboard')) return true;
if (
pathname.startsWith('/categories') ||
pathname.startsWith('/brands') ||
pathname.startsWith('/series') ||
pathname.startsWith('/models') ||
pathname.startsWith('/products') ||
pathname.startsWith('/attributes') ||
pathname.startsWith('/migration')
) return true;
if (
pathname.startsWith('/inventory') ||
pathname.startsWith('/warehouses') ||
pathname.startsWith('/purchases')
) return true;
if (
pathname.startsWith('/orders') ||
pathname.startsWith('/invoices') ||
pathname.startsWith('/warranties') ||
pathname.startsWith('/refunds') ||
pathname.startsWith('/pos-sessions') ||
pathname.startsWith('/user-pos-analytics') ||
pathname.startsWith('/bulk-quotations') ||
pathname.startsWith('/razorpay-dashboard') ||
pathname.startsWith('/logistics')
) return true;
if (pathname.startsWith('/reports') || pathname.startsWith('/simulation')) return true;
return false;
}
// 2. Technician
if (role === 'technician') {
if (pathname.startsWith('/dashboard/technician')) return true;
if (
pathname.startsWith('/repair-tickets') ||
pathname.startsWith('/service-catalog') ||
pathname.startsWith('/services') ||
pathname.startsWith('/technician') ||
pathname.startsWith('/service-quotes') ||
pathname.startsWith('/service-invoices')
) return true;
return false;
}
// 3. Sales
if (role === 'sales') {
if (pathname === '/dashboard' || pathname.startsWith('/dashboard/b2b')) return true;
if (
pathname.startsWith('/crm-leads') ||
pathname.startsWith('/crm-todos') ||
pathname.startsWith('/crm-calendar') ||
pathname.startsWith('/crm-quotations') ||
pathname.startsWith('/crm-deals') ||
pathname.startsWith('/customers') ||
pathname.startsWith('/whatsapp-web')
) return true;
if (
pathname.startsWith('/orders') ||
pathname.startsWith('/invoices') ||
pathname.startsWith('/bulk-quotations')
) return true;
return false;
}
// 4. Customer Support
if (role === 'customer support') {
if (
pathname.startsWith('/customers') ||
pathname.startsWith('/crm-todos') ||
pathname.startsWith('/reviews-moderation') ||
pathname.startsWith('/services/intake') ||
pathname.startsWith('/repair-tickets')
) return true;
return false;
}
// Fallback for unknown role or no session: deny by default
return false;
}
/**
* Returns the default authorized workspace route for a role upon login.
*/
export function getDefaultDashboardForRole(userRole?: string | null): string {
const role = normalizeRole(userRole);
switch (role) {
case 'technician':
return '/dashboard/technician';
case 'store manager':
return '/dashboard/store-manager';
case 'sales':
return '/crm-leads/new';
case 'customer support':
return '/customers';
case 'admin':
case 'super admin':
default:
return '/dashboard';
}
}