feat(system-admin): implement revocation of system admin privileges with safeguards

- Added RevokeSystemAdmin functionality to the user service and repository, ensuring atomic checks for self-revoke and last admin scenarios.
- Updated the system handler to utilize the new revocation method, improving error handling for various edge cases.
- Enhanced the bootstrap process to prevent unintended promotions when system admins already exist.
- Refactored related comments and documentation for clarity on the new behavior and safeguards in place.
This commit is contained in:
wizardchen
2026-05-24 21:53:15 +08:00
committed by lyingbug
parent 47a183aa65
commit d074dc067a
8 changed files with 301 additions and 195 deletions
+4 -14
View File
@@ -113,13 +113,10 @@ func RequireRole(min types.TenantRole, cfg *config.Config) gin.HandlerFunc {
// editing global settings, cross-tenant operations) where the per-tenant
// Owner/Admin/Contributor/Viewer ladder does not apply.
//
// When cfg.Tenant.EnableRBAC is false, the middleware logs the would-be
// rejection but lets the request through — preserving backward
// compatibility during rollout. Once operators flip the flag to true,
// the same code paths start rejecting unauthorised callers. SystemAdmin
// rides on the same RBAC kill-switch deliberately: ops should be able to
// disable BOTH per-tenant RBAC and system-admin gating during an
// emergency without juggling two independent flags.
// Unlike tenant-role guards, this check is always enforced. The
// tenant RBAC rollout switch only controls per-tenant Owner/Admin/etc.
// checks; it must not turn platform-wide administration endpoints into
// "any authenticated user can call this" endpoints.
func RequireSystemAdmin(cfg *config.Config) gin.HandlerFunc {
warnOnNilConfig(cfg)
return func(c *gin.Context) {
@@ -129,13 +126,6 @@ func RequireSystemAdmin(cfg *config.Config) gin.HandlerFunc {
return
}
uid, _ := types.UserIDFromContext(ctx)
if !rbacEnforcementEnabled(cfg) {
logger.Warnf(ctx,
"[rbac] system admin required (logged but not enforced): user=%s path=%s",
uid, c.Request.URL.Path)
c.Next()
return
}
logger.Warnf(ctx,
"[rbac] system admin required: user=%s path=%s",
uid, c.Request.URL.Path)