|
1 | | -from enum import Enum |
2 | 1 | from typing import Annotated |
3 | 2 |
|
4 | | -from fastapi import Depends, HTTPException, Request, status |
| 3 | +from fastapi import Cookie, Depends, HTTPException, status |
5 | 4 |
|
6 | 5 | import auth |
7 | 6 | import database |
8 | | -import officers |
9 | | -from officers.constants import OfficerPositionEnum |
10 | | -from permission.types import WEBSITE_ADMIN_POSITIONS |
| 7 | +from utils.permissions import is_user_election_officer, is_user_website_admin |
11 | 8 |
|
12 | 9 |
|
13 | | -# Permissions are granted if the Enum value >= the level needed |
14 | | -class AdminTypeEnum(Enum): |
15 | | - Election = 1 |
16 | | - Full = 2 |
17 | | - |
18 | | - |
19 | | -async def is_user_website_admin(computing_id: str, db_session: database.DBSession) -> bool: |
20 | | - for position in await officers.crud.current_officer_positions(db_session, computing_id): |
21 | | - if position in WEBSITE_ADMIN_POSITIONS: |
22 | | - return True |
| 10 | +async def user(db_session: database.DBSession, session_id: Annotated[str | None, Cookie()] = None) -> str | None: |
| 11 | + if session_id is None: |
| 12 | + return None |
23 | 13 |
|
24 | | - return False |
| 14 | + session_computing_id = await auth.crud.get_computing_id(db_session, session_id) |
25 | 15 |
|
| 16 | + return session_computing_id |
26 | 17 |
|
27 | | -# TODO: Add an election admin version that checks the election attempting to be modified as well |
28 | | -async def is_user_election_officer(computing_id: str, db_session: database.DBSession) -> bool: |
29 | | - """ |
30 | | - An current election officer has access to all election, prior election officers have no access. |
31 | | - """ |
32 | | - officer_terms = await officers.crud.get_current_terms_by_position(db_session, OfficerPositionEnum.ELECTIONS_OFFICER) |
33 | | - for officer in officer_terms: |
34 | | - if computing_id == officer.computing_id: |
35 | | - return True |
36 | 18 |
|
37 | | - return False |
| 19 | +SessionUser = Annotated[str, Depends(user)] |
38 | 20 |
|
39 | 21 |
|
40 | | -async def get_user(request: Request, db_session: database.DBSession) -> tuple[str, str]: |
41 | | - """gets the user's computing_id, or raises an exception if the current request is not logged in""" |
42 | | - session_id = request.cookies.get("session_id", None) |
| 22 | +async def logged_in_user(db_session: database.DBSession, session_id: Annotated[str | None, Cookie()] = None) -> str: |
43 | 23 | if session_id is None: |
44 | 24 | raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="no session id") |
45 | 25 |
|
46 | 26 | session_computing_id = await auth.crud.get_computing_id(db_session, session_id) |
47 | 27 | if session_computing_id is None: |
48 | 28 | raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="no computing id") |
49 | 29 |
|
50 | | - return session_id, session_computing_id |
| 30 | + return session_computing_id |
| 31 | + |
| 32 | + |
| 33 | +LoggedInUser = Annotated[str, Depends(logged_in_user)] |
| 34 | + |
| 35 | + |
| 36 | +async def perm_election(db_session: database.DBSession, computing_id: LoggedInUser) -> str: |
| 37 | + if not is_user_website_admin(computing_id, db_session) or is_user_election_officer(computing_id, db_session): |
| 38 | + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="must be an election admin") |
51 | 39 |
|
| 40 | + return computing_id |
52 | 41 |
|
53 | | -# Allows path functions to use this without having to add a bunch of checks |
54 | | -SessionUser = Annotated[tuple[str, str], Depends(get_user)] |
55 | 42 |
|
| 43 | +ElectionAdmin = Annotated[str, Depends(perm_election)] |
56 | 44 |
|
57 | | -async def get_admin( |
58 | | - db_session: database.DBSession, session_user: SessionUser, admin_type: AdminTypeEnum |
59 | | -) -> tuple[str, str]: |
60 | | - session_id, computing_id = session_user |
61 | | - # Website admins have full permissions |
62 | | - if is_user_website_admin(computing_id, db_session): |
63 | | - return (session_id, computing_id) |
64 | 45 |
|
65 | | - # Election officers have lower permissions |
66 | | - if admin_type == AdminTypeEnum.Election and is_user_election_officer(computing_id, db_session): |
67 | | - return (session_id, computing_id) |
| 46 | +async def perm_admin(db_session: database.DBSession, computing_id: LoggedInUser): |
| 47 | + if not is_user_website_admin(computing_id, db_session): |
| 48 | + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="must be an admin") |
68 | 49 |
|
69 | | - raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="must be an admin") |
| 50 | + return computing_id |
70 | 51 |
|
71 | 52 |
|
72 | | -# Allows path functions to use this without having to add a bunch of checks |
73 | | -SessionAdmin = Annotated[tuple[str, str], Depends(get_admin)] |
| 53 | +SiteAdmin = Annotated[str, Depends(perm_admin)] |
0 commit comments