diff options
author | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-09-07 13:29:19 +0100 |
---|---|---|
committer | Tiago Botelho <tiagonbotelho@hotmail.com> | 2018-09-24 12:02:01 +0100 |
commit | 81d949f656979429835e0b6059a10a44faba488b (patch) | |
tree | 671a8927eab32278c656cb24e220ea4df41abd30 /app/models/group.rb | |
parent | 4ce9f2fdfb6d135e6229675b9965c1b90efdfcfe (diff) | |
download | gitlab-ce-81d949f656979429835e0b6059a10a44faba488b.tar.gz |
Applies the CE backport of EE#657
Diffstat (limited to 'app/models/group.rb')
-rw-r--r-- | app/models/group.rb | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/app/models/group.rb b/app/models/group.rb index 62af20d2142..612c546ca57 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -82,8 +82,17 @@ class Group < Namespace User.reference_pattern end - def visible_to_user(user) - where(id: user.authorized_groups.select(:id).reorder(nil)) + # WARNING: This method should never be used on its own + # please do make sure the number of rows you are filtering is small + # enough for this query + def public_or_visible_to_user(user) + return public_to_user unless user + + public_for_user = public_to_user_arel(user) + visible_for_user = visible_to_user_arel(user) + public_or_visible = public_for_user.or(visible_for_user) + + where(public_or_visible) end def select_for_project_authorization @@ -95,6 +104,23 @@ class Group < Namespace super end end + + private + + def public_to_user_arel(user) + self.arel_table[:visibility_level] + .in(Gitlab::VisibilityLevel.levels_for_user(user)) + end + + def visible_to_user_arel(user) + groups_table = self.arel_table + authorized_groups = user.authorized_groups.as('authorized') + + groups_table.project(1) + .from(authorized_groups) + .where(authorized_groups[:id].eq(groups_table[:id])) + .exists + end end # Overrides notification_settings has_many association |