235 lines
11 KiB
Python
Raw Normal View History

2023-10-27 23:50:35 +02:00
from fastapi import APIRouter, Depends, HTTPException, status, Response
2023-10-21 10:10:02 +02:00
from fastapi.responses import JSONResponse
2023-10-25 23:42:34 +02:00
from datetime import datetime
2023-10-15 16:17:37 +02:00
from ..dependencies import users_token, permissions_checker, database
from ..models import users
2023-10-30 23:37:16 +01:00
from pydantic import EmailStr
2023-10-12 00:14:50 +02:00
from typing import Annotated
2023-10-14 15:48:45 +02:00
from bson import ObjectId
2023-10-10 22:13:47 +02:00
router = APIRouter()
2023-10-14 17:41:34 +02:00
2023-10-13 22:35:04 +02:00
@router.get("/users", tags=["users"], response_model=list[users.UserOut])
2023-10-30 23:37:16 +01:00
async def read_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], skip: int = 0, limit: int = 20, id_user: str | None = None, roles: str | None = None, status: int | None = None, email: EmailStr | None = None):
2023-10-14 15:16:38 +02:00
if limit < 1 or skip < 0 or limit < skip:
2023-10-14 11:53:31 +02:00
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
2023-10-14 15:16:38 +02:00
detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip"
2023-10-14 11:53:31 +02:00
)
limit = limit + skip
2023-10-13 14:59:57 +02:00
listUsers = []
2023-10-13 22:55:52 +02:00
user_repository = users.UserRepository(database=database.database)
2023-10-30 23:37:16 +01:00
object_search = {}
if status is not None and roles is not None:
object_search = {"$and":[{"roles":{"$eq": roles}}, {"status":{"$eq":status}}]}
else:
if status is not None:
object_search = {"status":{"$eq": status}}
if roles is not None:
object_search = {"roles":{"$eq":roles}}
if id_user is not None:
userid = ObjectId(id_user)
object_search = {"id": {"$regex": userid}}
if status is not None and roles is not None:
object_search = {"$and":[{"id":{"$regex": userid}}, {"roles":{"$eq": roles}}, {"status":{"$eq":status}}]}
else:
if status is not None:
object_search = {"$and":[{"id":{"$regex": userid}}, {"status":{"$eq":status}}]}
if roles is not None:
object_search = {"$and":[{"id":{"$regex": userid}}, {"roles":{"$eq":roles}}]}
if email is not None:
object_search = {"email": {"$eq": email}}
if status is not None and roles is not None:
object_search = {"$and":[{"email":{"$eq": email}}, {"roles":{"$eq": roles}}, {"status":{"$eq":status}}]}
else:
if status is not None:
object_search = {"$and":[{"email":{"$eq": email}}, {"status":{"$eq":status}}]}
if roles is not None:
object_search = {"$and":[{"email":{"$eq": email}}, {"roles":{"$eq":roles}}]}
for user_index in user_repository.find_by(object_search, limit=limit, skip=skip):
2023-10-25 23:42:34 +02:00
user = users.UserOut(id=user_index.id, username=user_index.username, email=user_index.email, status=user_index.status, roles=user_index.roles, firstName=user_index.firstName, name=user_index.name)
2023-10-13 14:59:57 +02:00
listUsers.append(user)
return listUsers
2023-10-12 00:14:50 +02:00
2023-10-14 15:48:45 +02:00
@router.get("/users/search", tags=["users"], response_model=list[users.UserOut])
2023-10-30 23:37:16 +01:00
async def read_users_search(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))], skip: int = 0, limit: int = 20):
2023-10-14 15:48:45 +02:00
if limit < 1 or skip < 0 or limit < skip:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="skip should be greater than 0 and limit should be greater than 1. Limit should be greater than skip"
)
if key is None or value is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Key or/and value parameter is empty"
)
limit = limit + skip
listUsers = []
user_repository = users.UserRepository(database=database.database)
for user_index in user_repository.find_by({key: {'$regex': value}}, limit=limit, skip=skip):
2023-10-25 23:42:34 +02:00
user = users.UserOut(id=user_index.id, username=user_index.username, status=user_index.status, roles=user_index.roles, email=user_index.email, firstName=user_index.firstName, name=user_index.name)
2023-10-14 15:48:45 +02:00
listUsers.append(user)
return listUsers
2023-10-22 18:58:35 +02:00
@router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"])
2023-10-14 17:34:34 +02:00
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"]))]):
2023-10-14 15:48:45 +02:00
return current_user
2023-10-21 10:10:02 +02:00
@router.get("/users/count", tags=["users"])
async def read_users_count(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]):
count = database.database.get_collection("users").estimated_document_count()
content = {"count":count}
response = JSONResponse(content=content)
return response
2023-10-14 15:48:45 +02:00
@router.get("/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"]))]):
user_repository = users.UserRepository(database=database.database)
user = user_repository.find_one_by_id(ObjectId(item_id))
return user
2023-10-14 18:20:08 +02:00
2023-10-22 18:58:35 +02:00
@router.delete("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"])
2023-10-26 00:08:14 +02:00
async def delete_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):
2023-10-14 18:20:08 +02:00
user_repository = users.UserRepository(database=database.database)
2023-10-22 18:58:35 +02:00
current_user.status = 0
2023-10-14 18:20:08 +02:00
if remove is True:
2023-10-22 18:58:35 +02:00
current_user.status = -1
2023-10-14 18:29:07 +02:00
user_repository.save(current_user)
2023-10-14 18:20:08 +02:00
return current_user
2023-10-31 17:44:24 +01:00
@router.delete("/users/groups",tags=["users"])
2023-11-02 23:54:40 +01:00
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:
2023-10-31 17:44:24 +01:00
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="userids should be greater than 0"
)
user_repository = users.UserRepository(database=database.database)
2023-11-02 23:54:40 +01:00
content = {"message": "users are disabled"}
for i in userids.ids:
2023-10-31 17:44:24 +01:00
user = user_repository.find_one_by_id(ObjectId(i))
user.status = 0
if remove is True:
user.status = -1
2023-11-02 23:54:40 +01:00
content = {"message": "users are deleted "}
user_repository.save(user)
2023-10-31 17:44:24 +01:00
2023-10-31 17:47:44 +01:00
2023-10-31 17:44:24 +01:00
response = JSONResponse(content=content)
return response
2023-10-14 18:20:08 +02:00
@router.delete("/users/{item_id}", tags=["users"], response_model=users.User)
2023-10-26 00:08:14 +02:00
async def delete_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], remove : bool = False):
2023-10-14 18:20:08 +02:00
user_repository = users.UserRepository(database=database.database)
user = user_repository.find_one_by_id(ObjectId(item_id))
2023-10-22 18:58:35 +02:00
user.status = 0
2023-10-14 18:20:08 +02:00
if remove is True:
2023-10-22 18:58:35 +02:00
user.status = -1
2023-10-14 18:29:07 +02:00
user_repository.save(user)
2023-10-14 18:20:08 +02:00
return user
2023-10-14 21:28:21 +02:00
2023-10-22 18:58:35 +02:00
@router.put("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["id", "password", "roles", "status"])
2023-10-26 00:08:14 +02:00
async def update_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):
2023-10-14 21:28:21 +02:00
user_repository = users.UserRepository(database=database.database)
current_user.username = userSingle.username
2023-10-14 22:14:29 +02:00
current_user.password = user_token.get_password_hash(userSingle.password)
2023-10-14 21:28:21 +02:00
current_user.roles = userSingle.roles
2023-10-14 23:04:19 +02:00
current_user.email = userSingle.email
2023-10-14 21:28:21 +02:00
user_repository.save(current_user)
return current_user
2023-11-03 21:36:08 +01:00
@router.put("/users", tags=["users"], response_model=users.User, status_code=status.HTTP_201_CREATED)
async def update_users(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response):
2023-10-14 21:28:21 +02:00
if userSingle is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Body request is empty"
2023-10-14 22:14:29 +02:00
)
user_repository = users.UserRepository(database=database.database)
2023-11-03 21:00:47 +01:00
user = user_repository.find_one_by({"$or":[{"username": {'$eq': userSingle.username}}, {"email": {"$eq": userSingle.email}}]})
2023-11-03 21:36:08 +01:00
if user is not None:
2023-11-03 15:22:59 +01:00
response.status_code = status.HTTP_201_CREATED
2023-11-03 21:36:08 +01:00
if user.username == userSingle.username:
raise HTTPException(
status_code=status.HTTP_204_NO_CONTENT,
detail="username"
)
if user.email == userSingle.email:
raise HTTPException(
status_code=status.HTTP_204_NO_CONTENT,
detail="email"
)
2023-10-14 21:28:21 +02:00
user.username = userSingle.username
2023-10-25 23:42:34 +02:00
user.password = users_token.get_password_hash(userSingle.password)
2023-10-14 21:28:21 +02:00
user.roles = userSingle.roles
2023-10-14 23:04:19 +02:00
user.email = userSingle.email
2023-10-25 23:42:34 +02:00
user.firstName = userSingle.firstName
user.name = userSingle.name
user.birth = userSingle.birth
user.updated_at = datetime.today()
2023-10-14 21:28:21 +02:00
user_repository.save(user)
2023-10-26 00:08:14 +02:00
return user
2023-11-02 17:30:15 +01:00
2023-11-03 21:36:08 +01:00
@router.put("/users/{item_id}", tags=["users"], response_model=users.User, status_code=status.HTTP_200_OK)
async def update_users_id(item_id: str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userSingle: users.UserIn | None = None, response: Response = Response):
if userSingle is None:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Body request is empty"
)
user_repository = users.UserRepository(database=database.database)
user = user_repository.find_one_by({"id": {'$eq': ObjectId(item_id)}})
if user is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="User not found"
)
user.username = userSingle.username
user.password = users_token.get_password_hash(userSingle.password)
user.roles = userSingle.roles
user.email = userSingle.email
user.firstName = userSingle.firstName
user.name = userSingle.name
user.birth = userSingle.birth
user.updated_at = datetime.today()
user_repository.save(user)
return user
2023-10-31 18:02:24 +01:00
@router.patch("/users/groups",tags=["users"])
2023-11-02 23:54:40 +01:00
async def patch_users_groups(authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))], userids: users.UserIDS | None = None):
if len(userids.ids) == 0:
2023-10-31 18:02:24 +01:00
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="userids should be greater than 0"
)
user_repository = users.UserRepository(database=database.database)
2023-11-02 23:54:40 +01:00
content = {"message": "users are enabled"}
for i in userids.ids:
2023-10-31 18:02:24 +01:00
user = user_repository.find_one_by_id(ObjectId(i))
user.status = 1
2023-11-02 23:54:40 +01:00
user_repository.save(user)
2023-10-31 18:02:24 +01:00
response = JSONResponse(content=content)
2023-11-02 17:30:15 +01:00
return response
@router.patch("/users/{item_id}", tags=["users"], response_model=users.User)
async def patch_users_id(item_id : str, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]):
user_repository = users.UserRepository(database=database.database)
user = user_repository.find_one_by_id(ObjectId(item_id))
user.status = 1
user_repository.save(user)
return user