Spaces:
Paused

File size: 1,548 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
534ec62
 
 
 
 
5eb4857
 
 
 
 
 
 
 
 
 
 
b7980f7
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
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