diff options
author | Rubén Dávila <ruben@gitlab.com> | 2017-08-23 19:51:11 +0300 |
---|---|---|
committer | Mike Greiling <mike@pixelcog.com> | 2017-08-26 11:30:01 +0300 |
commit | b2b9d63f9b7301cbef9d1e1b8d4ad3cefeacf35d (patch) | |
tree | c91cefa8b0d2afd67eeffd818415818b8f8fc972 /app/models/group.rb | |
parent | d413f8e4e426e2cb2dc61d5a72d84a7dc67a28c8 (diff) |
Add validation to check visibility level of sub groups.
Diffstat (limited to 'app/models/group.rb')
-rw-r--r-- | app/models/group.rb | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/app/models/group.rb b/app/models/group.rb index 15355418d05..fdd175341b3 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -26,6 +26,7 @@ class Group < Namespace validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? } validate :visibility_level_allowed_by_projects + validate :visibility_level_allowed_by_sub_groups, if: :visibility_level_changed? validate :visibility_level_allowed_by_parent validates :avatar, file_size: { maximum: 200.kilobytes.to_i } @@ -112,14 +113,25 @@ class Group < Namespace end def visibility_level_allowed_by_projects - allowed_by_projects = self.projects.where('visibility_level > ?', self.visibility_level).none? + check_visibility_level_for(:projects) + end + + def visibility_level_allowed_by_sub_groups + check_visibility_level_for(:children) + end - unless allowed_by_projects + def check_visibility_level_for(children_type) + base_query = public_send(children_type) + children_have_higher_visibility = base_query.where('visibility_level > ?', visibility_level).exists? + + if children_have_higher_visibility + children_label = children_type == :projects ? 'projects' : 'sub groups' level_name = Gitlab::VisibilityLevel.level_name(visibility_level).downcase - self.errors.add(:visibility_level, "#{level_name} is not allowed since there are projects with higher visibility.") + + self.errors.add(:visibility_level, "#{level_name} is not allowed since there are #{children_label} with higher visibility.") end - allowed_by_projects + children_have_higher_visibility end def avatar_url(**args) |