summaryrefslogtreecommitdiff
path: root/app/finders/projects/groups_finder.rb
blob: d0c42ad5611dfb3e58991a55058e3c0b6671fb81 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true

# Used to filter ancestor and shared project's Groups by a set of params
#
# Arguments:
#   project
#   current_user - which user is requesting groups
#   params:
#     with_shared: boolean (optional)
#     shared_min_access_level: integer (optional)
#     skip_groups: array of integers (optional)
#
module Projects
  class GroupsFinder < UnionFinder
    def initialize(project:, current_user: nil, params: {})
      @project = project
      @current_user = current_user
      @params = params
    end

    def execute
      return Group.none unless authorized?

      items = all_groups.map do |item|
        item = exclude_group_ids(item)
        item
      end

      find_union(items, Group).with_route.order_id_desc
    end

    private

    attr_reader :project, :current_user, :params

    def authorized?
      Ability.allowed?(current_user, :read_project, project)
    end

    # rubocop: disable CodeReuse/ActiveRecord
    def all_groups
      groups = []
      groups << project.group.self_and_ancestors if project.group

      if params[:with_shared]
        shared_groups = project.invited_groups

        if params[:shared_min_access_level]
          shared_groups = shared_groups.where(
            'project_group_links.group_access >= ?', params[:shared_min_access_level]
          )
        end

        groups << shared_groups
      end

      groups << Group.none if groups.compact.empty?
      groups
    end
    # rubocop: enable CodeReuse/ActiveRecord

    def exclude_group_ids(groups)
      return groups unless params[:skip_groups]

      groups.id_not_in(params[:skip_groups])
    end
  end
end