feat(admin): Phase 0 平台超管基础 — is_platform_admin + IsPlatformAdmin + create_platform_admin(admin/admin123) + AdminAuditLog + Admin 后台外壳与路由 gating

后端: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>
This commit is contained in:
seaislee1209
2026-06-19 20:49:34 +08:00
co-authored by Claude Opus 4.8
parent 8fc3870fa3
commit 443023a1a9
21 changed files with 907 additions and 12 deletions
+31
View File
@@ -15,6 +15,9 @@ class User(AbstractUser):
status = models.CharField(max_length=24, choices=Status.choices, default=Status.ACTIVE)
phone = models.CharField(max_length=32, blank=True)
avatar_url = models.URLField(blank=True)
# 平台超级管理员:凌驾于团队 owner 之上的跨团队后台权限(发开团队码 / 管所有团队 / 治理)。
# 与团队解耦 —— 平台超管可不属于任何团队(get_current_team 对其返回 None,前端登录直落 /admin)。
is_platform_admin = models.BooleanField(default=False)
@property
def is_disabled(self) -> bool:
@@ -145,3 +148,31 @@ class Invitation(TimeStampedModel):
def __str__(self) -> str:
return f"{self.team} / {self.code} / {self.status}"
class AdminAuditLog(TimeStampedModel):
"""平台超管操作审计:谁(operator)在什么时间对哪个对象(target)做了什么(action),
可选 before/after 快照。operator_name 单独存档,即使账号被删审计仍可读。所有跨团队
后台写操作(发码 / 启停团队 / 改密 / 调额 / 改模型等)都应经 log_admin_action() 落一条。"""
operator = models.ForeignKey(
User, on_delete=models.SET_NULL, null=True, blank=True, related_name="admin_audit_logs"
)
operator_name = models.CharField(max_length=150, blank=True)
action = models.CharField(max_length=48)
target_type = models.CharField(max_length=32, blank=True)
target_id = models.CharField(max_length=64, blank=True)
target_name = models.CharField(max_length=200, blank=True)
before = models.JSONField(null=True, blank=True)
after = models.JSONField(null=True, blank=True)
ip_address = models.GenericIPAddressField(null=True, blank=True)
class Meta:
ordering = ["-created_at"]
indexes = [
models.Index(fields=["action"]),
models.Index(fields=["-created_at"]),
]
def __str__(self) -> str:
return f"{self.operator_name} / {self.action} / {self.target_type}:{self.target_id}"