fix: resolve audit report gaps across bulk imports, auth, scoping, and exports
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||
from pydantic import BaseModel, Field
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
@@ -19,6 +20,10 @@ from ..services.permissions import Role
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
|
||||
|
||||
class ResetPasswordRequest(BaseModel):
|
||||
new_password: str = Field(..., min_length=6)
|
||||
|
||||
|
||||
def _user_list_out(user: User) -> UserListOut:
|
||||
return UserListOut(
|
||||
id=user.id,
|
||||
@@ -77,6 +82,12 @@ def create_user(
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_roles(Role.system_admin)),
|
||||
):
|
||||
if payload.role == "department_user" and payload.department_id is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Department is required for department_user role.",
|
||||
)
|
||||
|
||||
if payload.department_id is not None:
|
||||
dept = db.query(Department).filter(Department.id == payload.department_id).first()
|
||||
if dept is None:
|
||||
@@ -108,13 +119,52 @@ def update_user(
|
||||
user_id: int,
|
||||
payload: UserUpdate,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_roles(Role.system_admin)),
|
||||
current_user: User = Depends(require_roles(Role.system_admin)),
|
||||
):
|
||||
user = db.query(User).options(joinedload(User.department)).filter(User.id == user_id).first()
|
||||
if user is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found.")
|
||||
|
||||
data = payload.model_dump(exclude_unset=True)
|
||||
|
||||
# Department user validation
|
||||
target_role = data.get("role", user.role)
|
||||
target_dept = data.get("department_id", user.department_id)
|
||||
if target_role == "department_user" and target_dept is None:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Department is required for department_user role.",
|
||||
)
|
||||
|
||||
# Prevent self-deactivation or changing own admin role
|
||||
if user.id == current_user.id:
|
||||
if "is_active" in data and not data["is_active"]:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot deactivate your own administrator account.",
|
||||
)
|
||||
if "role" in data and data["role"] != "system_admin":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot change the role of your own administrator account.",
|
||||
)
|
||||
|
||||
# Prevent deactivating or demoting the last active system admin
|
||||
if user.role == "system_admin" and (
|
||||
("is_active" in data and not data["is_active"])
|
||||
or ("role" in data and data["role"] != "system_admin")
|
||||
):
|
||||
active_admins = (
|
||||
db.query(User)
|
||||
.filter(User.role == "system_admin", User.is_active.is_(True), User.id != user.id)
|
||||
.count()
|
||||
)
|
||||
if active_admins == 0:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot deactivate or change role: system must have at least one active system administrator.",
|
||||
)
|
||||
|
||||
if "department_id" in data and data["department_id"] is not None:
|
||||
dept = db.query(Department).filter(Department.id == data["department_id"]).first()
|
||||
if dept is None:
|
||||
@@ -153,12 +203,52 @@ def update_user(
|
||||
def delete_user(
|
||||
user_id: int,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_roles(Role.system_admin)),
|
||||
current_user: User = Depends(require_roles(Role.system_admin)),
|
||||
):
|
||||
if user_id == current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot delete your own administrator account.",
|
||||
)
|
||||
|
||||
user = db.query(User).options(joinedload(User.department)).filter(User.id == user_id).first()
|
||||
if user is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found.")
|
||||
|
||||
if user.role == "system_admin":
|
||||
active_admins = (
|
||||
db.query(User)
|
||||
.filter(User.role == "system_admin", User.is_active.is_(True), User.id != user.id)
|
||||
.count()
|
||||
)
|
||||
if active_admins == 0:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Cannot delete the only active system administrator account.",
|
||||
)
|
||||
|
||||
user.is_active = False
|
||||
db.commit()
|
||||
db.refresh(user)
|
||||
return _user_list_out(user)
|
||||
|
||||
|
||||
@router.post("/{user_id}/reset-password")
|
||||
def reset_user_password(
|
||||
user_id: int,
|
||||
payload: ResetPasswordRequest,
|
||||
db: Session = Depends(get_db),
|
||||
_: User = Depends(require_roles(Role.system_admin)),
|
||||
):
|
||||
user = db.query(User).filter(User.id == user_id).first()
|
||||
if user is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found.")
|
||||
new_pass = payload.new_password.strip()
|
||||
if len(new_pass) < 6:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
detail="Password must be at least 6 characters.",
|
||||
)
|
||||
user.password_hash = hash_password(new_pass)
|
||||
db.commit()
|
||||
return {"message": f"Password for {user.name} has been reset successfully."}
|
||||
|
||||
Reference in New Issue
Block a user