2026-04-06 12:41:49 +01:00
|
|
|
"""
|
2026-05-28 11:10:55 +01:00
|
|
|
Auth dependencies
|
2026-04-06 12:41:49 +01:00
|
|
|
|
2026-05-28 11:10:55 +01:00
|
|
|
Exports:
|
|
|
|
|
- org_query_user_claims_dependency: bool: Verifies user belongs to org
|
|
|
|
|
- org_model_root_claim_query_dependency: org_model: verifies org exists and user is either root or su, gets org from query
|
|
|
|
|
- org_model_root_claim_body_dependency: org_model: verifies org exists and user is either root or su, gets org from body
|
|
|
|
|
- super_admin_dependency: user_model: verifies the user is a super admin
|
2026-05-27 14:27:51 +01:00
|
|
|
"""
|
2026-05-27 15:22:32 +01:00
|
|
|
from typing import Annotated
|
2026-05-27 14:58:10 +01:00
|
|
|
from fastapi import Depends
|
2026-05-27 14:27:51 +01:00
|
|
|
|
|
|
|
|
from src.user.dependencies import user_model_claims_dependency
|
2026-05-28 11:06:54 +01:00
|
|
|
from src.user.models import User
|
2026-05-27 15:34:18 +01:00
|
|
|
from src.organisation.dependencies import org_model_query_dependency, org_model_body_dependency
|
2026-05-27 15:22:32 +01:00
|
|
|
from src.organisation.models import Organisation as Org
|
2026-05-27 14:27:51 +01:00
|
|
|
|
2026-05-27 14:58:10 +01:00
|
|
|
from src.auth.exceptions import UnauthorizedException
|
|
|
|
|
|
2026-05-27 14:27:51 +01:00
|
|
|
|
2026-05-28 10:56:08 +01:00
|
|
|
def is_super_admin(user_model) -> bool:
|
|
|
|
|
super_admin_emails = ["chris@sr2.uk"]
|
|
|
|
|
if user_model.email not in super_admin_emails:
|
|
|
|
|
raise UnauthorizedException()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2026-05-27 14:27:51 +01:00
|
|
|
async def org_query_user_claims(org_model: org_model_query_dependency, user_model: user_model_claims_dependency):
|
|
|
|
|
if user_model in org_model.user_rel:
|
|
|
|
|
return True
|
|
|
|
|
|
2026-05-27 14:58:10 +01:00
|
|
|
raise UnauthorizedException()
|
2026-05-27 14:27:51 +01:00
|
|
|
|
|
|
|
|
|
2026-05-27 15:22:32 +01:00
|
|
|
org_query_user_claims_dependency = Annotated[bool, Depends(org_query_user_claims)]
|
2026-05-27 14:27:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def org_query_root_claims(user_model: user_model_claims_dependency, org_model: org_model_query_dependency):
|
|
|
|
|
if org_model.root_user_id == user_model.id:
|
2026-05-27 15:22:32 +01:00
|
|
|
return org_model
|
2026-05-27 14:27:51 +01:00
|
|
|
|
2026-05-28 10:56:08 +01:00
|
|
|
if is_super_admin(user_model):
|
|
|
|
|
return org_model
|
|
|
|
|
|
2026-05-27 14:58:10 +01:00
|
|
|
raise UnauthorizedException()
|
2026-05-27 14:27:51 +01:00
|
|
|
|
|
|
|
|
|
2026-05-27 15:22:32 +01:00
|
|
|
org_model_root_claim_query_dependency = Annotated[type[Org], Depends(org_query_root_claims)]
|
2026-05-27 14:27:51 +01:00
|
|
|
|
|
|
|
|
|
2026-05-27 15:34:18 +01:00
|
|
|
async def org_body_root_claims(user_model: user_model_claims_dependency, org_model: org_model_body_dependency):
|
|
|
|
|
if org_model.root_user_id == user_model.id:
|
|
|
|
|
return org_model
|
|
|
|
|
|
2026-05-28 10:56:08 +01:00
|
|
|
if is_super_admin(user_model):
|
|
|
|
|
return org_model
|
|
|
|
|
|
2026-05-27 15:34:18 +01:00
|
|
|
raise UnauthorizedException()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
org_model_root_claim_body_dependency = Annotated[type[Org], Depends(org_body_root_claims)]
|
|
|
|
|
|
|
|
|
|
|
2026-05-28 10:56:08 +01:00
|
|
|
async def user_model_super_admin(user_model: user_model_claims_dependency):
|
|
|
|
|
if is_super_admin(user_model):
|
|
|
|
|
return user_model
|
|
|
|
|
|
|
|
|
|
raise UnauthorizedException()
|
2026-05-27 14:27:51 +01:00
|
|
|
|
|
|
|
|
|
2026-05-28 11:06:54 +01:00
|
|
|
super_admin_dependency = Annotated[type[User], Depends(user_model_super_admin)]
|