Refinamiento de permisos y ajustes en controles. Añade gestión sobre saldos y visualización. Entre otros..
This commit is contained in:
@@ -6,6 +6,7 @@ import HomePage from '../pages/HomePage';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
import MainLayout from '../layouts/MainLayout';
|
||||
import { Typography } from '@mui/material';
|
||||
import SectionProtectedRoute from './SectionProtectedRoute';
|
||||
|
||||
// Distribución
|
||||
import DistribucionIndexPage from '../pages/Distribucion/DistribucionIndexPage';
|
||||
@@ -38,6 +39,7 @@ import ContablesIndexPage from '../pages/Contables/ContablesIndexPage';
|
||||
import GestionarTiposPagoPage from '../pages/Contables/GestionarTiposPagoPage';
|
||||
import GestionarPagosDistribuidorPage from '../pages/Contables/GestionarPagosDistribuidorPage';
|
||||
import GestionarNotasCDPage from '../pages/Contables/GestionarNotasCDPage';
|
||||
import GestionarSaldosPage from '../pages/Contables/GestionarSaldosPage';
|
||||
|
||||
// Usuarios
|
||||
import UsuariosIndexPage from '../pages/Usuarios/UsuariosIndexPage'; // Crear este componente
|
||||
@@ -69,10 +71,14 @@ import ReporteComparativaConsumoBobinasPage from '../pages/Reportes/ReporteCompa
|
||||
import ReporteCuentasDistribuidoresPage from '../pages/Reportes/ReporteCuentasDistribuidoresPage';
|
||||
import ReporteListadoDistribucionPage from '../pages/Reportes/ReporteListadoDistribucionPage';
|
||||
import ReporteControlDevolucionesPage from '../pages/Reportes/ReporteControlDevolucionesPage';
|
||||
import GestionarNovedadesCanillaPage from '../pages/Distribucion/GestionarNovedadesCanillaPage';
|
||||
import ReporteNovedadesCanillasPage from '../pages/Reportes/ReporteNovedadesCanillasPage';
|
||||
import ReporteListadoDistMensualPage from '../pages/Reportes/ReporteListadoDistMensualPage';
|
||||
|
||||
// Auditorias
|
||||
import GestionarAuditoriaUsuariosPage from '../pages/Usuarios/Auditoria/GestionarAuditoriaUsuariosPage';
|
||||
|
||||
|
||||
// --- ProtectedRoute y PublicRoute SIN CAMBIOS ---
|
||||
const ProtectedRoute: React.FC<{ children: JSX.Element }> = ({ children }) => {
|
||||
const { isAuthenticated, isLoading } = useAuth();
|
||||
@@ -107,7 +113,7 @@ const MainLayoutWrapper: React.FC = () => (
|
||||
const AppRoutes = () => {
|
||||
return (
|
||||
<BrowserRouter>
|
||||
<Routes> {/* Un solo <Routes> de nivel superior */}
|
||||
<Routes>
|
||||
<Route path="/login" element={<PublicRoute><LoginPage /></PublicRoute>} />
|
||||
|
||||
{/* Rutas Protegidas que usan el MainLayout */}
|
||||
@@ -123,13 +129,21 @@ const AppRoutes = () => {
|
||||
<Route index element={<HomePage />} /> {/* Para la ruta exacta "/" */}
|
||||
|
||||
{/* Módulo de Distribución (anidado) */}
|
||||
<Route path="distribucion" element={<DistribucionIndexPage />}>
|
||||
<Route
|
||||
path="distribucion"
|
||||
element={
|
||||
<SectionProtectedRoute requiredPermission="SS001" sectionName="Distribución">
|
||||
<DistribucionIndexPage />
|
||||
</SectionProtectedRoute>
|
||||
}
|
||||
>
|
||||
<Route index element={<Navigate to="es-canillas" replace />} />
|
||||
<Route path="es-canillas" element={<GestionarEntradasSalidasCanillaPage />} />
|
||||
<Route path="control-devoluciones" element={<GestionarControlDevolucionesPage />} />
|
||||
<Route path="es-distribuidores" element={<GestionarEntradasSalidasDistPage />} />
|
||||
<Route path="salidas-otros-destinos" element={<GestionarSalidasOtrosDestinosPage />} />
|
||||
<Route path="canillas" element={<GestionarCanillitasPage />} />
|
||||
<Route path="canillas/:idCanilla/novedades" element={<GestionarNovedadesCanillaPage />} />
|
||||
<Route path="distribuidores" element={<GestionarDistribuidoresPage />} />
|
||||
<Route path="otros-destinos" element={<GestionarOtrosDestinosPage />} />
|
||||
<Route path="zonas" element={<GestionarZonasPage />} />
|
||||
@@ -146,15 +160,28 @@ const AppRoutes = () => {
|
||||
</Route>
|
||||
|
||||
{/* Módulo Contable (anidado) */}
|
||||
<Route path="contables" element={<ContablesIndexPage />}>
|
||||
<Route
|
||||
path="contables"
|
||||
element={
|
||||
<SectionProtectedRoute requiredPermission="SS002" sectionName="Contables">
|
||||
<ContablesIndexPage />
|
||||
</SectionProtectedRoute>
|
||||
}
|
||||
>
|
||||
<Route index element={<Navigate to="tipos-pago" replace />} />
|
||||
<Route path="tipos-pago" element={<GestionarTiposPagoPage />} />
|
||||
<Route path="pagos-distribuidores" element={<GestionarPagosDistribuidorPage />} />
|
||||
<Route path="notas-cd" element={<GestionarNotasCDPage />} />
|
||||
<Route path="gestion-saldos" element={<GestionarSaldosPage />} />
|
||||
</Route>
|
||||
|
||||
{/* Módulo de Impresión (anidado) */}
|
||||
<Route path="impresion" element={<ImpresionIndexPage />}>
|
||||
<Route path="impresion"
|
||||
element={
|
||||
<SectionProtectedRoute requiredPermission="SS003" sectionName="Impresión">
|
||||
<ImpresionIndexPage />
|
||||
</SectionProtectedRoute>}
|
||||
>
|
||||
<Route index element={<Navigate to="plantas" replace />} />
|
||||
<Route path="plantas" element={<GestionarPlantasPage />} />
|
||||
<Route path="tipos-bobina" element={<GestionarTiposBobinaPage />} />
|
||||
@@ -164,8 +191,13 @@ const AppRoutes = () => {
|
||||
</Route>
|
||||
|
||||
{/* Módulo de Reportes */}
|
||||
<Route path="reportes" element={<ReportesIndexPage />}> {/* Página principal del módulo */}
|
||||
<Route index element={<Typography sx={{p:2}}>Seleccione un reporte del menú lateral.</Typography>} /> {/* Placeholder */}
|
||||
<Route path="reportes"
|
||||
element={
|
||||
<SectionProtectedRoute requiredPermission="SS004" sectionName="Reportes">
|
||||
<ReportesIndexPage />
|
||||
</SectionProtectedRoute>}
|
||||
>
|
||||
<Route index element={<Typography sx={{ p: 2 }}>Seleccione un reporte del menú lateral.</Typography>} /> {/* Placeholder */}
|
||||
<Route path="existencia-papel" element={<ReporteExistenciaPapelPage />} />
|
||||
<Route path="movimiento-bobinas" element={<ReporteMovimientoBobinasPage />} />
|
||||
<Route path="movimiento-bobinas-estado" element={<ReporteMovimientoBobinasEstadoPage />} />
|
||||
@@ -181,10 +213,17 @@ const AppRoutes = () => {
|
||||
<Route path="cuentas-distribuidores" element={<ReporteCuentasDistribuidoresPage />} />
|
||||
<Route path="listado-distribucion-distribuidores" element={<ReporteListadoDistribucionPage />} />
|
||||
<Route path="control-devoluciones" element={<ReporteControlDevolucionesPage />} />
|
||||
<Route path="novedades-canillas" element={<ReporteNovedadesCanillasPage />} />
|
||||
<Route path="listado-distribucion-mensual" element={<ReporteListadoDistMensualPage />} />
|
||||
</Route>
|
||||
|
||||
{/* Módulo de Radios (anidado) */}
|
||||
<Route path="radios" element={<RadiosIndexPage />}>
|
||||
<Route path="radios"
|
||||
element={
|
||||
<SectionProtectedRoute requiredPermission="SS005" sectionName="Radios">
|
||||
<RadiosIndexPage />
|
||||
</SectionProtectedRoute>}
|
||||
>
|
||||
<Route index element={<Navigate to="ritmos" replace />} />
|
||||
<Route path="ritmos" element={<GestionarRitmosPage />} />
|
||||
<Route path="canciones" element={<GestionarCancionesPage />} />
|
||||
@@ -192,7 +231,12 @@ const AppRoutes = () => {
|
||||
</Route>
|
||||
|
||||
{/* Módulo de Usuarios (anidado) */}
|
||||
<Route path="usuarios" element={<UsuariosIndexPage />}>
|
||||
<Route path="usuarios"
|
||||
element={
|
||||
<SectionProtectedRoute requiredPermission="SS006" sectionName="Usuarios">
|
||||
<UsuariosIndexPage />
|
||||
</SectionProtectedRoute>}
|
||||
>
|
||||
<Route index element={<Navigate to="perfiles" replace />} /> {/* Redirigir a la primera subpestaña */}
|
||||
<Route path="perfiles" element={<GestionarPerfilesPage />} />
|
||||
<Route path="permisos" element={<GestionarPermisosPage />} />
|
||||
|
||||
52
Frontend/src/routes/SectionProtectedRoute.tsx
Normal file
52
Frontend/src/routes/SectionProtectedRoute.tsx
Normal file
@@ -0,0 +1,52 @@
|
||||
// src/routes/SectionProtectedRoute.tsx
|
||||
import React from 'react';
|
||||
import { Navigate, Outlet } from 'react-router-dom';
|
||||
import { useAuth } from '../contexts/AuthContext';
|
||||
import { usePermissions } from '../hooks/usePermissions';
|
||||
import { Box, CircularProgress } from '@mui/material';
|
||||
|
||||
interface SectionProtectedRouteProps {
|
||||
requiredPermission: string;
|
||||
sectionName: string;
|
||||
children?: React.ReactNode;
|
||||
}
|
||||
|
||||
const SectionProtectedRoute: React.FC<SectionProtectedRouteProps> = ({ requiredPermission, sectionName, children }) => {
|
||||
const { isAuthenticated, isLoading: authIsLoading } = useAuth(); // isLoading de AuthContext
|
||||
const { tienePermiso, isSuperAdmin, currentUser } = usePermissions();
|
||||
|
||||
if (authIsLoading) { // Esperar a que el AuthContext termine su carga inicial
|
||||
return (
|
||||
<Box sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', height: '80vh' }}>
|
||||
<CircularProgress />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
|
||||
// En este punto, si está autenticado, currentUser debería estar disponible.
|
||||
// Si currentUser pudiera ser null aun estando autenticado (poco probable con tu AuthContext),
|
||||
// se necesitaría un manejo adicional o un spinner aquí.
|
||||
if (!currentUser) {
|
||||
// Esto sería un estado inesperado si isAuthenticated es true.
|
||||
// Podrías redirigir a login o mostrar un error genérico.
|
||||
console.error("SectionProtectedRoute: Usuario autenticado pero currentUser es null.");
|
||||
return <Navigate to="/login" replace />; // O un error más específico
|
||||
}
|
||||
|
||||
const canAccessSection = isSuperAdmin || tienePermiso(requiredPermission);
|
||||
|
||||
if (!canAccessSection) {
|
||||
console.error('SectionProtectedRoute: Usuario autenticado pero sin acceso a sección ', sectionName);
|
||||
return <Navigate to="/" replace />;
|
||||
}
|
||||
|
||||
// Si children se proporciona (como <SectionProtectedRoute><IndexPage/></SectionProtectedRoute>), renderiza children.
|
||||
// Si no (como <Route element={<SectionProtectedRoute ... />} > <Route .../> </Route>), renderiza Outlet.
|
||||
return children ? <>{children}</> : <Outlet />;
|
||||
};
|
||||
|
||||
export default SectionProtectedRoute;
|
||||
Reference in New Issue
Block a user