users.py 2.41 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
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
Timothy J. Baek's avatar
Timothy J. Baek committed
12
13
from apps.web.models.auths import Auths

Timothy J. Baek's avatar
Timothy J. Baek committed
14

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

router = APIRouter()

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


@router.get("/", response_model=List[UserModel])
26
27
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
28
        raise HTTPException(
29
30
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
31
        )
32
    return Users.get_users(skip, limit)
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34
35
36
37
38
39
40


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


@router.post("/update/role", response_model=Optional[UserModel])
41
42
43
44
45
46
47
48
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
49

50
51
    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
52
53
    else:
        raise HTTPException(
54
55
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACTION_PROHIBITED,
Timothy J. Baek's avatar
Timothy J. Baek committed
56
        )
57
58
59


############################
Timothy J. Baek's avatar
Timothy J. Baek committed
60
# DeleteUserById
61
62
63
64
############################


@router.delete("/{user_id}", response_model=bool)
Anuraag Jain's avatar
Anuraag Jain committed
65
66
67
68
69
70
71
async def delete_user_by_id(user_id: str, user=Depends(get_current_user)):
    if user.role == "admin":
        if user.id != user_id:
            result = Auths.delete_auth_by_id(user_id)

            if result:
                return True
72
73
            else:
                raise HTTPException(
Anuraag Jain's avatar
Anuraag Jain committed
74
75
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    detail=ERROR_MESSAGES.DELETE_USER_ERROR,
76
77
78
79
                )
        else:
            raise HTTPException(
                status_code=status.HTTP_403_FORBIDDEN,
Anuraag Jain's avatar
Anuraag Jain committed
80
                detail=ERROR_MESSAGES.ACTION_PROHIBITED,
81
82
83
            )
    else:
        raise HTTPException(
Anuraag Jain's avatar
Anuraag Jain committed
84
85
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
86
        )