Skip to content

Commit

Permalink
api.main: rely on get_user_manager()
Browse files Browse the repository at this point in the history
Use get_user_manager() to get a UserManager object rather than
constructing it directly in the API handlers.

Signed-off-by: Guillaume Tucker <[email protected]>
  • Loading branch information
gctucker committed Nov 10, 2023
1 parent a5b7a15 commit 9c29602
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from bson import ObjectId, errors
from pymongo.errors import DuplicateKeyError
from fastapi_users import FastAPIUsers
from fastapi_users.db import BeanieUserDatabase
from beanie import PydanticObjectId
from .auth import Authentication
from .db import Database
Expand Down Expand Up @@ -167,7 +166,7 @@ async def register(request: Request, user: UserCreate,
groups.append(group)
user.groups = groups
created_user = await register_router.routes[0].endpoint(
request, user, UserManager(BeanieUserDatabase(User)))
request, user, get_user_manager())
# Update user to be an admin user explicitly if requested as
# `fastapi-users` register route does not allow it
if user.is_superuser:
Expand Down Expand Up @@ -244,7 +243,7 @@ async def update_me(request: Request, user: UserUpdate,
if groups:
user.groups = groups
return await users_router.routes[1].endpoint(
request, user, current_user, UserManager(BeanieUserDatabase(User)))
request, user, current_user, get_user_manager())


@app.patch("/user/{user_id}", response_model=UserRead, tags=["user"],
Expand Down Expand Up @@ -281,7 +280,7 @@ async def update_user(user_id: str, request: Request, user: UserUpdate,
user.groups = groups

updated_user = await users_router.routes[3].endpoint(
user, request, user_from_id, UserManager(BeanieUserDatabase(User)))
user, request, user_from_id, get_user_manager())
# Update user to be an admin user explicitly if requested as
# `fastapi-users` user update route does not allow it
if user.is_superuser:
Expand Down Expand Up @@ -334,8 +333,7 @@ async def update_password(request: Request,
credentials: OAuth2PasswordRequestForm = Depends(),
new_password: str = Form(None)):
"""Update user password"""
user = await UserManager(BeanieUserDatabase(User)).authenticate(
credentials)
user = await get_user_manager().authenticate(credentials)
if user is None or not user.is_active:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
Expand All @@ -344,8 +342,8 @@ async def update_password(request: Request,
user_update = UserUpdate(password=new_password)
user_from_username = await db.find_one(User, username=credentials.username)
await users_router.routes[3].endpoint(
user_update, request, user_from_username,
UserManager(BeanieUserDatabase(User)))
user_update, request, user_from_username, get_user_manager()
)


# -----------------------------------------------------------------------------
Expand Down

0 comments on commit 9c29602

Please sign in to comment.