Welcome to mirror list, hosted at ThFree Co, Russian Federation.

validate_management_project_permissions_service.rb « management « clusters « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e407c159bc7e7621f924a55f64646e8073ad75c2 (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
# frozen_string_literal: true

module Clusters
  module Management
    class ValidateManagementProjectPermissionsService
      attr_reader :current_user

      def initialize(user = nil)
        @current_user = user
      end

      def execute(cluster, management_project_id)
        if management_project_id.present?
          management_project = management_project_scope(cluster).find_by_id(management_project_id)

          unless management_project && can_admin_pipeline_for_project?(management_project)
            cluster.errors.add(:management_project_id, _('Project does not exist or you don\'t have permission to perform this action'))

            return false
          end
        end

        true
      end

      private

      def can_admin_pipeline_for_project?(project)
        Ability.allowed?(current_user, :admin_pipeline, project)
      end

      def management_project_scope(cluster)
        return ::Project.all if cluster.instance_type?

        group =
          if cluster.group_type?
            cluster.first_group
          elsif cluster.project_type?
            cluster.first_project&.namespace
          end

        # Prevent users from selecting nested projects until
        # https://gitlab.com/gitlab-org/gitlab/issues/34650 is resolved
        include_subgroups = cluster.group_type?

        ::GroupProjectsFinder.new(
          group: group,
          current_user: current_user,
          options: { exclude_shared: true, include_subgroups: include_subgroups }
        ).execute
      end
    end
  end
end