""" Role-based access control (RBAC) decorators and utilities. """ from typing import List from fastapi import Depends, HTTPException, status from models.user import User, UserRole from core.deps import get_current_user def require_roles(allowed_roles: List[UserRole]): """ Dependency generator that checks if the current user has one of the allowed roles. """ def role_checker(current_user: User = Depends(get_current_user)) -> User: # Convert user's role and allowed_roles to strings/values for safe comparison user_role = str(current_user.role) if current_user.role else "" allowed_values = [str(r.value if hasattr(r, 'value') else r) for r in allowed_roles] if user_role not in allowed_values: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="You do not have permission to perform this action", ) return current_user return role_checker # Pre-defined role groups for convenience ALL_CUSTOMER_ROLES = [UserRole.CUSTOMER_ADMIN, UserRole.CUSTOMER_USER] ALL_SC_ROLES = [UserRole.SC_AGENT, UserRole.SC_LEAD, UserRole.SC_ADMIN] # WORKSPACE_ADMIN_ROLES: Includes local Customer Admins (for user/project management) WORKSPACE_ADMIN_ROLES = [UserRole.CUSTOMER_ADMIN, UserRole.SC_ADMIN, UserRole.SC_LEAD] # INTERNAL_ADMIN_ROLES: Strictly 3SC Staff only (for forensics, AI traces, etc) INTERNAL_ADMIN_ROLES = [UserRole.SC_ADMIN, UserRole.SC_LEAD] EVERYBODY_ROLES = ALL_CUSTOMER_ROLES + ALL_SC_ROLES