share.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 apis
  15. import (
  16. "yunion.io/x/pkg/util/rbacscope"
  17. "yunion.io/x/onecloud/pkg/util/stringutils2"
  18. )
  19. const (
  20. CLOUD_ACCOUNT_SHARE_MODE_ACCOUNT_DOMAIN = "account_domain"
  21. CLOUD_ACCOUNT_SHARE_MODE_SYSTEM = "system"
  22. CLOUD_ACCOUNT_SHARE_MODE_PROVIDER_DOMAIN = "provider_domain"
  23. )
  24. type SAccountShareInfo struct {
  25. IsPublic bool `json:"is_public"`
  26. PublicScope rbacscope.TRbacScope `json:"public_scope"`
  27. ShareMode string `json:"share_mode"`
  28. SharedDomains []string `json:"shared_domains"`
  29. }
  30. type SShareInfo struct {
  31. IsPublic bool `json:"is_public"`
  32. PublicScope rbacscope.TRbacScope `json:"public_scope"`
  33. SharedDomains []string `json:"shared_domains"`
  34. SharedProjects []string `json:"shared_projects"`
  35. }
  36. func (i SAccountShareInfo) GetProjectShareInfo() SShareInfo {
  37. ret := SShareInfo{}
  38. switch i.ShareMode {
  39. case CLOUD_ACCOUNT_SHARE_MODE_ACCOUNT_DOMAIN:
  40. ret.IsPublic = true
  41. ret.PublicScope = rbacscope.ScopeDomain
  42. case CLOUD_ACCOUNT_SHARE_MODE_PROVIDER_DOMAIN:
  43. ret.IsPublic = true
  44. ret.PublicScope = rbacscope.ScopeDomain
  45. case CLOUD_ACCOUNT_SHARE_MODE_SYSTEM:
  46. ret.IsPublic = true
  47. if i.IsPublic && i.PublicScope == rbacscope.ScopeSystem {
  48. ret.PublicScope = rbacscope.ScopeSystem
  49. } else {
  50. ret.PublicScope = rbacscope.ScopeDomain
  51. ret.SharedDomains = i.SharedDomains
  52. }
  53. }
  54. return ret
  55. }
  56. func (i SAccountShareInfo) GetDomainShareInfo() SShareInfo {
  57. ret := SShareInfo{}
  58. switch i.ShareMode {
  59. case CLOUD_ACCOUNT_SHARE_MODE_ACCOUNT_DOMAIN:
  60. ret.IsPublic = false
  61. ret.PublicScope = rbacscope.ScopeNone
  62. case CLOUD_ACCOUNT_SHARE_MODE_PROVIDER_DOMAIN:
  63. ret.IsPublic = false
  64. ret.PublicScope = rbacscope.ScopeNone
  65. case CLOUD_ACCOUNT_SHARE_MODE_SYSTEM:
  66. if i.IsPublic && i.PublicScope == rbacscope.ScopeSystem {
  67. ret.IsPublic = true
  68. ret.PublicScope = rbacscope.ScopeSystem
  69. } else if len(i.SharedDomains) > 0 {
  70. ret.IsPublic = true
  71. ret.PublicScope = rbacscope.ScopeDomain
  72. ret.SharedDomains = i.SharedDomains
  73. } else {
  74. ret.IsPublic = false
  75. ret.PublicScope = rbacscope.ScopeNone
  76. }
  77. }
  78. return ret
  79. }
  80. func (i SShareInfo) IsViolate(i2 SShareInfo) bool {
  81. if i.IsPublic && !i2.IsPublic {
  82. return true
  83. } else if !i.IsPublic && i2.IsPublic {
  84. return false
  85. }
  86. // is_public equals
  87. if i.PublicScope.HigherThan(i2.PublicScope) {
  88. return true
  89. } else if i2.PublicScope.HigherThan(i.PublicScope) {
  90. return false
  91. }
  92. // public_scope equals
  93. aNoB, _, bNoA := stringutils2.Split(stringutils2.NewSortedStrings(i.SharedDomains), stringutils2.NewSortedStrings(i2.SharedDomains))
  94. if len(aNoB) > 0 {
  95. return true
  96. } else if len(bNoA) > 0 {
  97. return false
  98. }
  99. // shared_domains equals
  100. aNoB, _, bNoA = stringutils2.Split(stringutils2.NewSortedStrings(i.SharedProjects), stringutils2.NewSortedStrings(i2.SharedProjects))
  101. if len(aNoB) > 0 {
  102. return true
  103. } else if len(bNoA) > 0 {
  104. return false
  105. }
  106. return false
  107. }
  108. func (i SShareInfo) Intersect(i2 SShareInfo) SShareInfo {
  109. if i.IsPublic && !i2.IsPublic {
  110. return i2
  111. } else if !i.IsPublic && i2.IsPublic {
  112. return i
  113. }
  114. // is_public equals
  115. if i.PublicScope.HigherThan(i2.PublicScope) {
  116. return i2
  117. } else if i2.PublicScope.HigherThan(i.PublicScope) {
  118. return i
  119. }
  120. // public_scope equals
  121. _, domains, _ := stringutils2.Split(stringutils2.NewSortedStrings(i.SharedDomains), stringutils2.NewSortedStrings(i2.SharedDomains))
  122. _, projs, _ := stringutils2.Split(stringutils2.NewSortedStrings(i.SharedProjects), stringutils2.NewSortedStrings(i2.SharedProjects))
  123. ret := SShareInfo{
  124. IsPublic: i.IsPublic,
  125. PublicScope: i.PublicScope,
  126. SharedDomains: domains,
  127. SharedProjects: projs,
  128. }
  129. if ret.PublicScope == rbacscope.ScopeProject && len(ret.SharedProjects) == 0 {
  130. ret.IsPublic = false
  131. ret.PublicScope = rbacscope.ScopeNone
  132. }
  133. return ret
  134. }
  135. func (i SShareInfo) Equals(i2 SShareInfo) bool {
  136. if !i.IsViolate(i2) && !i2.IsViolate(i) {
  137. return true
  138. } else {
  139. return false
  140. }
  141. }
  142. func (i *SShareInfo) FixProjectShare() {
  143. if i.PublicScope == rbacscope.ScopeProject && len(i.SharedProjects) == 0 {
  144. i.IsPublic = false
  145. i.PublicScope = rbacscope.ScopeNone
  146. }
  147. }
  148. func (i *SShareInfo) FixDomainShare() {
  149. if i.PublicScope == rbacscope.ScopeProject {
  150. i.IsPublic = false
  151. i.PublicScope = rbacscope.ScopeNone
  152. i.SharedProjects = nil
  153. } else if i.PublicScope == rbacscope.ScopeDomain && len(i.SharedDomains) == 0 {
  154. i.IsPublic = false
  155. i.PublicScope = rbacscope.ScopeNone
  156. }
  157. }