Skip to content

Commit 55d1b94

Browse files
authored
Sport competition module (#621)
### Description Please explain the changes you made here. ### Checklist - [ ] Created tests which fail without the change (if possible) - [ ] All tests passing - [ ] Extended the documentation, if necessary
1 parent eb5d1c8 commit 55d1b94

37 files changed

+13005
-195
lines changed

app/core/groups/groups_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class GroupType(str, Enum):
2626
eclair = "1f841bd9-00be-41a7-96e1-860a18a46105"
2727
BDS = "61af3e52-7ef9-4608-823a-39d51e83d1db"
2828
seed_library = "09153d2a-14f4-49a4-be57-5d0f265261b9"
29+
competition_admin = "2b1fc736-1288-4043-b293-14bc23adae68"
2930

3031
# Auth related groups
3132

app/core/payment/types_payment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class HelloAssoConfigName(Enum):
3939
CDR = "CDR"
4040
RAID = "RAID"
4141
MYECLPAY = "MYECLPAY"
42+
CHALLENGER = "CHALLENGER"
4243

4344

4445
class HelloAssoConfig(BaseModel):

app/core/schools/factory_schools.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from uuid import uuid4
1+
from uuid import UUID, uuid4
22

33
from sqlalchemy.ext.asyncio import AsyncSession
44

@@ -11,11 +11,13 @@
1111
class CoreSchoolsFactory(Factory):
1212
depends_on = []
1313

14+
school_id: UUID = uuid4()
15+
1416
@classmethod
1517
async def run(cls, db: AsyncSession, settings: Settings) -> None:
1618
await cruds_schools.create_school(
1719
CoreSchool(
18-
id=uuid4(),
20+
id=cls.school_id,
1921
name="ENS",
2022
email_regex=r"^[a-zA-Z0-9_.+-]+@ens\.fr$",
2123
),

app/dependencies.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def get_users(db: AsyncSession = Depends(get_db)):
2525
from app.core.groups.groups_type import AccountType, GroupType, get_ecl_account_types
2626
from app.core.payment.payment_tool import PaymentTool
2727
from app.core.payment.types_payment import HelloAssoConfigName
28-
from app.core.users import models_users
28+
from app.core.users import cruds_users, models_users
2929
from app.core.utils import security
3030
from app.core.utils.config import Settings, construct_prod_settings
3131
from app.modules.raid.utils.drive.drive_file_manager import DriveFileManager
@@ -322,10 +322,42 @@ def get_token_data(
322322
)
323323

324324

325+
def get_user_id_from_token_with_scopes(
326+
scopes: list[list[ScopeType]],
327+
) -> Callable[
328+
[schemas_auth.TokenData],
329+
Coroutine[Any, Any, str],
330+
]:
331+
"""
332+
Generate a dependency which will:
333+
* check the request header contain a valid JWT token
334+
* make sure the token contain the given scopes
335+
* return the corresponding user_id of the token
336+
337+
This endpoint allows to require scopes other than the API scope. This should only be used by the auth endpoints.
338+
To restrict an endpoint from the API, use `is_user_in`.
339+
"""
340+
341+
async def get_current_user_id(
342+
token_data: schemas_auth.TokenData = Depends(get_token_data),
343+
) -> str:
344+
"""
345+
Dependency that makes sure the token is valid, contains the expected scopes and returns the corresponding user_id.
346+
The expected scopes are passed as list of list of scopes, each list of scopes is an "AND" condition, and the list of list of scopes is an "OR" condition.
347+
"""
348+
349+
return await auth_utils.get_user_id_from_token_with_scopes(
350+
scopes=scopes,
351+
token_data=token_data,
352+
)
353+
354+
return get_current_user_id
355+
356+
325357
def get_user_from_token_with_scopes(
326358
scopes: list[list[ScopeType]],
327359
) -> Callable[
328-
[AsyncSession, schemas_auth.TokenData],
360+
[AsyncSession, str],
329361
Coroutine[Any, Any, models_users.CoreUser],
330362
]:
331363
"""
@@ -338,22 +370,20 @@ def get_user_from_token_with_scopes(
338370
To restrict an endpoint from the API, use `is_user_in`.
339371
"""
340372

341-
async def get_current_user(
373+
async def get_user_from_user_id(
342374
db: AsyncSession = Depends(get_db),
343-
token_data: schemas_auth.TokenData = Depends(get_token_data),
375+
user_id: str = Depends(get_user_id_from_token_with_scopes(scopes)),
344376
) -> models_users.CoreUser:
345377
"""
346378
Dependency that makes sure the token is valid, contains the expected scopes and returns the corresponding user.
347379
The expected scopes are passed as list of list of scopes, each list of scopes is an "AND" condition, and the list of list of scopes is an "OR" condition.
348380
"""
381+
user = await cruds_users.get_user_by_id(db=db, user_id=user_id)
382+
if not user:
383+
raise HTTPException(status_code=404, detail="User not found")
384+
return user
349385

350-
return await auth_utils.get_user_from_token_with_scopes(
351-
scopes=scopes,
352-
db=db,
353-
token_data=token_data,
354-
)
355-
356-
return get_current_user
386+
return get_user_from_user_id
357387

358388

359389
def is_user(

0 commit comments

Comments
 (0)