blob: fb3ba52b36ee473dcb312db8ed4519c8931c972e (
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
module Ci
module PipelineSchedules
class VariablesBaseSaveService
include Gitlab::Utils::StrongMemoize
def execute
variable.assign_attributes(params)
return forbidden_to_update_pipeline_schedule unless allowed_to_update_pipeline_schedule?
return forbidden_to_save_variables unless allowed_to_save_variables?
if variable.save
ServiceResponse.success(payload: variable)
else
ServiceResponse.error(payload: variable, message: variable.errors.full_messages)
end
end
private
attr_reader :project, :user, :params, :variable, :pipeline_schedule
def allowed_to_update_pipeline_schedule?
user.can?(:update_pipeline_schedule, pipeline_schedule)
end
def forbidden_to_update_pipeline_schedule
# We add the error to the base object too
# because model errors are used in the API responses and the `form_errors` helper.
pipeline_schedule.errors.add(:base, authorize_message)
ServiceResponse.error(payload: pipeline_schedule, message: [authorize_message], reason: :forbidden)
end
def allowed_to_save_variables?
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.
pipeline_schedule.errors.add(:base, message)
ServiceResponse.error(payload: pipeline_schedule, message: [message], reason: :forbidden)
end
end
end
end
|