permission.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package middleware
  2. import (
  3. "IotAdmin/common/permission"
  4. log "IotAdmin/core/logger"
  5. "net/http"
  6. "IotAdmin/core/sdk"
  7. jwtauth "IotAdmin/core/sdk/pkg/jwt-auth"
  8. "github.com/gin-gonic/gin"
  9. )
  10. func SetDataPermission() gin.HandlerFunc {
  11. return func(c *gin.Context) {
  12. p := (&permission.DataPermission{}).NewDataPermission(c)
  13. c.Set(permission.DataPermissionKey, p)
  14. c.Next()
  15. }
  16. }
  17. // AuthCheckRole 权限检查中间件
  18. func AuthCheckRole() gin.HandlerFunc {
  19. return func(c *gin.Context) {
  20. data, _ := c.Get(jwtauth.JwtPayloadKey)
  21. v := data.(jwtauth.MapClaims)
  22. e := sdk.Runtime.GetCasbinKey(c.Request.Host)
  23. res := permission.CheckRoleApi(c, v, e)
  24. if res {
  25. log.Debugf("isTrue: %v role: %s method: %s path: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path)
  26. c.Next()
  27. } else {
  28. log.Warnf("isTrue: %v role: %s method: %s path: %s message: %s", res, v["rolekey"], c.Request.Method, c.Request.URL.Path, "当前接口无权限,请管理员确认!")
  29. c.JSON(http.StatusOK, gin.H{
  30. "code": 403,
  31. "msg": "对不起,您没有该接口访问权限,请联系管理员",
  32. })
  33. c.Abort()
  34. return
  35. }
  36. }
  37. }