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

base_save_service.rb « pipeline_schedules « ci « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 45d70e5a65d9c422843cde4d9ae1c399b9faa82f (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 Ci
  module PipelineSchedules
    class BaseSaveService
      include Gitlab::Utils::StrongMemoize

      def execute
        schedule.assign_attributes(params)

        return forbidden_to_save unless allowed_to_save?
        return forbidden_to_save_variables unless allowed_to_save_variables?

        if schedule.save
          ServiceResponse.success(payload: schedule)
        else
          ServiceResponse.error(payload: schedule, message: schedule.errors.full_messages)
        end
      end

      private

      attr_reader :project, :user, :params, :schedule

      def allowed_to_save?
        user.can?(self.class::AUTHORIZE, schedule)
      end

      def forbidden_to_save
        # We add the error to the base object too
        # because model errors are used in the API responses and the `form_errors` helper.
        schedule.errors.add(:base, authorize_message)

        ServiceResponse.error(payload: schedule, message: [authorize_message], reason: :forbidden)
      end

      def allowed_to_save_variables?
        return true if params[:variables_attributes].blank?

        user.can?(:set_pipeline_variables, project)
      end

      def forbidden_to_save_variables
        message = _('The current user is not authorized to set pipeline schedule variables')

        # We add the error to the base object too
        # because model errors are used in the API responses and the `form_errors` helper.
        schedule.errors.add(:base, message)

        ServiceResponse.error(payload: schedule, message: [message], reason: :forbidden)
      end
    end
  end
end