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.

33 lines
766 B

  1. package middleware
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model/request"
  5. "gin-vue-admin/model/response"
  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. success, _ := e.Enforce(sub, obj, act)
  23. if global.GVA_CONFIG.System.Env == "develop" || success {
  24. c.Next()
  25. } else {
  26. response.FailWithDetailed(gin.H{}, "权限不足", c)
  27. c.Abort()
  28. return
  29. }
  30. }
  31. }