-
Notifications
You must be signed in to change notification settings - Fork 0
/
casbin.go
48 lines (44 loc) · 901 Bytes
/
casbin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package middlewares
import (
"fmt"
"net/http"
"github.com/itcuihao/staging/s1/common"
"github.com/gin-gonic/gin"
)
// CasbinMiddleware casbin中间件
func (m Middleware) CasbinMiddleware(c *gin.Context) {
cfg := common.GetCasbin()
if !cfg.Enable {
c.Next()
return
}
if SkipHandler(c, m.Skippers...) {
c.Next()
return
}
// 资源
obj := c.Request.URL.Path
// 方法
act := c.Request.Method
// 用户
sub := c.GetString(AuthKeyUserRole)
fmt.Println(sub, act, obj)
pass, err := m.CasbinRule.Enforce(sub, obj, act)
fmt.Println(pass)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"code": common.ErrCodePermissionDenied,
"error": "没有权限访问",
})
c.Abort()
return
} else if !pass {
c.JSON(http.StatusUnauthorized, gin.H{
"code": common.ErrCodePermissionDenied,
"error": "没有权限访问",
})
c.Abort()
return
}
c.Next()
}