summaryrefslogtreecommitdiff
path: root/app/finders/groups/accepting_project_shares_finder.rb
blob: 253961b8e52b748bdf36626fa75da12abdf74636 (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
# frozen_string_literal: true

# AcceptingProjectSharesFinder
#
# Used to filter Shareable Groups by a set of params
#
# Arguments:
#   current_user - which user is requesting groups
#   params:
#     search: string
module Groups
  class AcceptingProjectSharesFinder < Base
    def initialize(current_user, project_to_be_shared, params = {})
      @current_user = current_user
      @params = params
      @project_to_be_shared = project_to_be_shared
    end

    def execute
      return Group.none unless can_share_project?

      groups = if has_admin_access?
                 Group.all
               else
                 groups_with_guest_access_plus
               end

      groups = by_search(groups)

      sort(groups).with_route
    end

    private

    attr_reader :current_user, :project_to_be_shared, :params

    def has_admin_access?
      current_user&.can_read_all_resources?
    end

    # rubocop: disable CodeReuse/Finder
    def groups_with_guest_access_plus
      GroupsFinder.new(current_user, min_access_level: Gitlab::Access::GUEST).execute
    end
    # rubocop: enable CodeReuse/Finder

    def can_share_project?
      Ability.allowed?(current_user, :admin_project, project_to_be_shared) &&
        project_to_be_shared.allowed_to_share_with_group?
    end
  end
end