use get_user_by_name

This commit is contained in:
Jörn-Michael Miehe 2022-03-29 00:10:24 +00:00
parent 186ac0eab3
commit d3ed11fce4

View file

@ -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()