Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add role field to JWTUser schema and implement authorization for retrieving all users #82

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions backend/app/api/routes/authentication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Dict

from backend.app.api.dependencies.authentication import get_current_user_authorizer
from fastapi import APIRouter, Body, Depends, HTTPException
from starlette.status import HTTP_201_CREATED, HTTP_400_BAD_REQUEST

Expand All @@ -21,14 +22,20 @@
from app.services import jwt
from app.services.authentication import check_email_is_taken, check_username_is_taken
from app.services.event import send_event
from fastapi import Security
from fastapi.security import OAuth2PasswordBearer


router = APIRouter()

@router.get("/", response_model=Dict[str, List[User]], name="users:get-all-users")
async def retrieve_all_users(
users_repo: UsersRepository = Depends(get_repository(UsersRepository)),
user: User = Depends(get_current_user_authorizer()),
) -> Dict[str, List[User]]:
if user.role != UserRole.ADMIN:
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Not enough permissions")

users = await users_repo.get_all_users()
return {"users": users}

Expand Down
Loading