From 08896b917ca2bb86121d0121320685b72204345e Mon Sep 17 00:00:00 2001 From: Guillaume Tucker Date: Fri, 10 Nov 2023 13:24:06 +0100 Subject: [PATCH] api.main: rely on create_user_manager() method Use create_user_manager() to get a UserManager object rather than constructing it directly in the API handlers. This removes dependencies in api.main, avoids duplication and simplifies the implementation. Signed-off-by: Guillaume Tucker --- api/main.py | 19 +++++++++---------- api/user_manager.py | 9 +++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/api/main.py b/api/main.py index 239179b6..3faa63cf 100644 --- a/api/main.py +++ b/api/main.py @@ -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 @@ -39,14 +38,13 @@ ) from .paginator_models import PageModel from .pubsub import PubSub, Subscription -from .user_manager import get_user_manager +from .user_manager import get_user_manager, create_user_manager from .user_models import ( User, UserRead, UserCreate, UserUpdate, ) -from .user_manager import UserManager # List of all the supported API versions. This is a placeholder until the API @@ -64,6 +62,7 @@ get_user_manager, [auth_backend], ) +user_manager = create_user_manager() @app.on_event('startup') @@ -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, 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: @@ -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, user_manager) @app.patch("/user/{user_id}", response_model=UserRead, tags=["user"], @@ -281,7 +280,8 @@ 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, 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: @@ -334,8 +334,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 user_manager.authenticate(credentials) if user is None or not user.is_active: raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, @@ -344,8 +343,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, user_manager + ) # ----------------------------------------------------------------------------- diff --git a/api/user_manager.py b/api/user_manager.py index b4d53a63..39347b82 100644 --- a/api/user_manager.py +++ b/api/user_manager.py @@ -140,11 +140,16 @@ async def authenticate( return user +def create_user_manager(): + """Create a UserManager object""" + return UserManager(BeanieUserDatabase(User)) + + async def get_user_db(): - """Database adapter for fastapi-users""" + """Get a generator with the database adapter for fastapi-users""" yield BeanieUserDatabase(User) async def get_user_manager(user_db: BeanieUserDatabase = Depends(get_user_db)): - """Get user manager""" + """Get a generator with the user manager""" yield UserManager(user_db)