trabajo-afectivo/app/controllers/import_otrs_controller.rb

131 lines
3 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 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
if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
render json: {
result: 'invalid',
message: 'Invalid URL!',
2014-10-26 12:13:44 +00:00
}
return
end
# connection test
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!',
}
response = UserAgent.request( params[:url] )
2014-10-26 12:13:44 +00:00
if !response.success? && response.code.to_s !~ /^40.$/
message_human = ''
2016-06-30 20:04:48 +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
}
render json: {
result: 'invalid',
message_human: message_human,
message: response.error.to_s,
2014-10-26 12:13:44 +00:00
}
return
end
result = {}
if response.body =~ /zammad migrator/
migrator_response = JSON.parse(response.body)
2014-10-26 12:13:44 +00:00
if migrator_response['Success'] == 1
2014-10-26 12:13:44 +00:00
url_parts = params[:url].split(';')
key_parts = url_parts[1].split('=')
Setting.set('import_backend', 'otrs')
Setting.set('import_otrs_endpoint', url_parts[0])
Setting.set('import_otrs_endpoint_key', key_parts[1])
result = {
result: 'ok',
url: params[:url],
}
else
result = {
result: 'invalid',
message_human: migrator_response['Error']
}
end
elsif response.body =~ /(otrs\sag|otrs\.com|otrs\.org)/i
result = {
result: 'invalid',
message_human: 'Host found, but no OTRS migrator is installed!'
}
else
result = {
result: 'invalid',
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
2014-11-01 12:32:30 +00:00
Setting.set('import_mode', true)
welcome = Import::OTRS.connection_test
2014-11-01 12:32:30 +00:00
if !welcome
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
2016-01-13 21:21:34 +00:00
Import::OTRS.delay.start_bg
2014-10-26 12:13:44 +00:00
render json: {
result: 'ok',
2014-10-26 12:13:44 +00:00
}
end
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
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
render json: {
setup_done: true,
2014-10-26 12:13:44 +00:00
}
true
end
end