2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2016-01-18 19:32:34 +00:00
|
|
|
class ImportZendeskController < ApplicationController
|
|
|
|
|
|
|
|
def url_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
# validate
|
2017-08-17 11:48:55 +00:00
|
|
|
if params[:url].blank? || params[:url] !~ %r{^(http|https)://.+?$}
|
2016-01-18 19:32:34 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('Invalid URL!'),
|
2016-01-18 19:32:34 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# connection test
|
|
|
|
translation_map = {
|
2021-11-15 15:58:19 +00:00
|
|
|
'No such file' => __('Hostname not found!'),
|
|
|
|
'getaddrinfo: nodename nor servname provided, or not known' => __('Hostname not found!'),
|
|
|
|
'503 Service Temporarily Unavailable' => __('Hostname not found!'),
|
|
|
|
'No route to host' => __('No route to host!'),
|
|
|
|
'Connection refused' => __('Connection refused!'),
|
2016-01-18 19:32:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 06:53:18 +00:00
|
|
|
response = UserAgent.request(URI.join(params[:url], '/api/v2/users/me').to_s, verify_ssl: true)
|
2016-01-18 19:32:34 +00:00
|
|
|
|
|
|
|
if !response.success?
|
|
|
|
message_human = ''
|
2017-10-01 12:25:52 +00:00
|
|
|
translation_map.each do |key, message|
|
2021-05-12 11:37:44 +00:00
|
|
|
if response.error.to_s.match?(%r{#{Regexp.escape(key)}}i)
|
2016-01-18 19:32:34 +00:00
|
|
|
message_human = message
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-01-18 19:32:34 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2016-01-18 19:32:34 +00:00
|
|
|
message_human: message_human,
|
2018-12-19 17:31:51 +00:00
|
|
|
message: response.error.to_s,
|
2016-01-18 19:32:34 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2021-07-28 06:53:18 +00:00
|
|
|
if response.header['x-zendesk-api-version'].blank?
|
2016-10-15 02:31:15 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Hostname not found!'),
|
2016-10-15 02:31:15 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2017-08-01 21:35:54 +00:00
|
|
|
endpoint = "#{params[:url]}/api/v2"
|
2017-08-17 11:48:55 +00:00
|
|
|
endpoint.gsub!(%r{([^:])//+}, '\\1/')
|
2021-05-25 12:30:12 +00:00
|
|
|
|
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',
|
2018-12-19 17:31:51 +00:00
|
|
|
url: params[:url],
|
2016-01-18 19:32:34 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def credentials_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
if !params[:username] || !params[:token]
|
|
|
|
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Incomplete credentials'),
|
2016-01-18 19:32:34 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
Setting.set('import_zendesk_endpoint_username', params[:username])
|
|
|
|
Setting.set('import_zendesk_endpoint_key', params[:token])
|
|
|
|
|
2018-01-08 15:29:34 +00:00
|
|
|
result = Sequencer.process('Import::Zendesk::ConnectionTest')
|
|
|
|
|
|
|
|
if !result[:connected]
|
2016-01-18 19:32:34 +00:00
|
|
|
|
|
|
|
Setting.set('import_zendesk_endpoint_username', nil)
|
|
|
|
Setting.set('import_zendesk_endpoint_key', nil)
|
|
|
|
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Invalid credentials!'),
|
2016-01-18 19:32:34 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_start
|
|
|
|
return if setup_done_response
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-18 19:32:34 +00:00
|
|
|
Setting.set('import_mode', true)
|
2017-08-17 11:48:55 +00:00
|
|
|
Setting.set('import_backend', 'zendesk')
|
2016-01-18 19:32:34 +00:00
|
|
|
|
2018-01-08 15:29:34 +00:00
|
|
|
job = ImportJob.create(name: 'Import::Zendesk')
|
2020-10-27 15:36:38 +00:00
|
|
|
AsyncImportJob.perform_later(job)
|
2016-01-18 19:32:34 +00:00
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_status
|
2018-01-08 15:29:34 +00:00
|
|
|
job = ImportJob.find_by(name: 'Import::Zendesk')
|
|
|
|
|
|
|
|
if job.finished_at.present?
|
2016-01-18 19:32:34 +00:00
|
|
|
Setting.reload
|
|
|
|
end
|
2018-01-08 15:29:34 +00:00
|
|
|
|
|
|
|
model_show_render_item(job)
|
2016-01-18 19:32:34 +00:00
|
|
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-01-18 19:32:34 +00:00
|
|
|
render json: {
|
|
|
|
setup_done: true,
|
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|