CoolFace
Apppublic

fmatituy/selectospromanager

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
security.py48 linesDownload Raw Back to core
1from functools import wraps
2from nicegui import ui, app
3from core.logger import logger
4
5class SecurityManager:
6    """
7    Control de acceso empresarial (CAPA 2).
8    Gestiona permisos por roles y scopes.
9    """
10    
11    @staticmethod
12    def check_auth():
13        """Verifica si el usuario está autenticado en la sesión. (BYPASSED)"""
14        return True
15
16    @staticmethod
17    def get_user_role():
18        """Obtiene el rol del usuario actual. (BYPASSED)"""
19        return app.storage.user.get('rol', 'Administrador')
20
21    @staticmethod
22    def require_role(allowed_roles: list):
23        """
24        Decorador para proteger páginas o funciones.
25        Lanza AuthError o redirige si el rol no coincide.
26        """
27        def decorator(func):
28            @wraps(func)
29            def wrapper(*args, **kwargs):
30                if not SecurityManager.check_auth():
31                    ui.navigate.to('/login')
32                    return None
33                
34                rol = SecurityManager.get_user_role()
35                if rol not in allowed_roles and 'Administrador' not in rol:
36                    logger.warning(f"ACCESO DENEGADO: Rol {rol} intentó entrar a {func.__name__}")
37                    ui.notify("No tiene permisos para esta sección.", type='warning')
38                    ui.navigate.to('/')
39                    return None
40                
41                return func(*args, **kwargs)
42            return wrapper
43        return decorator
44
45security = SecurityManager()
46require_admin = SecurityManager.require_role(['Administrador'])
47require_sst = SecurityManager.require_role(['SST', 'Administrador'])
48