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
from constants import ERROR_MESSAGES

18

Timothy J. Baek's avatar
Timothy J. Baek committed
19
20
21
22
23
24
25
26
router = APIRouter()

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


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


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


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

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


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


@router.delete("/{user_id}", response_model=bool)
Anuraag Jain's avatar
Anuraag Jain committed
66
67
68
69
70
71
72
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
73
74
            else:
                raise HTTPException(
Anuraag Jain's avatar
Anuraag Jain committed
75
76
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    detail=ERROR_MESSAGES.DELETE_USER_ERROR,
77
78
79
80
                )
        else:
            raise HTTPException(
                status_code=status.HTTP_403_FORBIDDEN,
Anuraag Jain's avatar
Anuraag Jain committed
81
                detail=ERROR_MESSAGES.ACTION_PROHIBITED,
82
83
84
            )
    else:
        raise HTTPException(
Anuraag Jain's avatar
Anuraag Jain committed
85
86
            status_code=status.HTTP_403_FORBIDDEN,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
87
        )