23 lines
479 B
Go
23 lines
479 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// RequireAdmin returns middleware that restricts access to admin users.
|
|
// Must be used after Auth() middleware which sets "role" in context.
|
|
func RequireAdmin() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
role, exists := c.Get("role")
|
|
if !exists || role != "admin" {
|
|
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{
|
|
"error": "admin access required",
|
|
})
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|