File size: 1,746 Bytes
2756f0f
 
 
 
 
7a3215f
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a3215f
 
 
2756f0f
 
 
7a3215f
2756f0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Request dependencies shared by all controllers."""

from typing import Annotated
from uuid import UUID

from fastapi import Depends, Header, Request
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.database import get_db
from app.core.errors import Forbidden, Unauthenticated
from app.core.pagination import PageParams, page_params
from app.core.security import decode_access_token
from app.integrations.storage import StorageClient, get_storage
from app.modules.accounts.models import Account

Db = Annotated[AsyncSession, Depends(get_db)]
Storage = Annotated[StorageClient, Depends(get_storage)]
Paging = Annotated[PageParams, Depends(page_params)]


def current_account_id(authorization: Annotated[str | None, Header()] = None) -> UUID:
    if not authorization or not authorization.lower().startswith("bearer "):
        raise Unauthenticated()
    return decode_access_token(authorization[7:].strip())


async def current_account(
    db: Db, account_id: Annotated[UUID, Depends(current_account_id)], request: Request,
) -> Account:
    account = await db.get(Account, account_id)
    if account is None:
        raise Unauthenticated()
    request.state.audit_account_id = account.id
    return account


CurrentAccount = Annotated[Account, Depends(current_account)]


async def require_coach(account: CurrentAccount) -> Account:
    if account.role != "coach":
        raise Forbidden("Only coaches can do this.")
    return account


async def require_player(account: CurrentAccount) -> Account:
    if account.role != "player":
        raise Forbidden("Only players can do this.")
    return account


CurrentCoach = Annotated[Account, Depends(require_coach)]
CurrentPlayer = Annotated[Account, Depends(require_player)]