""" Router endpoints for user module Endpoints: - [get]/self/claims - Retrieves user's OIDC claims - [get]/self/db - Retrieves the user data from the db that corresponds to the current OIDC user - [get]/self/orgs - Retrieves all organisations associated with the current user - [get]/self/orgs/admin - Retrieves only admin organisations for the current user - [get]/{user_id} - Retrieves a specific user by their ID - [get]/{user_id}/orgs - Retrieves all organisations associated with a specific user - [get]/{user_id}/orgs/admin - Retrieves only admin organisations for a specific user - [delete]/{user_id} - Deletes a user from the db by their db ID """ from fastapi import APIRouter from starlette import status from src.user.schemas import UserResponse, OIDCClaims, UserDeleteUserRequest from src.user.dependencies import user_model_claims_dependency, user_model_query_dependency, user_model_body_dependency from src.auth.service import claims_dependency from src.database import db_dependency router = APIRouter( prefix="/user", tags=["User"], ) @router.get("/self/claims", response_model=OIDCClaims, status_code=status.HTTP_200_OK, responses={ status.HTTP_200_OK: {"description": "Successful retrieval from database"}, }) async def current_user_claims(user: claims_dependency): """ Returns the full OIDC claims associated with the currently logged-in user. """ user["allowed_origins"] = user.get("allowed-origins", []) return user @router.get("/self/db", response_model=UserResponse, status_code=status.HTTP_200_OK, responses={ status.HTTP_404_NOT_FOUND: {"description": "User not found"}, status.HTTP_200_OK: {"description": "Successful retrieval from database"}, }) async def current_user(user_model: user_model_claims_dependency): """ Returns the database details associated with the currently logged-in user. """ return user_model @router.get("/", response_model=UserResponse, status_code=status.HTTP_200_OK, responses={ status.HTTP_404_NOT_FOUND: {"description": "User not found"}, status.HTTP_200_OK: {"description": "Successful retrieval from database"}, }) async def get_user_by_id(user_model: user_model_query_dependency): """ Returns the database details associated with the provided user ID. """ return user_model @router.delete("/", status_code=status.HTTP_204_NO_CONTENT, responses={ status.HTTP_204_NO_CONTENT: {"description": "User deleted"}, status.HTTP_404_NOT_FOUND: {"description": "User not found"}, }) async def delete_user_by_id(db: db_dependency, user_model: user_model_body_dependency, request_model: UserDeleteUserRequest): """ Deletes the user with the provided ID from the database. This will not remove them from OIDC, and they will be automatically readded on next login. """ db.delete(user_model) db.commit()