backend-collector/app/dependencies/permissions_checker.py

19 lines
551 B
Python

from ..dependencies import users_active
from fastapi import Depends, HTTPException, status
from ..models import users
class PermissionChecker:
def __init__(self, roles: list[str]) -> None:
self.roles = roles
def __call__(self, user: users.User = Depends(users_active.get_current_active_user)) -> bool:
for role in self.roles:
if role == user.roles:
return True
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='Roles unauthorized'
)