From e12c1c91b3f5411cb3d38f7b4aa835012e77fac4 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Sun, 22 Oct 2023 18:58:35 +0200 Subject: [PATCH] change status --- app/dependencies/user_add.py | 7 +++---- app/dependencies/users_token.py | 2 +- app/models/users.py | 8 ++------ app/routers/users.py | 18 +++++++++--------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/dependencies/user_add.py b/app/dependencies/user_add.py index fae97c5..a9f5a70 100644 --- a/app/dependencies/user_add.py +++ b/app/dependencies/user_add.py @@ -4,18 +4,17 @@ from passlib.context import CryptContext from pydantic import EmailStr -def add(username="", password="", roles="User", disabled=False, confirmed=True, email="test@toto.com"): +def add(username="", password="", roles="User", status=1, email="test@toto.com"): 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, confirmed=confirmed, email=email) + user = users.User(username=username, password=pwd_context.hash(password), status=status, email=email) if result is not None: result.password=pwd_context.hash(password) result.roles=roles - result.disabled=disabled - result.confirmed=confirmed + result.status=status result.email=email user = result change = "updated" diff --git a/app/dependencies/users_token.py b/app/dependencies/users_token.py index 8ef3032..b34ea00 100644 --- a/app/dependencies/users_token.py +++ b/app/dependencies/users_token.py @@ -70,6 +70,6 @@ async def get_current_user(token_str: Annotated[str, Depends(oauth2_scheme)]): async def get_current_active_user( current_user: Annotated[users.User, Depends(get_current_user)] ): - if current_user.disabled: + if current_user.status == 0: raise HTTPException(status_code=400, detail="Inactive user") return current_user \ No newline at end of file diff --git a/app/models/users.py b/app/models/users.py index 4912304..5e7f358 100644 --- a/app/models/users.py +++ b/app/models/users.py @@ -6,18 +6,14 @@ class User(BaseModel): username: str password: str roles: str = "User" - disabled: bool = False - removed: bool = False - confirmed: bool = False + status: int = 0 email: EmailStr class UserOut(BaseModel): id: ObjectIdField = None username: str roles: str - disabled: bool - removed: bool - confirmed: bool + status: int = 0 email: EmailStr diff --git a/app/routers/users.py b/app/routers/users.py index 30f44be..ae27d57 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, email=user_index.email, disabled=user_index.disabled, roles=user_index.roles, removed=user_index.removed, confirmed=user_index.confirmed) + user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, status=user_index.status, roles=user_index.roles) listUsers.append(user) return listUsers @@ -39,12 +39,12 @@ 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, email=user_index.email, removed=user_index.removed, confirmed=user_index.confirmed) + user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email) listUsers.append(user) return listUsers -@router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "disabled"]) +@router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) 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"]))]): return current_user @@ -63,12 +63,12 @@ async def read_users_id(item_id : str, authorize: Annotated[bool, Depends(permis return user -@router.delete("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "disabled"]) +@router.delete("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) 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 + current_user.status = 0 if remove is True: - current_user.removed = True + current_user.status = -1 user_repository.save(current_user) return current_user @@ -76,13 +76,13 @@ async def read_users_me(current_user: Annotated[users.User, Depends(users_token. 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 + user.status = 0 if remove is True: - user.removed = True + user.status = -1 user_repository.save(user) return user -@router.put("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "disabled"]) +@router.put("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"]) 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"]))], userSingle: users.UserIn | None = None): user_repository = users.UserRepository(database=database.database) current_user.username = userSingle.username