mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-12-23 06:42:41 +00:00
role_required.go add
This commit is contained in:
parent
9178fcfd36
commit
668f421efd
1 changed files with 28 additions and 0 deletions
28
web/middleware/role_required.go
Normal file
28
web/middleware/role_required.go
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
package middleware
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RoleRequired проверяет, есть ли у пользователя нужная роль.
|
||||||
|
func RoleRequired(roles ...string) gin.HandlerFunc {
|
||||||
|
allowed := make(map[string]bool)
|
||||||
|
for _, r := range roles {
|
||||||
|
allowed[r] = true
|
||||||
|
}
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
roleVal, exists := c.Get("role") // где-то до этого роль должна быть положена в контекст
|
||||||
|
if !exists {
|
||||||
|
c.AbortWithStatus(http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
role, ok := roleVal.(string)
|
||||||
|
if !ok || !allowed[role] {
|
||||||
|
c.AbortWithStatus(http.StatusForbidden)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue