2014-10-26 12:13:44 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
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: {
|
|
|
|
result: 'invalid',
|
|
|
|
message: 'Invalid!',
|
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!',
|
|
|
|
}
|
|
|
|
|
|
|
|
# try connecting to otrs
|
|
|
|
response = UserAgent.request(params[:url])
|
|
|
|
if !response.success? && response.code.to_s !~ /^40.$/
|
|
|
|
message_human = ''
|
2015-05-05 14:14:18 +00:00
|
|
|
translation_map.each {|key, message|
|
2014-10-26 12:13:44 +00:00
|
|
|
if response.error.to_s =~ /#{Regexp.escape(key)}/i
|
|
|
|
message_human = message
|
|
|
|
end
|
|
|
|
}
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message_human: message_human,
|
|
|
|
message: response.error.to_s,
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
message_human = 'Host found, but it seems to be no OTRS installation!'
|
|
|
|
suffixes = ['/public.pl', '/otrs/public.pl']
|
|
|
|
suffixes.each {|suffix|
|
|
|
|
url = params[:url] + suffix + '?Action=ZammadMigrator'
|
|
|
|
# strip multible / in url
|
2015-05-01 12:12:37 +00:00
|
|
|
url.gsub!(%r{([^:])(/+/)}, '\\1/')
|
2014-10-26 12:13:44 +00:00
|
|
|
response = UserAgent.request( url )
|
|
|
|
|
2014-11-04 09:06:41 +00:00
|
|
|
#Setting.set('import_mode', true)
|
|
|
|
Setting.set('import_backend', 'otrs')
|
2014-10-26 12:13:44 +00:00
|
|
|
Setting.set('import_otrs_endpoint', url)
|
2014-11-01 12:32:30 +00:00
|
|
|
Setting.set('import_otrs_endpoint_key', '01234567899876543210')
|
2014-10-26 12:13:44 +00:00
|
|
|
if response.body =~ /zammad migrator/
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
url: url,
|
|
|
|
result: 'ok',
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
2015-05-05 14:36:05 +00:00
|
|
|
return # rubocop:disable Lint/NonLocalExitFromIterator
|
2014-10-26 12:13:44 +00:00
|
|
|
elsif response.body =~ /(otrs\sag|otrs.com|otrs.org)/i
|
|
|
|
message_human = 'Host found, but no OTRS migrator is installed!'
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# return result
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
2015-04-27 15:21:17 +00:00
|
|
|
result: 'invalid',
|
2015-05-01 12:02:27 +00:00
|
|
|
message_human: message_human,
|
2014-10-26 12:13:44 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_start
|
|
|
|
return if setup_done_response
|
|
|
|
|
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!',
|
|
|
|
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
|
2015-05-07 11:57:19 +00:00
|
|
|
Import::OTRS.delay.start
|
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
|
|
|
|
|
|
|
|
def import_status
|
|
|
|
return if setup_done_response
|
|
|
|
|
2015-05-07 11:57:19 +00:00
|
|
|
state = Import::OTRS.current_state
|
2014-10-26 12:13:44 +00:00
|
|
|
|
2015-04-27 13:42:53 +00:00
|
|
|
render json: {
|
|
|
|
data: state,
|
|
|
|
result: 'in_progress',
|
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
|
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
|