2025-06-06 18:33:09 -03:00
|
|
|
import React from 'react';
|
|
|
|
|
import { Navigate, Outlet } from 'react-router-dom';
|
|
|
|
|
import { useAuth } from '../contexts/AuthContext';
|
|
|
|
|
import { usePermissions } from '../hooks/usePermissions';
|
2025-06-09 19:37:07 -03:00
|
|
|
import { Alert, Box, CircularProgress } from '@mui/material';
|
2025-06-06 18:33:09 -03:00
|
|
|
|
|
|
|
|
interface SectionProtectedRouteProps {
|
2025-06-09 19:37:07 -03:00
|
|
|
requiredPermission?: string | null; // Hacerlo opcional
|
|
|
|
|
onlySuperAdmin?: boolean; // Nueva prop
|
2025-06-06 18:33:09 -03:00
|
|
|
sectionName: string;
|
|
|
|
|
children?: React.ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-09 19:37:07 -03:00
|
|
|
const SectionProtectedRoute: React.FC<SectionProtectedRouteProps> = ({
|
|
|
|
|
requiredPermission,
|
|
|
|
|
onlySuperAdmin = false, // Default a false
|
|
|
|
|
sectionName,
|
|
|
|
|
children
|
|
|
|
|
}) => {
|
|
|
|
|
const { isAuthenticated, isLoading: authIsLoading } = useAuth();
|
2025-06-06 18:33:09 -03:00
|
|
|
const { tienePermiso, isSuperAdmin, currentUser } = usePermissions();
|
|
|
|
|
|
2025-06-09 19:37:07 -03:00
|
|
|
if (authIsLoading) {
|
2025-06-06 18:33:09 -03:00
|
|
|
return (
|
|
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '80vh' }}>
|
|
|
|
|
<CircularProgress />
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isAuthenticated) {
|
|
|
|
|
return <Navigate to="/login" replace />;
|
|
|
|
|
}
|
|
|
|
|
if (!currentUser) {
|
|
|
|
|
console.error("SectionProtectedRoute: Usuario autenticado pero currentUser es null.");
|
2025-06-09 19:37:07 -03:00
|
|
|
return <Navigate to="/login" replace />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let canAccessSection = false;
|
|
|
|
|
if (onlySuperAdmin) {
|
|
|
|
|
canAccessSection = isSuperAdmin;
|
|
|
|
|
} else if (requiredPermission) {
|
|
|
|
|
canAccessSection = isSuperAdmin || tienePermiso(requiredPermission);
|
|
|
|
|
} else {
|
|
|
|
|
// Si no es onlySuperAdmin y no hay requiredPermission, por defecto se permite si está autenticado
|
|
|
|
|
// Esto podría ser para secciones públicas post-login pero sin permiso específico.
|
|
|
|
|
// O podrías querer que siempre haya un requiredPermission o onlySuperAdmin.
|
|
|
|
|
// Por ahora, lo dejaremos pasar si no se especifica ninguno y no es onlySuperAdmin.
|
|
|
|
|
// Sin embargo, para los SSxxx, siempre habrá un requiredPermission.
|
|
|
|
|
// Este else es más un fallback teórico.
|
|
|
|
|
canAccessSection = true;
|
2025-06-06 18:33:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!canAccessSection) {
|
2025-06-09 19:37:07 -03:00
|
|
|
return (
|
|
|
|
|
<Box sx={{p: 3, display:'flex', justifyContent:'center', mt: 2}}>
|
|
|
|
|
<Alert severity="error" sx={{width: '100%', maxWidth: 'md'}}>
|
|
|
|
|
No tiene permiso para acceder a la sección de {sectionName}.
|
|
|
|
|
</Alert>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
2025-06-06 18:33:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return children ? <>{children}</> : <Outlet />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SectionProtectedRoute;
|