2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2014-10-26 12:13:44 +00:00
|
|
|
|
|
|
|
class ImportOtrsController < ApplicationController
|
|
|
|
|
|
|
|
def url_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
# validate
|
2015-05-01 12:12:37 +00:00
|
|
|
if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2016-01-13 12:07:23 +00:00
|
|
|
message: 'Invalid URL!',
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# connection test
|
2015-05-05 14:14:18 +00:00
|
|
|
translation_map = {
|
2014-10-26 12:13:44 +00:00
|
|
|
'authentication failed' => 'Authentication failed!',
|
|
|
|
'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
|
|
|
|
'No route to host' => 'No route to host!',
|
|
|
|
'Connection refused' => 'Connection refused!',
|
|
|
|
}
|
|
|
|
|
2017-01-15 10:47:20 +00:00
|
|
|
response = UserAgent.request(params[:url])
|
2021-05-12 11:37:44 +00:00
|
|
|
if !response.success? && response.code.to_s !~ %r{^40.$}
|
2014-10-26 12:13:44 +00:00
|
|
|
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)
|
2014-10-26 12:13:44 +00:00
|
|
|
message_human = message
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2015-04-27 13:42:53 +00:00
|
|
|
message_human: message_human,
|
2018-12-19 17:31:51 +00:00
|
|
|
message: response.error.to_s,
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-01-13 12:07:23 +00:00
|
|
|
result = {}
|
2020-07-07 06:30:20 +00:00
|
|
|
if response.body.include?('zammad migrator')
|
2016-01-13 12:07:23 +00:00
|
|
|
|
2016-01-13 16:14:28 +00:00
|
|
|
migrator_response = JSON.parse(response.body)
|
2014-10-26 12:13:44 +00:00
|
|
|
|
2016-01-13 16:14:28 +00:00
|
|
|
if migrator_response['Success'] == 1
|
2014-10-26 12:13:44 +00:00
|
|
|
|
2017-01-15 10:47:20 +00:00
|
|
|
# set url and key for import endpoint
|
|
|
|
url = migrator_response['URL']
|
|
|
|
key = migrator_response['Key']
|
|
|
|
|
|
|
|
# get first part url, used for import_otrs_endpoint
|
|
|
|
if !url || !key
|
|
|
|
url_parts = params[:url].split(';')
|
|
|
|
if !url_parts[1] # in case of & instead of ;
|
|
|
|
url_parts = params[:url].split('&')
|
|
|
|
end
|
|
|
|
key_parts = url_parts[1].split('=')
|
|
|
|
|
|
|
|
if !key_parts[1]
|
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2017-01-15 10:47:20 +00:00
|
|
|
message_human: 'Unable to get key from URL!'
|
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if !url
|
|
|
|
url = url_parts[0]
|
|
|
|
end
|
|
|
|
if !key
|
|
|
|
key = key_parts[1]
|
|
|
|
end
|
|
|
|
end
|
2016-01-13 16:14:28 +00:00
|
|
|
|
|
|
|
Setting.set('import_backend', 'otrs')
|
2017-01-15 10:47:20 +00:00
|
|
|
Setting.set('import_otrs_endpoint', url)
|
|
|
|
Setting.set('import_otrs_endpoint_key', key)
|
2016-01-13 16:14:28 +00:00
|
|
|
|
|
|
|
result = {
|
|
|
|
result: 'ok',
|
2018-12-19 17:31:51 +00:00
|
|
|
url: params[:url],
|
2016-01-13 16:14:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result = {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2016-01-13 16:14:28 +00:00
|
|
|
message_human: migrator_response['Error']
|
|
|
|
}
|
|
|
|
end
|
2021-05-12 11:37:44 +00:00
|
|
|
elsif response.body.match?(%r{(otrs\sag|otrs\.com|otrs\.org)}i)
|
2016-01-13 12:07:23 +00:00
|
|
|
result = {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2016-01-13 12:07:23 +00:00
|
|
|
message_human: 'Host found, but no OTRS migrator is installed!'
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2016-01-13 12:07:23 +00:00
|
|
|
message_human: 'Host found, but it seems to be no OTRS installation!',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: result
|
2014-10-26 12:13:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def import_start
|
|
|
|
return if setup_done_response
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2014-11-01 12:32:30 +00:00
|
|
|
Setting.set('import_mode', true)
|
2015-05-07 11:57:19 +00:00
|
|
|
welcome = Import::OTRS.connection_test
|
2014-11-01 12:32:30 +00:00
|
|
|
if !welcome
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
message: 'Migrator can\'t read OTRS output!',
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'invalid',
|
2014-11-01 12:32:30 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
2014-10-26 12:13:44 +00:00
|
|
|
|
2014-11-01 12:32:30 +00:00
|
|
|
# start migration
|
2020-10-27 15:36:38 +00:00
|
|
|
AsyncOtrsImportJob.perform_later
|
2014-10-26 12:13:44 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-01-15 10:47:20 +00:00
|
|
|
def import_check
|
2019-06-28 11:38:49 +00:00
|
|
|
Import::OTRS::Requester.list
|
2017-01-15 10:47:20 +00:00
|
|
|
issues = []
|
|
|
|
|
|
|
|
# check count of dynamic fields
|
|
|
|
dynamic_field_count = 0
|
|
|
|
dynamic_fields = Import::OTRS::Requester.load('DynamicField')
|
2017-10-01 12:25:52 +00:00
|
|
|
dynamic_fields.each do |dynamic_field|
|
2017-01-15 10:47:20 +00:00
|
|
|
next if dynamic_field['ValidID'].to_i != 1
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-15 10:47:20 +00:00
|
|
|
dynamic_field_count += 1
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-15 10:47:20 +00:00
|
|
|
if dynamic_field_count > 20
|
|
|
|
issues.push 'otrsDynamicFields'
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if process exsists
|
|
|
|
sys_configs = Import::OTRS::Requester.load('SysConfig')
|
2017-10-01 12:25:52 +00:00
|
|
|
sys_configs.each do |sys_config|
|
2017-01-15 10:47:20 +00:00
|
|
|
next if sys_config['Key'] != 'Process'
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-15 10:47:20 +00:00
|
|
|
issues.push 'otrsProcesses'
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-15 10:47:20 +00:00
|
|
|
|
|
|
|
result = 'ok'
|
2017-11-23 08:09:44 +00:00
|
|
|
if issues.present?
|
2017-01-15 10:47:20 +00:00
|
|
|
result = 'failed'
|
|
|
|
end
|
|
|
|
render json: {
|
|
|
|
result: result,
|
|
|
|
issues: issues,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-10-26 12:13:44 +00:00
|
|
|
def import_status
|
2016-01-13 19:45:51 +00:00
|
|
|
result = Import::OTRS.status_bg
|
2016-01-13 21:21:34 +00:00
|
|
|
if result[:result] == 'import_done'
|
2016-01-13 19:45:51 +00:00
|
|
|
Setting.reload
|
2016-01-13 13:12:05 +00:00
|
|
|
end
|
2016-01-13 19:45:51 +00:00
|
|
|
render json: result
|
2014-10-26 12:13:44 +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
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
setup_done: true,
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|