This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/middleware/team.go
Jeffrey Smith 23dddae0c5
All checks were successful
CI/CD / detect-changes (pull_request) Successful in 4s
CI/CD / test-runners (pull_request) Has been skipped
CI/CD / e2e-smoke (pull_request) Has been skipped
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Successful in 2m49s
CI/CD / test-sqlite (pull_request) Successful in 2m57s
CI/CD / build-and-deploy (pull_request) Successful in 1m19s
Feat v0.9.3 team user roles
Promote team roles to a kernel primitive with many-to-many support.
Users can now hold multiple roles within a team simultaneously.

- Migration 016: team_user_roles table (both dialects)
- 6 new TeamStore methods (AddUserRole, RemoveUserRole, ListUserRoles,
  GetMemberRoles, HasRole, RemoveAllUserRoles)
- RequireRole() middleware with OR semantics and system admin bypass
- 3 new handler endpoints for member role CRUD
- Manifest requires_roles field (advisory for v0.9.3)
- Starlark teams module: get_member_roles(), has_role()
- Team-admin UI: role badge chips + assignment dropdown
- Fixed pre-existing SDK auto-unwrap bug in loadRoles
- 10 new tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-03 15:22:37 +00:00

133 lines
3.3 KiB
Go

package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
"armature/auth"
"armature/store"
)
// isSystemAdmin checks if the user has the surface.admin.access permission,
// which grants system-wide admin privileges including team access bypass.
func isSystemAdmin(c *gin.Context, stores store.Stores) bool {
userID := c.GetString("user_id")
if userID == "" {
return false
}
perms, err := resolveAndCachePerms(c, stores, userID)
if err != nil {
return false
}
return perms[auth.PermSurfaceAdminAccess]
}
// RequireTeamAdmin returns middleware that restricts access to team admins.
// System admins are always allowed through.
func RequireTeamAdmin(teams store.TeamStore, allStores ...store.Stores) gin.HandlerFunc {
return func(c *gin.Context) {
// System admin bypass via permissions
if len(allStores) > 0 && isSystemAdmin(c, allStores[0]) {
c.Next()
return
}
userID := c.GetString("user_id")
teamID := c.Param("teamId")
if teamID == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "team ID required",
})
return
}
isAdmin, err := teams.IsTeamAdmin(c.Request.Context(), teamID, userID)
if err != nil || !isAdmin {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "team admin access required",
})
return
}
c.Next()
}
}
// RequireRole returns middleware that restricts access to users holding at least
// one of the specified roles in the team identified by :teamId.
// Checks both the primary role (team_members.role) and additional roles
// (team_user_roles). System admins are always allowed through.
func RequireRole(teams store.TeamStore, roles []string, allStores ...store.Stores) gin.HandlerFunc {
return func(c *gin.Context) {
if len(allStores) > 0 && isSystemAdmin(c, allStores[0]) {
c.Next()
return
}
userID := c.GetString("user_id")
teamID := c.Param("teamId")
if teamID == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "team ID required",
})
return
}
memberRoles, err := teams.GetMemberRoles(c.Request.Context(), teamID, userID)
if err != nil || len(memberRoles) == 0 {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "required role not held",
})
return
}
roleSet := make(map[string]bool, len(memberRoles))
for _, r := range memberRoles {
roleSet[r] = true
}
for _, required := range roles {
if roleSet[required] {
c.Next()
return
}
}
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "required role not held",
})
}
}
// RequireTeamMember returns middleware that restricts access to users who
// belong to the team identified by :teamId (any role).
// System admins are always allowed through.
func RequireTeamMember(teams store.TeamStore, allStores ...store.Stores) gin.HandlerFunc {
return func(c *gin.Context) {
// System admin bypass via permissions
if len(allStores) > 0 && isSystemAdmin(c, allStores[0]) {
c.Next()
return
}
userID := c.GetString("user_id")
teamID := c.Param("teamId")
if teamID == "" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"error": "team ID required",
})
return
}
isMember, _ := teams.IsMember(c.Request.Context(), teamID, userID)
if !isMember {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "team membership required",
})
return
}
c.Next()
}
}