results.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 rbacutils
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/util/tagutils"
  19. )
  20. type SPolicyMatch struct {
  21. Rule SRbacRule
  22. DomainTags tagutils.TTagSetList
  23. ProjectTags tagutils.TTagSetList
  24. ObjectTags tagutils.TTagSetList
  25. }
  26. type SPolicyResult struct {
  27. Result TRbacResult
  28. DomainTags tagutils.TTagSetList
  29. ProjectTags tagutils.TTagSetList
  30. ObjectTags tagutils.TTagSetList
  31. }
  32. var (
  33. PolicyDeny = SPolicyResult{
  34. Result: Deny,
  35. }
  36. PolicyAllow = SPolicyResult{
  37. Result: Allow,
  38. }
  39. )
  40. type TPolicyMatches []SPolicyMatch
  41. func (matches TPolicyMatches) GetResult() SPolicyResult {
  42. result := SPolicyResult{
  43. Result: Deny,
  44. }
  45. isWideDomainTag, isWideProjectTag, isWideObjectTag := false, false, false
  46. for _, match := range matches {
  47. if match.Rule.Result == Allow {
  48. result.Result = Allow
  49. result.DomainTags = result.DomainTags.AppendAll(match.DomainTags)
  50. result.ProjectTags = result.ProjectTags.AppendAll(match.ProjectTags)
  51. result.ObjectTags = result.ObjectTags.AppendAll(match.ObjectTags)
  52. if len(match.DomainTags) == 0 {
  53. isWideDomainTag = true
  54. }
  55. if len(match.ProjectTags) == 0 {
  56. isWideProjectTag = true
  57. }
  58. if len(match.ObjectTags) == 0 {
  59. isWideObjectTag = true
  60. }
  61. }
  62. }
  63. if isWideDomainTag {
  64. result.DomainTags = tagutils.TTagSetList{}
  65. }
  66. if isWideProjectTag {
  67. result.ProjectTags = tagutils.TTagSetList{}
  68. }
  69. if isWideObjectTag {
  70. result.ObjectTags = tagutils.TTagSetList{}
  71. }
  72. return result
  73. }
  74. func (result SPolicyResult) String() string {
  75. return fmt.Sprintf("[%s] domain:%s project:%s object:%s", result.Result, result.DomainTags.String(), result.ProjectTags.String(), result.ObjectTags.String())
  76. }
  77. func (result SPolicyResult) IsEmpty() bool {
  78. return len(result.ObjectTags) == 0 && len(result.ProjectTags) == 0 && len(result.DomainTags) == 0
  79. }
  80. func (result SPolicyResult) Json() jsonutils.JSONObject {
  81. ret := jsonutils.NewDict()
  82. if len(result.ObjectTags) > 0 {
  83. ret.Add(jsonutils.Marshal(result.ObjectTags), "policy_object_tags")
  84. }
  85. if len(result.ProjectTags) > 0 {
  86. ret.Add(jsonutils.Marshal(result.ProjectTags), "policy_project_tags")
  87. }
  88. if len(result.DomainTags) > 0 {
  89. ret.Add(jsonutils.Marshal(result.DomainTags), "policy_domain_tags")
  90. }
  91. return ret
  92. }
  93. func mergeTagList(t1, t2 tagutils.TTagSetList) tagutils.TTagSetList {
  94. return t1.IntersectList(t2)
  95. }
  96. func (r1 SPolicyResult) Merge(r2 SPolicyResult) SPolicyResult {
  97. if r1.Result.IsDeny() || r2.Result.IsDeny() {
  98. return SPolicyResult{Result: Deny}
  99. }
  100. r1.ProjectTags = mergeTagList(r1.ProjectTags, r2.ProjectTags)
  101. r1.DomainTags = mergeTagList(r1.DomainTags, r2.DomainTags)
  102. r1.ObjectTags = mergeTagList(r1.ObjectTags, r2.ObjectTags)
  103. return r1
  104. }