mirror of
https://github.com/open-webui/open-webui
synced 2025-05-17 20:05:08 +00:00
- feat: added new util to get the current user when needed. Middleware was adding authentication logic to all the routes. let's revisit if we can move the non-auth endpoints to a separate route. - refac: update the routes to use new helpers for verification and retrieving user - chore: added black for local formatting of py code
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
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
|
|
|
|
from utils.utils import get_current_user
|
|
from constants import ERROR_MESSAGES
|
|
|
|
router = APIRouter()
|
|
|
|
############################
|
|
# GetUsers
|
|
############################
|
|
|
|
|
|
@router.get("/", response_model=List[UserModel])
|
|
async def get_users(skip: int = 0, limit: int = 50, user=Depends(get_current_user)):
|
|
if user.role != "admin":
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
|
)
|
|
return Users.get_users(skip, limit)
|
|
|
|
|
|
############################
|
|
# UpdateUserRole
|
|
############################
|
|
|
|
|
|
@router.post("/update/role", response_model=Optional[UserModel])
|
|
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,
|
|
)
|
|
|
|
if user.id != form_data.id:
|
|
return Users.update_user_role_by_id(form_data.id, form_data.role)
|
|
else:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail=ERROR_MESSAGES.ACTION_PROHIBITED,
|
|
)
|