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.

34 lines
941 B

  1. package middleware
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  5. "github.com/flipped-aurora/gin-vue-admin/server/service"
  6. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  7. "github.com/gin-gonic/gin"
  8. )
  9. var casbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService
  10. // 拦截器
  11. func CasbinHandler() gin.HandlerFunc {
  12. return func(c *gin.Context) {
  13. waitUse, _ := utils.GetClaims(c)
  14. // 获取请求的URI
  15. obj := c.Request.URL.RequestURI()
  16. // 获取请求方法
  17. act := c.Request.Method
  18. // 获取用户的角色
  19. sub := waitUse.AuthorityId
  20. e := casbinService.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. }