|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from enum import StrEnum, auto |
| 4 | +from typing import Annotated, Callable |
| 5 | + |
| 6 | +from fastapi import Depends, HTTPException, status |
| 7 | + |
| 8 | +from diracx.core.properties import GENERIC_PILOT, OPERATOR, PILOT, SERVICE_ADMINISTRATOR |
| 9 | +from diracx.db.os import PilotLogsDB |
| 10 | +from diracx.routers.access_policies import BaseAccessPolicy |
| 11 | + |
| 12 | +from ..utils.users import AuthorizedUserInfo |
| 13 | + |
| 14 | + |
| 15 | +class ActionType(StrEnum): |
| 16 | + #: Create/update pilot log records |
| 17 | + CREATE = auto() |
| 18 | + #: download pilot logs |
| 19 | + READ = auto() |
| 20 | + #: delete pilot logs |
| 21 | + DELETE = auto() |
| 22 | + #: Search |
| 23 | + QUERY = auto() |
| 24 | + |
| 25 | + |
| 26 | +class PilotLogsAccessPolicy(BaseAccessPolicy): |
| 27 | + """ToDo |
| 28 | + ---- |
| 29 | +
|
| 30 | + """ |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + async def policy( |
| 34 | + policy_name: str, |
| 35 | + user_info: AuthorizedUserInfo, |
| 36 | + /, |
| 37 | + *, |
| 38 | + action: ActionType | None = None, |
| 39 | + pilot_db: PilotLogsDB | None = None, |
| 40 | + pilot_ids: list[int] | None = None, # or pilot stamp list ? |
| 41 | + ): |
| 42 | + print("user_info.properties:", user_info.properties) |
| 43 | + assert action, "action is a mandatory parameter" |
| 44 | + assert pilot_db, "pilot_db is a mandatory parameter" |
| 45 | + |
| 46 | + if GENERIC_PILOT in user_info.properties: |
| 47 | + return |
| 48 | + if PILOT in user_info.properties: |
| 49 | + return |
| 50 | + if SERVICE_ADMINISTRATOR in user_info.properties: |
| 51 | + return |
| 52 | + if OPERATOR in user_info.properties: |
| 53 | + return |
| 54 | + |
| 55 | + raise HTTPException(status.HTTP_403_FORBIDDEN, detail=user_info.properties) |
| 56 | + |
| 57 | + |
| 58 | +CheckPilotLogsPolicyCallable = Annotated[Callable, Depends(PilotLogsAccessPolicy.check)] |
0 commit comments