add deleted method
This commit is contained in:
parent
9bfd25a908
commit
ecc6e7642f
@ -3,17 +3,18 @@ from ..dependencies import database
|
|||||||
from passlib.context import CryptContext
|
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)
|
user_repository = users.UserRepository(database=database.database)
|
||||||
|
|
||||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||||
result = user_repository.find_one_by({'username': username})
|
result = user_repository.find_one_by({'username': username})
|
||||||
change = "added"
|
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:
|
if result is not None:
|
||||||
result.password=pwd_context.hash(password)
|
result.password=pwd_context.hash(password)
|
||||||
result.roles=roles
|
result.roles=roles
|
||||||
result.disabled=disabled
|
result.disabled=disabled
|
||||||
|
result.confirmed=confirmed
|
||||||
user = result
|
user = result
|
||||||
change = "updated"
|
change = "updated"
|
||||||
user_repository.save(user)
|
user_repository.save(user)
|
||||||
|
@ -6,13 +6,17 @@ class User(BaseModel):
|
|||||||
username: str
|
username: str
|
||||||
password: str
|
password: str
|
||||||
roles: str
|
roles: str
|
||||||
disabled: bool
|
disabled: bool = False
|
||||||
|
removed: bool = False
|
||||||
|
confirmed: bool = False
|
||||||
|
|
||||||
class UserOut(BaseModel):
|
class UserOut(BaseModel):
|
||||||
id: ObjectIdField = None
|
id: ObjectIdField = None
|
||||||
username: str
|
username: str
|
||||||
roles: str
|
roles: str
|
||||||
disabled: bool
|
disabled: bool
|
||||||
|
removed: bool
|
||||||
|
confirmed: bool
|
||||||
|
|
||||||
class UserInDB(User):
|
class UserInDB(User):
|
||||||
password: str
|
password: str
|
||||||
|
@ -19,7 +19,7 @@ async def read_users(authorize: Annotated[bool, Depends(permissions_checker.Perm
|
|||||||
listUsers = []
|
listUsers = []
|
||||||
user_repository = users.UserRepository(database=database.database)
|
user_repository = users.UserRepository(database=database.database)
|
||||||
for user_index in user_repository.find_by({}, limit=limit, skip=skip):
|
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)
|
listUsers.append(user)
|
||||||
return listUsers
|
return listUsers
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ async def read_users_id(authorize: Annotated[bool, Depends(permissions_checker.P
|
|||||||
listUsers = []
|
listUsers = []
|
||||||
user_repository = users.UserRepository(database=database.database)
|
user_repository = users.UserRepository(database=database.database)
|
||||||
for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip):
|
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)
|
listUsers.append(user)
|
||||||
return listUsers
|
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_repository = users.UserRepository(database=database.database)
|
||||||
user = user_repository.find_one_by_id(ObjectId(item_id))
|
user = user_repository.find_one_by_id(ObjectId(item_id))
|
||||||
return user
|
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user