Skip to content

Commit

Permalink
api.main: rely on create_user_manager() method
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
gctucker committed Nov 16, 2023
1 parent 73c29be commit 08896b9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
19 changes: 9 additions & 10 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 All @@ -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
Expand All @@ -64,6 +62,7 @@
get_user_manager,
[auth_backend],
)
user_manager = create_user_manager()


@app.on_event('startup')
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, 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, user_manager)


@app.patch("/user/{user_id}", response_model=UserRead, tags=["user"],
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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
)


# -----------------------------------------------------------------------------
Expand Down
9 changes: 7 additions & 2 deletions api/user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 08896b9

Please sign in to comment.