后端:User.is_platform_admin + migration;权限类 IsPlatformAdmin;管理命令建 admin/admin123(幂等); AdminAuditLog 模型 + log_admin_action() helper;me/login 对无团队超管优雅返回 team=null;UserSerializer 暴露标志。 前端:routes/admin 后台外壳(分组侧栏 + 概览 + 占位)、/admin 路由解析与 gating(超管直落、非超管纠回)、 侧栏平台入口、admin-page.css(仅 token)、IconKitSvg 补图标。 测试:accounts 11/11 单测过;无头 e2e _admin-p0.mjs 全断言过 + 0 console error;tsc+build 绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""平台超管审计写入 helper。失败绝不阻断主流程(审计是旁路)。"""
|
|
|
|
|
|
def _client_ip(request):
|
|
if request is None:
|
|
return None
|
|
forwarded = request.META.get("HTTP_X_FORWARDED_FOR", "")
|
|
if forwarded:
|
|
return forwarded.split(",")[0].strip()
|
|
return request.META.get("REMOTE_ADDR") or None
|
|
|
|
|
|
def log_admin_action(
|
|
request,
|
|
action,
|
|
*,
|
|
target_type="",
|
|
target_id="",
|
|
target_name="",
|
|
before=None,
|
|
after=None,
|
|
):
|
|
"""记录一条平台超管操作审计。action 用动词短语(如 'invite.issue' / 'team.disable')。
|
|
operator_name 取当前登录用户名快照。任何异常吞掉,不影响业务返回。"""
|
|
from .models import AdminAuditLog
|
|
|
|
try:
|
|
user = getattr(request, "user", None)
|
|
operator = user if (user is not None and getattr(user, "is_authenticated", False)) else None
|
|
AdminAuditLog.objects.create(
|
|
operator=operator,
|
|
operator_name=(getattr(operator, "username", "") or ""),
|
|
action=action,
|
|
target_type=target_type or "",
|
|
target_id=str(target_id or ""),
|
|
target_name=target_name or "",
|
|
before=before,
|
|
after=after,
|
|
ip_address=_client_ip(request),
|
|
)
|
|
except Exception: # noqa: BLE001 — 审计失败不应阻断主流程
|
|
pass
|