users.py 1.49 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
from fastapi import Response
from fastapi import Depends, FastAPI, HTTPException, status
from datetime import datetime, timedelta
from typing import List, Union, Optional

from fastapi import APIRouter
from pydantic import BaseModel
import time
import uuid

from apps.web.models.users import UserModel, UserRoleUpdateForm, Users

13
from utils.utils import get_current_user
Timothy J. Baek's avatar
Timothy J. Baek committed
14
15
16
17
18
19
20
21
22
23
from constants import ERROR_MESSAGES

router = APIRouter()

############################
# GetUsers
############################


@router.get("/", response_model=List[UserModel])
24
25
async def get_users(skip: int = 0, limit: int = 50, user=Depends(get_current_user)):
    if user.role != "admin":
Timothy J. Baek's avatar
Timothy J. Baek committed
26
        raise HTTPException(
27
28
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
29
        )
30
    return Users.get_users(skip, limit)
Timothy J. Baek's avatar
Timothy J. Baek committed
31
32
33
34
35
36
37
38


############################
# UpdateUserRole
############################


@router.post("/update/role", response_model=Optional[UserModel])
39
40
41
42
43
44
45
46
async def update_user_role(
    form_data: UserRoleUpdateForm, user=Depends(get_current_user)
):
    if user.role != "admin":
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
47

48
49
    if user.id != form_data.id:
        return Users.update_user_role_by_id(form_data.id, form_data.role)
Timothy J. Baek's avatar
Timothy J. Baek committed
50
51
    else:
        raise HTTPException(
52
53
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACTION_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
54
        )