Read about our upcoming Code of Conduct on this issue

group_projects_finder.rb 2.41 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
5
6
7
8
9
10
11
12
13
# GroupProjectsFinder
#
# Used to filter Projects  by set of params
#
# Arguments:
#   current_user - which user use
#   project_ids_relation: int[] - project ids to use
#   group
#   options:
#     only_owned: boolean
#     only_shared: boolean
14
#     limit: integer
15
16
17
18
19
20
21
22
23
#   params:
#     sort: string
#     visibility_level: int
#     tags: string[]
#     personal: boolean
#     search: string
#     non_archived: boolean
#
class GroupProjectsFinder < ProjectsFinder
24
25
  DEFAULT_PROJECTS_LIMIT = 100

26
27
28
  attr_reader :group, :options

  def initialize(group:, params: {}, options: {}, current_user: nil, project_ids_relation: nil)
29
30
31
32
33
34
    super(
      params: params,
      current_user: current_user,
      project_ids_relation: project_ids_relation
    )
    @group = group
Douwe Maan's avatar
Douwe Maan committed
35
36
37
    @options = options
  end

38
39
40
41
42
  def execute
    collection = super
    limit(collection)
  end

Douwe Maan's avatar
Douwe Maan committed
43
44
  private

45
46
47
48
49
50
  def limit(collection)
    limit = options[:limit]

    limit.present? ? collection.with_limit(limit) : collection
  end

51
  def init_collection
52
53
54
55
56
    projects = if current_user
                 collection_with_user
               else
                 collection_without_user
               end
57

58
59
    union(projects)
  end
Douwe Maan's avatar
Douwe Maan committed
60

61
  def collection_with_user
62
63
64
65
    if only_shared?
      [shared_projects.public_or_visible_to_user(current_user)]
    elsif only_owned?
      [owned_projects.public_or_visible_to_user(current_user)]
Douwe Maan's avatar
Douwe Maan committed
66
    else
67
68
69
70
      [
        owned_projects.public_or_visible_to_user(current_user),
        shared_projects.public_or_visible_to_user(current_user)
      ]
Douwe Maan's avatar
Douwe Maan committed
71
    end
72
  end
Douwe Maan's avatar
Douwe Maan committed
73

74
75
76
77
78
79
80
81
  def collection_without_user
    if only_shared?
      [shared_projects.public_only]
    elsif only_owned?
      [owned_projects.public_only]
    else
      [shared_projects.public_only, owned_projects.public_only]
    end
Douwe Maan's avatar
Douwe Maan committed
82
  end
83
84

  def union(items)
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    if items.one?
      items.first
    else
      find_union(items, Project)
    end
  end

  def only_owned?
    options.fetch(:only_owned, false)
  end

  def only_shared?
    options.fetch(:only_shared, false)
  end

100
101
102
103
104
  # subgroups are supported only for owned projects not for shared
  def include_subgroups?
    options.fetch(:include_subgroups, false)
  end

105
  def owned_projects
106
    if include_subgroups?
107
      Project.for_group_and_its_subgroups(group)
108
109
110
    else
      group.projects
    end
111
112
113
114
  end

  def shared_projects
    group.shared_projects
115
  end
Douwe Maan's avatar
Douwe Maan committed
116
end
117
118

GroupProjectsFinder.prepend_if_ee('EE::GroupProjectsFinder')