add deleted method

This commit is contained in:
Valentin CZERYBA 2023-10-14 18:20:08 +02:00
parent 9bfd25a908
commit ecc6e7642f
3 changed files with 31 additions and 5 deletions

View File

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

View File

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

View File

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