summaryrefslogtreecommitdiff
path: root/app/finders/personal_projects_finder.rb
blob: a61ffa229900574be7d1e0c8304b4c29baa7d780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class PersonalProjectsFinder
  def initialize(user)
    @user = user
  end

  # Finds the projects belonging to the user in "@user", limited to either
  # public projects or projects visible to the given user.
  #
  # current_user - When given the list of projects is limited to those only
  #                visible by this user.
  #
  # Returns an ActiveRecord::Relation.
  def execute(current_user = nil)
    if current_user
      relation = projects_visible_to_user(current_user)
    else
      relation = public_projects
    end

    relation.includes(:namespace).order_id_desc
  end

  private

  def projects_visible_to_user(current_user)
    authorized = @user.personal_projects.visible_to_user(current_user)

    union = Gitlab::SQL::Union.
      new([authorized.select(:id), public_and_internal_projects.select(:id)])

    Project.where("projects.id IN (#{union.to_sql})")
  end

  def public_projects
    @user.personal_projects.public_only
  end

  def public_and_internal_projects
    @user.personal_projects.public_and_internal_only
  end
end