2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2016-01-18 19:32:34 +00:00
|
|
|
require 'zendesk_api'
|
|
|
|
|
|
|
|
class ImportZendeskController < ApplicationController
|
|
|
|
|
|
|
|
def url_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
# validate
|
|
|
|
if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message: 'Invalid URL!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# connection test
|
|
|
|
translation_map = {
|
|
|
|
'No such file' => 'Hostname not found!',
|
|
|
|
'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
|
|
|
|
'No route to host' => 'No route to host!',
|
|
|
|
'Connection refused' => 'Connection refused!',
|
|
|
|
}
|
|
|
|
|
2016-10-15 02:31:15 +00:00
|
|
|
response = UserAgent.request(params[:url])
|
2016-01-18 19:32:34 +00:00
|
|
|
|
|
|
|
if !response.success?
|
|
|
|
message_human = ''
|
2016-06-30 20:04:48 +00:00
|
|
|
translation_map.each { |key, message|
|
2016-01-18 19:32:34 +00:00
|
|
|
if response.error.to_s =~ /#{Regexp.escape(key)}/i
|
|
|
|
message_human = message
|
|
|
|
end
|
|
|
|
}
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: message_human,
|
|
|
|
message: response.error.to_s,
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-10-15 02:31:15 +00:00
|
|
|
# since 2016-10-15 a redirect to a signup page has been implemented
|
|
|
|
if response.body =~ /(Take it for a risk-free|Take it for a risk-free)/i
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: 'Hostname not found!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-08-01 21:35:54 +00:00
|
|
|
endpoint = "#{params[:url]}/api/v2"
|
2017-08-02 06:11:03 +00:00
|
|
|
endpoint.gsub(%r{[^:]//}, '/')
|
2017-08-01 21:35:54 +00:00
|
|
|
Setting.set('import_zendesk_endpoint', endpoint)
|
2016-01-18 19:32:34 +00:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
url: params[:url],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def credentials_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
if !params[:username] || !params[:token]
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: 'Incomplete credentials',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Setting.set('import_zendesk_endpoint_username', params[:username])
|
|
|
|
Setting.set('import_zendesk_endpoint_key', params[:token])
|
|
|
|
|
|
|
|
if !Import::Zendesk.connection_test
|
|
|
|
|
|
|
|
Setting.set('import_zendesk_endpoint_username', nil)
|
|
|
|
Setting.set('import_zendesk_endpoint_key', nil)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: 'Invalid credentials!',
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_start
|
|
|
|
return if setup_done_response
|
|
|
|
Setting.set('import_mode', true)
|
|
|
|
|
|
|
|
# start migration
|
|
|
|
Import::Zendesk.delay.start_bg
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_status
|
|
|
|
result = Import::Zendesk.status_bg
|
|
|
|
if result[:result] == 'import_done'
|
|
|
|
Setting.reload
|
|
|
|
end
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def setup_done
|
|
|
|
count = User.all.count()
|
|
|
|
done = true
|
|
|
|
if count <= 2
|
|
|
|
done = false
|
|
|
|
end
|
|
|
|
done
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup_done_response
|
|
|
|
if !setup_done
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
render json: {
|
|
|
|
setup_done: true,
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|