From ecc6e7642f216af33f0ba29ae9975a7a0adfe680 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sat, 14 Oct 2023 18:20:08 +0200 Subject: [PATCH] add deleted method --- app/dependencies/user_add.py | 5 +++-- app/models/users.py | 6 +++++- app/routers/users.py | 25 +++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app/dependencies/user_add.py b/app/dependencies/user_add.py index 198c10f..74cc7f4 100644 --- a/app/dependencies/user_add.py +++ b/app/dependencies/user_add.py @@ -3,17 +3,18 @@ from ..dependencies import database from passlib.context import CryptContext -def add(username="", password="", roles="User", disabled=False): +def add(username="", password="", roles="User", disabled=False, confirmed=True): user_repository = users.UserRepository(database=database.database) pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") result = user_repository.find_one_by({'username': username}) change = "added" - user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled) + user = users.User(username=username, password=pwd_context.hash(password), roles=roles, disabled=disabled, confirmed=confirmed) if result is not None: result.password=pwd_context.hash(password) result.roles=roles result.disabled=disabled + result.confirmed=confirmed user = result change = "updated" user_repository.save(user) diff --git a/app/models/users.py b/app/models/users.py index 743a924..01dd92b 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -6,13 +6,17 @@ class User(BaseModel): username: str password: str roles: str - disabled: bool + disabled: bool = False + removed: bool = False + confirmed: bool = False class UserOut(BaseModel): id: ObjectIdField = None username: str roles: str disabled: bool + removed: bool + confirmed: bool class UserInDB(User): password: str diff --git a/app/routers/users.py b/app/routers/users.py index 702dbe5..d7947ae 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -19,7 +19,7 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm listUsers = [] user_repository = users.UserRepository(database=database.database) for user_index in user_repository.find_by({}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles) + user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles, removed=user_index.removed, confirmed=user_index.confirmed) listUsers.append(user) return listUsers @@ -39,7 +39,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P listUsers = [] user_repository = users.UserRepository(database=database.database) for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip): - user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles) + user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles, removed=user_index.removed, confirmed=user_index.confirmed) listUsers.append(user) return listUsers @@ -53,3 +53,24 @@ async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permis user_repository = users.UserRepository(database=database.database) user = user_repository.find_one_by_id(ObjectId(item_id)) return user + + + +@router.delete("/users/me",tags=["users"]) +async def read_users_me(current_user: Annotated[users.User, Depends(users_token.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], remove: bool = False): + user_repository = users.UserRepository(database=database.database) + current_user.disabled = True + if remove is True: + current_user.removed = True + user_repository.update(current_user) + return current_user + +@router.delete("/users/{item_id}", tags=["users"], response_model=users.User) +async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False): + user_repository = users.UserRepository(database=database.database) + user = user_repository.find_one_by_id(ObjectId(item_id)) + user.disabled = True + if remove is True: + user.removed = True + user_repository.update(user) + return user