debian-mirror-gitlab/app/services/alert_management/http_integrations/update_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

60 lines
1.5 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module AlertManagement
module HttpIntegrations
class UpdateService
# @param integration [AlertManagement::HttpIntegration]
# @param current_user [User]
# @param params [Hash]
def initialize(integration, current_user, params)
@integration = integration
@current_user = current_user
2021-03-11 19:13:27 +05:30
@params = params.with_indifferent_access
2021-01-29 00:20:46 +05:30
end
def execute
return error_no_permissions unless allowed?
params[:token] = nil if params.delete(:regenerate_token)
2021-03-11 19:13:27 +05:30
if integration.update(permitted_params)
2021-01-29 00:20:46 +05:30
success
else
error(integration.errors.full_messages.to_sentence)
end
end
private
attr_reader :integration, :current_user, :params
def allowed?
current_user&.can?(:admin_operations, integration)
end
2021-03-11 19:13:27 +05:30
def permitted_params
params.slice(*permitted_params_keys)
end
# overriden in EE
def permitted_params_keys
%i[name active token]
end
2021-01-29 00:20:46 +05:30
def error(message)
ServiceResponse.error(message: message)
end
def success
ServiceResponse.success(payload: { integration: integration.reset })
end
def error_no_permissions
error(_('You have insufficient permissions to update this HTTP integration'))
end
end
end
end
2021-03-11 19:13:27 +05:30
2021-06-08 01:23:25 +05:30
::AlertManagement::HttpIntegrations::UpdateService.prepend_mod_with('AlertManagement::HttpIntegrations::UpdateService')