feat(system-admin): implement bootstrap for system admin promotion and enhance system settings management
- Added WEKNORA_BOOTSTRAP_SYSTEM_ADMIN_EMAIL environment variable to promote a specified user to system admin on startup. - Introduced a new bootstrap process in `bootstrap.go` to handle the promotion logic. - Updated `.env.example` to document the new environment variable and its behavior. - Created new views for managing system administrators and system settings, including listing, promoting, and revoking admin privileges. - Enhanced the frontend to reflect the new system admin features, including UI elements for admin management and settings configuration. - Updated API interfaces to support system admin functionalities, ensuring proper data handling and user management.
This commit is contained in:
@@ -160,12 +160,14 @@ func Auth(
|
||||
c.Set(types.UserContextKey.String(), user)
|
||||
c.Set(types.UserIDContextKey.String(), user.ID)
|
||||
c.Set(types.TenantRoleContextKey.String(), role)
|
||||
c.Set(types.SystemAdminContextKey.String(), user.IsSystemAdmin)
|
||||
ctx := c.Request.Context()
|
||||
ctx = context.WithValue(ctx, types.TenantIDContextKey, targetTenantID)
|
||||
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)
|
||||
ctx = context.WithValue(ctx, types.UserContextKey, user)
|
||||
ctx = context.WithValue(ctx, types.UserIDContextKey, user.ID)
|
||||
ctx = context.WithValue(ctx, types.TenantRoleContextKey, role)
|
||||
ctx = context.WithValue(ctx, types.SystemAdminContextKey, user.IsSystemAdmin)
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
return
|
||||
@@ -237,9 +239,11 @@ func Auth(
|
||||
c.Set(types.UserContextKey.String(), user)
|
||||
c.Set(types.UserIDContextKey.String(), user.ID)
|
||||
c.Set(types.TenantRoleContextKey.String(), types.TenantRoleAdmin)
|
||||
c.Set(types.SystemAdminContextKey.String(), user.IsSystemAdmin)
|
||||
ctx = context.WithValue(ctx, types.UserContextKey, user)
|
||||
ctx = context.WithValue(ctx, types.UserIDContextKey, user.ID)
|
||||
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleAdmin)
|
||||
ctx = context.WithValue(ctx, types.SystemAdminContextKey, user.IsSystemAdmin)
|
||||
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
|
||||
@@ -103,6 +103,54 @@ func RequireRole(min types.TenantRole, cfg *config.Config) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// RequireSystemAdmin returns a gin middleware that aborts the request with
|
||||
// HTTP 403 unless the caller is a system administrator
|
||||
// (User.IsSystemAdmin = true).
|
||||
//
|
||||
// System administrators operate independently of tenant-scoped roles and
|
||||
// are not bound by the per-tenant RBAC matrix. Use this guard for
|
||||
// platform-wide administrative endpoints (managing other system admins,
|
||||
// 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.
|
||||
func RequireSystemAdmin(cfg *config.Config) gin.HandlerFunc {
|
||||
warnOnNilConfig(cfg)
|
||||
return func(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if types.IsSystemAdminFromContext(ctx) {
|
||||
c.Next()
|
||||
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)
|
||||
// Durable audit row for the reject — same dedup as RequireRole.
|
||||
if svc := AuditServiceFromContext(c); svc != nil {
|
||||
tenantID, _ := types.TenantIDFromContext(ctx)
|
||||
_ = svc.LogDenied(ctx, c, tenantID, uid, "user", "system_admin")
|
||||
}
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Forbidden: system administrator required",
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
// RequireOwnershipOrRole guards endpoints whose access is allowed for
|
||||
// either (a) callers whose role is at least min, or (b) the original
|
||||
// creator of the resource being touched.
|
||||
|
||||
Reference in New Issue
Block a user