permission.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package middleware
  2. import (
  3. "fmt"
  4. "git.getensh.com/common/gopkgs/cache"
  5. "github.com/tidwall/gjson"
  6. "google.golang.org/grpc/status"
  7. "strings"
  8. "property-applete-gateway/errors"
  9. )
  10. const SystemPermissionChangeKeyPrefix = "system_permission_change_"
  11. const GlobalPermissionKey = "property_global_permission"
  12. func globalPermissionTime() string {
  13. str, _ := cache.Redis().Get(GlobalPermissionKey)
  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") {
  24. return nil
  25. }
  26. // 权限变更触发重新登录
  27. if gPermissionTime != globalPermissionTime() || uPermissionTime != userPermissionTime(uid) {
  28. return status.Error(10011, "权限已变更,重新登录")
  29. }
  30. array := strings.Split(router, "?")
  31. array = strings.Split(array[0], "/")
  32. path := ""
  33. // 逐个匹配路径
  34. for _, v := range array {
  35. if v == "" {
  36. continue
  37. }
  38. path = path + "/" + v
  39. if _, ok := routers[path]; ok {
  40. return nil
  41. }
  42. }
  43. return errors.PermissionError
  44. }