permission.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package middleware
  2. import (
  3. "fmt"
  4. "git.getensh.com/common/gopkgs/cache"
  5. "github.com/tidwall/gjson"
  6. "property-system-gateway/errors"
  7. "strings"
  8. )
  9. const SystemPermissionChangeKeyPrefix = "system_permission_change_"
  10. const GlobalPermissionKey = "property_global_permission"
  11. func gloablePermissionTime(gardenId int64) string {
  12. key := fmt.Sprintf("%s%d", GlobalPermissionKey, gardenId)
  13. str, _ := cache.Redis().Get(key)
  14. return str
  15. }
  16. func userPermissionTime(uid int64) string {
  17. key := fmt.Sprintf("%s%d", SystemPermissionChangeKeyPrefix, uid)
  18. str, _ := cache.Redis().Get(key)
  19. return str
  20. }
  21. func checkPermission(routers map[string]gjson.Result, router string, uid int64, gPermissionTime string, uPermissionTime string) error {
  22. //手机号登录选择账户不作权限检查; 获取权限不做检查
  23. if strings.Contains(router, "user/choose_user") || strings.Contains(router, "permission_reget") {
  24. return nil
  25. }
  26. array := strings.Split(router, "?")
  27. array = strings.Split(array[0], "/")
  28. path := ""
  29. fmt.Printf("has permission:%v\n", routers)
  30. // 逐个匹配路径
  31. for _, v := range array {
  32. if v == "" {
  33. continue
  34. }
  35. if v == "api" || v == "v1" {
  36. continue
  37. }
  38. if path == "" {
  39. path = v
  40. } else {
  41. path = path + "/" + v
  42. }
  43. fmt.Printf("now path:%v\n", path)
  44. if _, ok := routers[path]; ok {
  45. return nil
  46. }
  47. }
  48. return errors.PermissionError
  49. }