BigW Consortium Gitlab

group_projects_finder.rb 1.13 KB
Newer Older
1 2
class GroupProjectsFinder < UnionFinder
  def initialize(group, options = {})
3
    @group   = group
4 5 6 7 8 9 10 11 12 13 14
    @options = options
  end

  def execute(current_user = nil)
    segments = group_projects(current_user)
    find_union(segments, Project)
  end

  private

  def group_projects(current_user)
15
    only_owned  = @options.fetch(:only_owned, false)
Douwe Maan committed
16
    only_shared = @options.fetch(:only_shared, false)
17 18 19 20 21

    projects = []

    if current_user
      if @group.users.include?(current_user)
Douwe Maan committed
22 23
        projects << @group.projects unless only_shared
        projects << @group.shared_projects unless only_owned
24
      else
Douwe Maan committed
25
        unless only_shared
26 27 28 29
          projects << @group.projects.visible_to_user(current_user)
          projects << @group.projects.public_to_user(current_user)
        end

Douwe Maan committed
30
        unless only_owned
31 32 33 34 35
          projects << @group.shared_projects.visible_to_user(current_user)
          projects << @group.shared_projects.public_to_user(current_user)
        end
      end
    else
Douwe Maan committed
36 37
      projects << @group.projects.public_only unless only_shared
      projects << @group.shared_projects.public_only unless only_owned
38 39 40 41 42
    end

    projects
  end
end