casbin_rbac.go 933 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package middleware
  2. import (
  3. "strconv"
  4. "strings"
  5. "github.com/gin-gonic/gin"
  6. "lc-fangdaosha/global"
  7. "lc-fangdaosha/model/common/response"
  8. "lc-fangdaosha/service"
  9. "lc-fangdaosha/utils"
  10. )
  11. var casbinService = service.ServiceGroupApp.SystemServiceGroup.CasbinService
  12. // CasbinHandler 拦截器
  13. func CasbinHandler() gin.HandlerFunc {
  14. return func(c *gin.Context) {
  15. if global.Config.System.Env != "develop" {
  16. waitUse, _ := utils.GetClaims(c)
  17. //获取请求的PATH
  18. path := c.Request.URL.Path
  19. obj := strings.TrimPrefix(path, global.Config.System.RouterPrefix)
  20. // 获取请求方法
  21. act := c.Request.Method
  22. // 获取用户的角色
  23. sub := strconv.Itoa(int(waitUse.AuthorityId))
  24. e := casbinService.Casbin() // 判断策略中是否存在
  25. success, _ := e.Enforce(sub, obj, act)
  26. if !success {
  27. response.FailWithDetailed(gin.H{}, "权限不足", c)
  28. c.Abort()
  29. return
  30. }
  31. }
  32. c.Next()
  33. }
  34. }