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.

32 lines
749 B

  1. package middleware
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model/request"
  6. "gin-vue-admin/service"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // 拦截器
  10. func CasbinHandler() gin.HandlerFunc {
  11. return func(c *gin.Context) {
  12. claims, _ := c.Get("claims")
  13. waitUse := claims.(*request.CustomClaims)
  14. // 获取请求的URI
  15. obj := c.Request.URL.RequestURI()
  16. // 获取请求方法
  17. act := c.Request.Method
  18. // 获取用户的角色
  19. sub := waitUse.AuthorityId
  20. e := service.Casbin()
  21. // 判断策略中是否存在
  22. if global.GVA_CONFIG.System.Env == "develop" || e.Enforce(sub, obj, act) {
  23. c.Next()
  24. } else {
  25. response.Result(response.ERROR, gin.H{}, "权限不足", c)
  26. c.Abort()
  27. return
  28. }
  29. }
  30. }