gitlab-org--gitlab-foss/app/finders/group_members_finder.rb
2018-06-07 20:54:24 +00:00

32 lines
897 B
Ruby

class GroupMembersFinder
def initialize(group)
@group = group
end
def execute(include_descendants: false)
group_members = @group.members
wheres = []
return group_members unless @group.parent || include_descendants
wheres << "members.id IN (#{group_members.select(:id).to_sql})"
if @group.parent
parents_members = GroupMember.non_request
.where(source_id: @group.ancestors.select(:id))
.where.not(user_id: @group.users.select(:id))
wheres << "members.id IN (#{parents_members.select(:id).to_sql})"
end
if include_descendants
descendant_members = GroupMember.non_request
.where(source_id: @group.descendants.select(:id))
.where.not(user_id: @group.users.select(:id))
wheres << "members.id IN (#{descendant_members.select(:id).to_sql})"
end
GroupMember.where(wheres.join(' OR '))
end
end