debian-mirror-gitlab/app/services/snippets/destroy_service.rb

63 lines
1.5 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
module Snippets
class DestroyService
include Gitlab::Allowable
2020-04-08 14:13:33 +05:30
attr_reader :current_user, :snippet
DestroyError = Class.new(StandardError)
2020-03-13 15:44:24 +05:30
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-04-08 14:13:33 +05:30
attempt_destroy!
ServiceResponse.success(message: 'Snippet was deleted.')
rescue DestroyError
service_response_error('Failed to remove snippet repository.', 400)
rescue
attempt_rollback_repository
service_response_error('Failed to remove snippet.', 400)
2020-03-13 15:44:24 +05:30
end
private
2020-04-08 14:13:33 +05:30
def attempt_destroy!
result = Repositories::DestroyService.new(snippet.repository).execute
raise DestroyError if result[:status] == :error
snippet.destroy!
end
def attempt_rollback_repository
Repositories::DestroyRollbackService.new(snippet.repository).execute
end
2020-03-13 15:44:24 +05:30
def user_can_delete_snippet?
can?(current_user, :admin_snippet, snippet)
end
def service_response_error(message, http_status)
ServiceResponse.error(message: message, http_status: http_status)
end
end
end
2020-11-24 15:15:51 +05:30
Snippets::DestroyService.prepend_if_ee('EE::Snippets::DestroyService')