2023-10-12 00:14:50 +02:00
|
|
|
from fastapi import APIRouter, Depends
|
2023-10-13 21:44:30 +02:00
|
|
|
from ..dependencies import users_active, permissions_checker, database
|
2023-10-12 00:04:18 +02:00
|
|
|
from ..models import users
|
2023-10-12 00:14:50 +02:00
|
|
|
from typing import Annotated
|
2023-10-10 22:13:47 +02:00
|
|
|
|
2023-10-11 23:45:12 +02:00
|
|
|
|
2023-10-10 22:13:47 +02:00
|
|
|
router = APIRouter()
|
|
|
|
|
2023-10-13 22:35:04 +02:00
|
|
|
@router.get("/users", tags=["users"], response_model=list[users.UserOut])
|
2023-10-14 11:53:31 +02:00
|
|
|
async def read_users(skip : int = 0, limit : int = 20, authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin"]))]):
|
|
|
|
if limit < 1 or skip < 0:
|
|
|
|
raise HTTPException(
|
|
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
|
detail="skip should be greater than 0 and limit should be greater than 1"
|
|
|
|
)
|
|
|
|
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-14 11:53:31 +02:00
|
|
|
for user_index in user_repository.find_by({}, limit=limit, skip=skip):
|
2023-10-13 23:40:25 +02:00
|
|
|
user = users.UserOut(id=user_index.id, username=user_index.username, disabled=user_index.disabled, roles=user_index.roles)
|
2023-10-13 14:59:57 +02:00
|
|
|
listUsers.append(user)
|
|
|
|
return listUsers
|
2023-10-12 00:14:50 +02:00
|
|
|
|
2023-10-13 14:59:57 +02:00
|
|
|
@router.get("/users/me",tags=["users"], response_model=users.User, response_model_exclude=["password"])
|
2023-10-12 23:54:50 +02:00
|
|
|
async def read_users_me(current_user: Annotated[users.User, Depends(users_active.get_current_active_user)], authorize: Annotated[bool, Depends(permissions_checker.PermissionChecker(roles=["Admin", "User"]))]):
|
2023-10-12 23:15:26 +02:00
|
|
|
return current_user
|