add enable and remove groups

This commit is contained in:
Valentin CZERYBA 2023-11-02 23:54:40 +01:00
parent a279439e72
commit 753f4ee087
2 changed files with 14 additions and 11 deletions

View File

@ -46,6 +46,9 @@ class UserCreate(BaseModel):
class UserInDB(User):
password: str
class UserIDS(BaseModel):
ids: list[str]
class UserRepository(AbstractRepository[User]):
class Meta:
collection_name = "users"

View File

@ -104,22 +104,22 @@ async def delete_users_me(current_user: Annotated[users.User, Depends(users_toke
return current_user
@router.delete("/users/groups",tags=["users"])
async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, userids: list[str] = []):
if len(userids) == 0:
async def delete_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove: bool = False, userids: users.UserIDS | None = None):
if len(userids.ids) == 0:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="userids should be greater than 0"
)
user_repository = users.UserRepository(database=database.database)
content = {"roles":user.roles,"message": "users are disabled"}
for i in userids:
content = {"message": "users are disabled"}
for i in userids.ids:
user = user_repository.find_one_by_id(ObjectId(i))
user.status = 0
if remove is True:
user.status = -1
content = {"roles":user.roles,"message": "users are deleted "}
user_repository.save(current_user)
content = {"message": "users are deleted "}
user_repository.save(user)
response = JSONResponse(content=content)
@ -172,19 +172,19 @@ async def update_users_id(authorize: Annotated[bool, Depends(permissions_checker
@router.patch("/users/groups",tags=["users"])
async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userids: list[str] = []):
if len(userids) == 0:
async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userids: users.UserIDS | None = None):
if len(userids.ids) == 0:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="userids should be greater than 0"
)
user_repository = users.UserRepository(database=database.database)
content = {"roles":user.roles,"message": "users are enabled"}
for i in userids:
content = {"message": "users are enabled"}
for i in userids.ids:
user = user_repository.find_one_by_id(ObjectId(i))
user.status = 1
user_repository.save(current_user)
user_repository.save(user)
response = JSONResponse(content=content)