From d3ed11fce4cb6ac55055f2d898e90763eda143af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Tue, 29 Mar 2022 00:10:24 +0000 Subject: [PATCH] use get_user_by_name --- api/kiwi_vpn_api/routers/user.py | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/api/kiwi_vpn_api/routers/user.py b/api/kiwi_vpn_api/routers/user.py index c54b51c..18222dd 100644 --- a/api/kiwi_vpn_api/routers/user.py +++ b/api/kiwi_vpn_api/routers/user.py @@ -8,7 +8,8 @@ from pydantic import BaseModel from ..config import Config from ..db import User, UserCapabilityType, UserCreate, UserRead -from ._common import Responses, get_current_user, get_current_user_if_admin +from ._common import (Responses, get_current_user, get_current_user_if_admin, + get_user_by_name) router = APIRouter(prefix="/user", tags=["user"]) @@ -112,20 +113,13 @@ async def add_user( response_model=User, ) async def remove_user( - user_name: str, _: User = Depends(get_current_user_if_admin), + user: User = Depends(get_user_by_name), ): """ DELETE ./{user_name}: Remove a user from the database. """ - # get the user - user = User.get(user_name) - - # fail if user not found - if user is None: - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND) - # delete user user.delete() @@ -140,17 +134,14 @@ async def remove_user( }, ) async def extend_capabilities( - user_name: str, capabilities: list[UserCapabilityType], _: User = Depends(get_current_user_if_admin), + user: User = Depends(get_user_by_name), ): """ POST ./{user_name}/capabilities: Add capabilities to a user. """ - # get and change the user - user = User.get(user_name) - user.set_capabilities(user.get_capabilities() | set(capabilities)) user.update() @@ -166,17 +157,14 @@ async def extend_capabilities( }, ) async def remove_capabilities( - user_name: str, capabilities: list[UserCapabilityType], _: User = Depends(get_current_user_if_admin), + user: User = Depends(get_user_by_name), ): """ DELETE ./{user_name}/capabilities: Remove capabilities from a user. """ - # get and change the user - user = User.get(user_name) - user.set_capabilities(user.get_capabilities() - set(capabilities)) user.update()