scope.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rbacscope
  15. import (
  16. "strings"
  17. )
  18. type TRbacScope string
  19. const (
  20. ScopeSystem = TRbacScope("system")
  21. ScopeDomain = TRbacScope("domain")
  22. ScopeProject = TRbacScope("project")
  23. // ScopeObject = "object"
  24. ScopeUser = TRbacScope("user")
  25. ScopeNone = TRbacScope("none")
  26. )
  27. var (
  28. scopeScore = map[TRbacScope]int{
  29. ScopeNone: 0,
  30. ScopeUser: 1,
  31. ScopeProject: 2,
  32. ScopeDomain: 3,
  33. ScopeSystem: 4,
  34. }
  35. )
  36. func (s1 TRbacScope) HigherEqual(s2 TRbacScope) bool {
  37. return scopeScore[s1] >= scopeScore[s2]
  38. }
  39. func (s1 TRbacScope) HigherThan(s2 TRbacScope) bool {
  40. return scopeScore[s1] > scopeScore[s2]
  41. }
  42. func String2Scope(str string) TRbacScope {
  43. return String2ScopeDefault(str, ScopeProject)
  44. }
  45. func String2ScopeDefault(str string, defScope TRbacScope) TRbacScope {
  46. switch strings.ToLower(str) {
  47. case string(ScopeSystem):
  48. return ScopeSystem
  49. case string(ScopeDomain):
  50. return ScopeDomain
  51. case string(ScopeProject):
  52. return ScopeProject
  53. case string(ScopeUser):
  54. return ScopeUser
  55. case "true":
  56. return ScopeSystem
  57. default:
  58. return defScope
  59. }
  60. }