2020-01-16 19:09:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Snippets
|
|
|
|
class DestroyService
|
|
|
|
include Gitlab::Allowable
|
|
|
|
|
2020-02-27 19:09:08 -05:00
|
|
|
attr_reader :current_user, :snippet
|
|
|
|
|
|
|
|
DestroyError = Class.new(StandardError)
|
2020-01-16 19:09:00 -05:00
|
|
|
|
|
|
|
def initialize(user, snippet)
|
|
|
|
@current_user = user
|
|
|
|
@snippet = snippet
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
if snippet.nil?
|
|
|
|
return service_response_error('No snippet found.', 404)
|
|
|
|
end
|
|
|
|
|
|
|
|
unless user_can_delete_snippet?
|
|
|
|
return service_response_error(
|
|
|
|
"You don't have access to delete this snippet.",
|
|
|
|
403
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-27 19:09:08 -05:00
|
|
|
attempt_destroy!
|
|
|
|
|
|
|
|
ServiceResponse.success(message: 'Snippet was deleted.')
|
|
|
|
rescue DestroyError
|
|
|
|
service_response_error('Failed to remove snippet repository.', 400)
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2020-02-27 19:09:08 -05:00
|
|
|
service_response_error('Failed to remove snippet.', 400)
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-02-27 19:09:08 -05:00
|
|
|
def attempt_destroy!
|
|
|
|
result = Repositories::DestroyService.new(snippet.repository).execute
|
|
|
|
|
|
|
|
raise DestroyError if result[:status] == :error
|
|
|
|
|
|
|
|
snippet.destroy!
|
|
|
|
end
|
|
|
|
|
2020-01-16 19:09:00 -05:00
|
|
|
def user_can_delete_snippet?
|
2020-01-23 07:08:38 -05:00
|
|
|
can?(current_user, :admin_snippet, snippet)
|
2020-01-16 19:09:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def service_response_error(message, http_status)
|
|
|
|
ServiceResponse.error(message: message, http_status: http_status)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-11 17:08:44 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Snippets::DestroyService.prepend_mod_with('Snippets::DestroyService')
|