drop users.role column: full RBAC through group membership
All checks were successful
CI/CD / detect-changes (pull_request) Successful in 4s
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Successful in 2m22s
CI/CD / test-sqlite (pull_request) Successful in 2m35s
CI/CD / build-and-deploy (pull_request) Successful in 1m18s
All checks were successful
CI/CD / detect-changes (pull_request) Successful in 4s
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Successful in 2m22s
CI/CD / test-sqlite (pull_request) Successful in 2m35s
CI/CD / build-and-deploy (pull_request) Successful in 1m18s
The role column was a pre-RBAC artifact. All authorization now flows through explicit group membership and permission grants: - Everyone group: all users added on creation (no implicit membership) - Admins group: grants surface.admin.access + all platform permissions - JWT claims, login response, profile: role field removed - OIDC: isIdPAdmin() maps IdP claims → Admins group (no role writes) - Admin UI: role dropdown removed, admin managed through groups - Middleware cache simplified to isActive only 28 files changed, -79 lines net. Zero magic roles. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -69,29 +69,25 @@ func (h *AdminHandler) CreateUser(c *gin.Context) {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Email string `json:"email" binding:"required"`
|
||||
Password string `json:"password" binding:"required,min=8"`
|
||||
Role string `json:"role"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
hash, _ := bcrypt.GenerateFromPassword([]byte(req.Password), bcryptCost)
|
||||
role := models.UserRoleUser
|
||||
if req.Role == models.UserRoleAdmin {
|
||||
role = models.UserRoleAdmin
|
||||
}
|
||||
|
||||
user := &models.User{
|
||||
Username: strings.ToLower(req.Username),
|
||||
Email: strings.ToLower(req.Email),
|
||||
PasswordHash: string(hash),
|
||||
Role: role,
|
||||
IsActive: true,
|
||||
Handle: auth.UniqueHandle(c.Request.Context(), h.stores.Users, models.HandleFromName(req.Username)),
|
||||
Handle: auth.UniqueHandle(ctx, h.stores.Users, models.HandleFromName(req.Username)),
|
||||
}
|
||||
|
||||
if err := h.stores.Users.Create(c.Request.Context(), user); err != nil {
|
||||
if err := h.stores.Users.Create(ctx, user); err != nil {
|
||||
if database.IsUniqueViolation(err) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "username or email already exists"})
|
||||
return
|
||||
@@ -100,8 +96,10 @@ func (h *AdminHandler) CreateUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Sync Admins group membership with assigned role.
|
||||
auth.SyncAdminsGroupMembership(c.Request.Context(), h.stores, user.ID, role)
|
||||
auth.EnsureEveryoneGroup(ctx, h.stores, user.ID)
|
||||
if req.IsAdmin {
|
||||
auth.AddToAdminsGroup(ctx, h.stores, user.ID)
|
||||
}
|
||||
|
||||
h.auditLog(c, "user.create", "user", user.ID, nil)
|
||||
c.JSON(http.StatusCreated, user)
|
||||
@@ -111,42 +109,42 @@ func (h *AdminHandler) UpdateUserRole(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
ctx := c.Request.Context()
|
||||
var req struct {
|
||||
Role string `json:"role" binding:"required"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.stores.Users.GetByID(ctx, id)
|
||||
if err != nil || user == nil {
|
||||
if _, err := h.stores.Users.GetByID(ctx, id); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Guard: prevent demoting the last admin.
|
||||
// Count Admins group members — the source of truth for admin access.
|
||||
if req.Role != models.UserRoleAdmin && user.Role == models.UserRoleAdmin {
|
||||
if !req.IsAdmin {
|
||||
// Guard: prevent removing the last admin from Admins group.
|
||||
members, _ := h.stores.Groups.ListMembers(ctx, auth.AdminsGroupID)
|
||||
if len(members) <= 1 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "cannot demote the last admin"})
|
||||
isMember := false
|
||||
for _, m := range members {
|
||||
if m.UserID == id {
|
||||
isMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isMember && len(members) <= 1 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "cannot remove the last admin"})
|
||||
return
|
||||
}
|
||||
auth.RemoveFromAdminsGroup(ctx, h.stores, id)
|
||||
} else {
|
||||
auth.AddToAdminsGroup(ctx, h.stores, id)
|
||||
}
|
||||
|
||||
if err := h.stores.Users.Update(ctx, id, map[string]interface{}{"role": req.Role}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to update role"})
|
||||
return
|
||||
}
|
||||
|
||||
// Sync Admins group membership with role change.
|
||||
auth.SyncAdminsGroupMembership(ctx, h.stores, id, req.Role)
|
||||
|
||||
h.auditLog(c, "user.role_change", "user", id, gin.H{"role": req.Role})
|
||||
h.auditLog(c, "user.role_change", "user", id, gin.H{"is_admin": req.IsAdmin})
|
||||
if h.onUserChanged != nil {
|
||||
h.onUserChanged(id)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "role updated"})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "admin status updated"})
|
||||
}
|
||||
|
||||
func (h *AdminHandler) ToggleUserActive(c *gin.Context) {
|
||||
@@ -255,18 +253,22 @@ func (h *AdminHandler) DeleteUser(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
// Guard: prevent deleting the last admin
|
||||
user, err := h.stores.Users.GetByID(c.Request.Context(), id)
|
||||
if err != nil || user == nil {
|
||||
if _, err := h.stores.Users.GetByID(c.Request.Context(), id); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "user not found"})
|
||||
return
|
||||
}
|
||||
if user.Role == models.UserRoleAdmin {
|
||||
members, _ := h.stores.Groups.ListMembers(c.Request.Context(), auth.AdminsGroupID)
|
||||
if len(members) <= 1 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "cannot delete the last admin"})
|
||||
return
|
||||
members, _ := h.stores.Groups.ListMembers(c.Request.Context(), auth.AdminsGroupID)
|
||||
isAdmin := false
|
||||
for _, m := range members {
|
||||
if m.UserID == id {
|
||||
isAdmin = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isAdmin && len(members) <= 1 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "cannot delete the last admin"})
|
||||
return
|
||||
}
|
||||
|
||||
// Destroy vault and personal BYOK keys before deleting the user row
|
||||
h.destroyVault(c, id)
|
||||
|
||||
Reference in New Issue
Block a user