diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/member.rb | 1 | ||||
-rw-r--r-- | app/models/user.rb | 13 |
2 files changed, 11 insertions, 3 deletions
diff --git a/app/models/member.rb b/app/models/member.rb index a5084c8a60c..45ad47f56a4 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -170,6 +170,7 @@ class Member < ApplicationRecord scope :owners_and_maintainers, -> { active.where(access_level: [OWNER, MAINTAINER]) } scope :with_user, -> (user) { where(user: user) } scope :by_access_level, -> (access_level) { active.where(access_level: access_level) } + scope :all_by_access_level, -> (access_level) { where(access_level: access_level) } scope :preload_user_and_notification_settings, -> { preload(user: :notification_settings) } diff --git a/app/models/user.rb b/app/models/user.rb index 8aae4441852..b9a8e5855bf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1366,9 +1366,16 @@ class User < ApplicationRecord end def solo_owned_groups - @solo_owned_groups ||= owned_groups.includes(:owners).select do |group| - group.owners == [self] - end + # For each owned group, count the owners found in self and ancestors. + counts = GroupMember + .from('unnest(namespaces.traversal_ids) AS ancestors(ancestor_id), members') + .where('members.source_id = ancestors.ancestor_id') + .all_by_access_level(GroupMember::OWNER) + .having('count(members.user_id) = 1') + + Group + .from(owned_groups, :namespaces) + .where_exists(counts) end def with_defaults |