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/admin.go
Jeffrey Smith 5836ddad21
Some checks failed
CI/CD / detect-changes (push) Successful in 4s
CI/CD / test-frontend (push) Successful in 5s
CI/CD / build-and-deploy (push) Has been cancelled
CI/CD / test-sqlite (push) Has been cancelled
CI/CD / test-go-pg (push) Has been cancelled
Feat admin rbac migration (#1)
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com>
Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
2026-03-26 17:30:36 +00:00

28 lines
708 B
Go

package middleware
import (
"net/http"
"github.com/gin-gonic/gin"
"switchboard-core/auth"
"switchboard-core/store"
)
// RequireAdmin returns middleware that restricts access to users with the
// surface.admin.access permission (i.e. members of the Admins group).
// Must be used after Auth() middleware which sets "user_id" in context.
func RequireAdmin(stores store.Stores) gin.HandlerFunc {
return func(c *gin.Context) {
userID := c.GetString("user_id")
perms, err := resolveAndCachePerms(c, stores, userID)
if err != nil || !perms[auth.PermSurfaceAdminAccess] {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
"error": "admin access required",
})
return
}
c.Next()
}
}