backend-collector/app/dependencies/permissions_checker.py

19 lines
549 B
Python
Raw Permalink Normal View History

2023-10-14 17:34:34 +02:00
from ..dependencies import users_token
2023-10-12 23:54:50 +02:00
from fastapi import Depends, HTTPException, status
from ..models import users
class PermissionChecker:
def __init__(self, roles: list[str]) -> None:
self.roles = roles
2023-10-14 17:34:34 +02:00
def __call__(self, user: users.User = Depends(users_token.get_current_active_user)) -> bool:
2023-10-12 23:54:50 +02:00
for role in self.roles:
if role == user.roles:
return True
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail='Roles unauthorized'
)