b50495bebe
短信(SMS_MOCK 切 mock/real): - integrations/sms.py 重写: real 模式走极光短信 REST /v1/messages 自定义验证码(本服务 secrets 生成 6 位码 + 进程内存 + 本地校验一次性/防爆破), 鉴权复用极光一键登录 JG_APP_KEY/MASTER_SECRET (同一极光应用, 上线只需 SMS_MOCK=false); mock 仍"任意6位通过"不动其余测试 - 防刷四层: 单号冷却 + 单号每日上限 + 单IP rate_limit(/sms/send 10/min、/sms/login 20/min) + 单码失败次数作废; SmsError 带 status_code 映射 429/503/400 - config 增 SMS_SEND_ENDPOINT/SIGN_ID/TEMPLATE_ID/CODE_LENGTH/DAILY_LIMIT/MAX_VERIFY_ATTEMPTS; test_auth 加 real 模式单测; sms.md/后端技术实现/待办账本同步 admin 后台(app/admin/ 独立子应用, uvicorn app.admin.main:admin_app :8771): - 复用主仓 models/repositories/integrations + 同库, 鉴权完全隔离(ADMIN_JWT_SECRET≠JWT_SECRET_KEY + payload typ=admin + bcrypt 密码 + 可选 IP 白名单); 主 app 不 import 本包, admin 崩不影响主进程 - 路由: 登录 / 账号管理(RBAC: super_admin·finance·operator) / 用户列表+360详情+封禁+手动调币 / 钱包流水 / 提现重试对账 / 反馈工单 / 数据大盘; 全写操作落 admin_audit_log(涉钱与业务写同事务) - 涉钱逻辑(调微信/退款/对账)复用 app.repositories.wallet 不重写 - 新增 models/admin.py(AdminUser/AdminAuditLog) + admin_tables 迁移 + create_admin.py + deploy/shaguabijia-admin.service; 依赖加 bcrypt Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
70 lines
2.8 KiB
Python
70 lines
2.8 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 通过
|
|
)
|
|
|
|
|
|
@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="不能禁用自己")
|
|
|
|
changes: dict = {}
|
|
if body.role is not None:
|
|
target.role = body.role
|
|
changes["role"] = body.role
|
|
if body.status is not None:
|
|
target.status = body.status
|
|
changes["status"] = body.status
|
|
if body.password is not None:
|
|
target.password_hash = hash_password(body.password)
|
|
changes["password"] = "reset"
|
|
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)
|