CognxSafeTrack commited on
Commit ·
803f4c5
1
Parent(s): 1ef1cda
feat: implement RoleGuard for centralized authorization
Browse files- Created reusable RoleGuard component
- Refactored MainLayout to use RoleGuard for admin features
- Reduced manual role checks in the UI layout
Files changed (2)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152import React from 'react';import { useAuth } from '@/lib/auth';
type Role = 'STUDENT' | 'ADMIN' | 'ORG_MEMBER' | 'ORG_ADMIN' | 'SUPER_ADMIN';
interface RoleGuardProps { children: React.ReactNode; allowedRoles?: Role[]; requireSuperAdmin?: boolean; fallback?: React.ReactNode;}
/** * RoleGuard component to conditionally render content based on user roles. * * Usage: * <RoleGuard allowedRoles={['SUPER_ADMIN', 'ADMIN']}> * <SecretComponent /> * </RoleGuard> */export const RoleGuard: React.FC<RoleGuardProps> = ({ children, allowedRoles, requireSuperAdmin = false, fallback = null }) => { const { user } = useAuth();
if (!user) return <>{fallback}</>;
const userRole = user.role as Role;
// Special case for Super Admin logic if (requireSuperAdmin) { if (userRole === 'SUPER_ADMIN' || userRole === 'ADMIN') { return <>{children}</>; } return <>{fallback}</>; }
// Default role check if (allowedRoles && allowedRoles.length > 0) { if (allowedRoles.includes(userRole)) { return <>{children}</>; } return <>{fallback}</>; }
return <>{children}</>;};
export default RoleGuard;3 unmodified lines45678936 unmodified lines464748495051527 unmodified lines606162636465663 unmodified linesimport { useTenant } from '@/lib/tenant';import { BarChart2, TrendingUp, Users, BookOpen, Mic, Building2, Activity, Lightbulb } from 'lucide-react';interface MainLayoutProps {children: React.ReactNode;isSuperAdmin: boolean;36 unmodified lines<span className="truncate">{currentOrg?.name || 'EdTech Admin'}</span></div>{isSuperAdmin && (<div className="mb-8"><label className="block text-[10px] uppercase font-bold text-slate-500 tracking-wider mb-2">Gestion Multi-Tenant</label><select7 unmodified lines))}</select></div>)}<nav className="space-y-1 flex-1">{navItems.map(n => (3 unmodified lines456789101136 unmodified lines484950515253547 unmodified lines626364656667683 unmodified linesimport { useTenant } from '@/lib/tenant';import { BarChart2, TrendingUp, Users, BookOpen, Mic, Building2, Activity, Lightbulb } from 'lucide-react';import RoleGuard from '@/components/RoleGuard';interface MainLayoutProps {children: React.ReactNode;isSuperAdmin: boolean;36 unmodified lines<span className="truncate">{currentOrg?.name || 'EdTech Admin'}</span></div><RoleGuard requireSuperAdmin><div className="mb-8"><label className="block text-[10px] uppercase font-bold text-slate-500 tracking-wider mb-2">Gestion Multi-Tenant</label><select7 unmodified lines))}</select></div></RoleGuard><nav className="space-y-1 flex-1">{navItems.map(n => (
apps/admin/src/components/RoleGuard.tsxAdded+52
apps/admin/src/components/layouts/MainLayout.tsx+4-2