permissions.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from uuid import UUID
  2. from fastapi import HTTPException
  3. from flowsint_core.core.models import InvestigationUserRole
  4. from flowsint_core.core.types import Role
  5. def can_user(roles: list[Role], actions: list[str]) -> bool:
  6. """
  7. Vérifie si au moins un rôle de la liste autorise au moins une action de la liste.
  8. """
  9. for role in roles:
  10. for action in actions:
  11. if role == Role.OWNER:
  12. return True
  13. if role == Role.EDITOR and action in ["read", "create", "update"]:
  14. return True
  15. if role == Role.VIEWER and action == "read":
  16. return True
  17. return False
  18. from fastapi import HTTPException
  19. def check_investigation_permission(
  20. user_id: UUID, investigation_id: str, actions: list[str], db
  21. ):
  22. role_entry = (
  23. db.query(InvestigationUserRole)
  24. .filter_by(user_id=user_id, investigation_id=investigation_id)
  25. .first()
  26. )
  27. if not role_entry or not can_user(role_entry.roles, actions):
  28. raise HTTPException(status_code=403, detail="Forbidden")
  29. return True