This commit is contained in:
Shubhankar45
2026-10-27 12:03:31 +00:00
parent 2ed3945b5d
commit 4e5e95775d
104 changed files with 5210 additions and 606 deletions
+7 -5
View File
@@ -16,7 +16,7 @@ from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.auth.security import decode_access_token
from app.common.enums import Role
from app.common.enums import Role, FIELD_ROLES
from app.common.exceptions import ForbiddenError, UnauthorizedError
from app.database.session import get_db
from app.organization.models import AMHeadquarters
@@ -51,7 +51,9 @@ def require_role(*allowed: Role):
"""Layer 1. Usage: `user: User = Depends(require_role(Role.MR))`."""
async def _check(user: User = Depends(get_current_user)) -> User:
if user.role not in allowed:
# Wherever an MR is allowed, an SFE is too.
permitted = set(allowed) | ({Role.SFE} if Role.MR in allowed else set())
if user.role not in permitted:
raise ForbiddenError(
"Role not permitted for this action",
{"your_role": user.role.value, "required": [r.value for r in allowed]},
@@ -76,8 +78,8 @@ async def visible_hq_ids(db: AsyncSession, user: User) -> set[uuid.UUID] | None:
return None
if user.role == Role.AM:
return await active_hq_ids_for_am(db, user.id)
if user.role == Role.MR:
return {user.headquarters_id} if user.headquarters_id else set()
if user.role in FIELD_ROLES:
return user.covered_headquarter_ids
return set()
@@ -96,7 +98,7 @@ async def assert_can_view_dcr(db: AsyncSession, user: User, dcr) -> None:
"""
if user.role in (Role.GM, Role.ADMIN):
return
if user.role == Role.MR and dcr.mr_user_id == user.id:
if user.role in FIELD_ROLES and dcr.mr_user_id == user.id:
return
if user.role == Role.AM:
if dcr.headquarters_id in await active_hq_ids_for_am(db, user.id):
+2
View File
@@ -31,3 +31,5 @@ class CurrentUser(BaseModel):
role: Role
headquarters_id: uuid.UUID | None
is_active: bool
is_superadmin: bool = False
extra_headquarter_ids: list[uuid.UUID] = []