143 lines
3.7 KiB
Python
143 lines
3.7 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_active_superuser, get_current_active_user, get_db
|
|
from app.models.user import User
|
|
from app.schemas.user import User as UserSchema
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
from app.services.user import (
|
|
create_user,
|
|
delete_user,
|
|
get_user,
|
|
get_user_by_email,
|
|
get_users,
|
|
update_user,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[UserSchema])
|
|
def read_users(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Retrieve users. Only superusers can retrieve all users.
|
|
"""
|
|
users = get_users(db, skip=skip, limit=limit)
|
|
return users
|
|
|
|
|
|
@router.post("/", response_model=UserSchema)
|
|
def create_new_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_in: UserCreate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new user. Only superusers can create new users.
|
|
"""
|
|
user = get_user_by_email(db, email=user_in.email)
|
|
if user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A user with this email already exists.",
|
|
)
|
|
user = create_user(db, obj_in=user_in)
|
|
return user
|
|
|
|
|
|
@router.get("/me", response_model=UserSchema)
|
|
def read_user_me(
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get current user.
|
|
"""
|
|
return current_user
|
|
|
|
|
|
@router.put("/me", response_model=UserSchema)
|
|
def update_user_me(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_in: UserUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update own user.
|
|
"""
|
|
user = update_user(db, db_obj=current_user, obj_in=user_in)
|
|
return user
|
|
|
|
|
|
@router.get("/{user_id}", response_model=UserSchema)
|
|
def read_user_by_id(
|
|
user_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get a specific user by id.
|
|
"""
|
|
user = get_user(db, id=user_id)
|
|
if user == current_user:
|
|
return user
|
|
if not current_user.is_superuser:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="The user doesn't have enough privileges",
|
|
)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found",
|
|
)
|
|
return user
|
|
|
|
|
|
@router.put("/{user_id}", response_model=UserSchema)
|
|
def update_specific_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_id: int,
|
|
user_in: UserUpdate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update a user. Only superusers can update other users.
|
|
"""
|
|
user = get_user(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found",
|
|
)
|
|
user = update_user(db, db_obj=user, obj_in=user_in)
|
|
return user
|
|
|
|
|
|
@router.delete("/{user_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_specific_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_id: int,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete a user. Only superusers can delete users.
|
|
"""
|
|
user = get_user(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="User not found",
|
|
)
|
|
delete_user(db, id=user_id)
|
|
return None |