All checks were successful
CI/CD / test-go-pg (push) Successful in 2m54s
CI/CD / test-sqlite (push) Successful in 3m1s
CI/CD / build-and-deploy (push) Successful in 1m47s
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-frontend (push) Successful in 6s
CI/CD / detect-changes (push) Successful in 4s
CI/CD / test-runners (push) Has been skipped
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com> Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
88 lines
2.1 KiB
Go
88 lines
2.1 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()
|
|
}
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
}
|