27f76918b2
本 PR 汇合三块运营后台改动(原 #50 仅含其中「加固」一块,已并入本 PR 并关闭)。 ## 1. review 加固 (436b2a3) - 超管防自锁:降级/禁用最后一个 active super_admin 前校验,杜绝零超管死局 - 时间筛选统一 tz-aware(列为 timestamptz),比较绝对时刻、不依赖 DB 会话时区 - 上报审核 / 调余额(set·扣减)加行锁,防并发/连点重复发钱 - ad_audit 复算排序补 id 次级键;health-check 限 finance;调账/拒绝 reason 去空白校验 ## 2. 反馈改版 (5a18dbb) - contact 可选、截图≤6;admin 反馈列表筛选/排序;admin·wallet 接口调整 + docs ## 3. 列表页码分页 (1a7a624) - CursorPage 加 total;新增 offset_paginate(count 与分页同源) - 上报/审计日志从 id 游标改 offset 分页(支持跳页) - 用户 / 提现 / 上报 / 审计日志 四页接入页码分页 测试:admin 套件 47 passed。前端配套改动见 shaguabijia-admin-web。 --------- Co-authored-by: OuYingJun1024 <1034284404@qq.com> Reviewed-on: #51 Co-authored-by: ouzhou <ouzhou@wonderable.ai> Co-committed-by: ouzhou <ouzhou@wonderable.ai>
89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
"""admin 账号管理(仅 super_admin):列表 / 创建 / 改角色启停重置密码。均写审计。"""
|
|
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request
|
|
|
|
from app.admin.audit import write_audit
|
|
from app.admin.deps import AdminDb, CurrentAdmin, get_client_ip, require_role
|
|
from app.admin.repositories import admin_user as admin_repo
|
|
from app.admin.schemas.admin import AdminCreateRequest, AdminUpdateRequest
|
|
from app.admin.schemas.auth import AdminOut
|
|
from app.core.security import hash_password
|
|
|
|
router = APIRouter(
|
|
prefix="/admin/api/admins",
|
|
tags=["admin-accounts"],
|
|
dependencies=[Depends(require_role())], # require_role() 无参 = 仅 super_admin 通过
|
|
)
|
|
|
|
|
|
def _active_super_count(db: AdminDb) -> int:
|
|
return sum(
|
|
1 for a in admin_repo.list_admins(db) if a.role == "super_admin" and a.status == "active"
|
|
)
|
|
|
|
|
|
@router.get("", response_model=list[AdminOut], summary="管理员列表")
|
|
def list_admins(db: AdminDb) -> list[AdminOut]:
|
|
return [AdminOut.model_validate(a) for a in admin_repo.list_admins(db)]
|
|
|
|
|
|
@router.post("", response_model=AdminOut, summary="创建管理员")
|
|
def create_admin(
|
|
body: AdminCreateRequest, request: Request, admin: CurrentAdmin, db: AdminDb
|
|
) -> AdminOut:
|
|
if admin_repo.get_by_username(db, body.username) is not None:
|
|
raise HTTPException(status_code=409, detail="用户名已存在")
|
|
new = admin_repo.create_admin(
|
|
db, username=body.username, password=body.password, role=body.role
|
|
)
|
|
write_audit(
|
|
db, admin, action="admin.create", target_type="admin", target_id=new.id,
|
|
detail={"username": new.username, "role": new.role}, ip=get_client_ip(request), commit=True,
|
|
)
|
|
return AdminOut.model_validate(new)
|
|
|
|
|
|
@router.patch("/{admin_id}", response_model=AdminOut, summary="改角色/启停/重置密码")
|
|
def update_admin(
|
|
admin_id: int, body: AdminUpdateRequest, request: Request, admin: CurrentAdmin, db: AdminDb
|
|
) -> AdminOut:
|
|
target = admin_repo.get_by_id(db, admin_id)
|
|
if target is None:
|
|
raise HTTPException(status_code=404, detail="管理员不存在")
|
|
if admin_id == admin.id and body.status == "disabled":
|
|
raise HTTPException(status_code=400, detail="不能禁用自己")
|
|
|
|
# 防自锁:降级 / 禁用某个 super_admin 前,确认操作后仍至少剩 1 个 active super_admin,
|
|
# 否则会进入「零可用超管」死局——本路由仅 super 可进,只能改库恢复。
|
|
demotes_super = (
|
|
target.role == "super_admin"
|
|
and target.status == "active"
|
|
and (
|
|
(body.role is not None and body.role != "super_admin")
|
|
or body.status == "disabled"
|
|
)
|
|
)
|
|
if demotes_super and _active_super_count(db) <= 1:
|
|
raise HTTPException(status_code=400, detail="不能降级/禁用最后一个超级管理员")
|
|
|
|
changes: dict = {}
|
|
if body.role is not None and body.role != target.role:
|
|
changes["role"] = {"before": target.role, "after": body.role}
|
|
target.role = body.role
|
|
if body.status is not None and body.status != target.status:
|
|
changes["status"] = {"before": target.status, "after": body.status}
|
|
target.status = body.status
|
|
if body.password is not None:
|
|
changes["password"] = "reset"
|
|
target.password_hash = hash_password(body.password)
|
|
if not changes:
|
|
raise HTTPException(status_code=400, detail="无任何变更字段")
|
|
db.commit()
|
|
db.refresh(target)
|
|
write_audit(
|
|
db, admin, action="admin.update", target_type="admin", target_id=admin_id,
|
|
detail=changes, ip=get_client_ip(request), commit=True,
|
|
)
|
|
return AdminOut.model_validate(target)
|