You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-fly/middleware/casbin.go

31 lines
584 B

package middleware
import (
"fmt"
"github.com/casbin/casbin/v2"
"github.com/gin-gonic/gin"
"log"
)
func CasbinACL(c *gin.Context){
sub, _ :=c.Get("user")
obj:=c.Request.RequestURI
act:=c.Request.Method
e, err := casbin.NewEnforcer("config/model.conf", "config/policy.csv")
log.Println(sub,obj,act,err)
ok,err:=e.Enforce(sub,obj,act)
if err!=nil{
c.JSON(200, gin.H{
"code": 403,
"msg": "没有权限:"+err.Error(),
})
c.Abort()
}
if !ok{
c.JSON(200, gin.H{
"code": 403,
"msg": fmt.Sprintf("没有权限:%s,%s,%s",sub,obj,act),
})
c.Abort()
}
}