2021-10-20 09:43:35 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class ImportKayakoController < ApplicationController
|
|
|
|
def url_check
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
url = params[:url]
|
|
|
|
|
|
|
|
# validate
|
|
|
|
if !valid_url_syntax?(url)
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message: __('Invalid URL!'),
|
2021-10-20 09:43:35 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
endpoint = build_endpoint_url(url)
|
|
|
|
|
|
|
|
return if !valid_endpoint?(endpoint)
|
|
|
|
|
|
|
|
Setting.set('import_kayako_endpoint', endpoint)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
url: url,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-11-15 15:58:19 +00:00
|
|
|
def credentials_check # rubocop:disable Metrics/AbcSize
|
2021-10-20 09:43:35 +00:00
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
if !params[:username] || !params[:password]
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Incomplete credentials'),
|
2021-10-20 09:43:35 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
save_endpoint_settings(params[:username], params[:password])
|
|
|
|
|
|
|
|
return if !valid_connection?
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_start
|
|
|
|
return if setup_done_response
|
|
|
|
|
|
|
|
Setting.set('import_mode', true)
|
|
|
|
Setting.set('import_backend', 'kayako')
|
|
|
|
|
|
|
|
job = ImportJob.create(name: 'Import::Kayako')
|
|
|
|
AsyncImportJob.perform_later(job)
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'ok',
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_status
|
|
|
|
job = ImportJob.find_by(name: 'Import::Kayako')
|
|
|
|
|
|
|
|
if job.finished_at.present?
|
|
|
|
Setting.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
model_show_render_item(job)
|
|
|
|
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
|
|
|
|
|
|
|
|
def valid_url_syntax?(url)
|
|
|
|
return false if url.blank? || url !~ %r{^(http|https)://.+?$}
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_endpoint?(endpoint)
|
|
|
|
response = UserAgent.request("#{endpoint}/teams", verify_ssl: true)
|
|
|
|
|
|
|
|
if response.header.nil? || !response.header['x-api-version']
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
|
|
|
message: response.error.to_s,
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Hostname not found!'),
|
2021-10-20 09:43:35 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_endpoint_url(url)
|
|
|
|
endpoint = "#{url}/api/v1"
|
|
|
|
endpoint.gsub(%r{([^:])//+}, '\\1/')
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_connection?
|
|
|
|
result = Sequencer.process('Import::Kayako::ConnectionTest')
|
|
|
|
|
|
|
|
if !result[:connected]
|
|
|
|
reset_endpoint_settings
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
result: 'invalid',
|
2021-11-15 15:58:19 +00:00
|
|
|
message_human: __('Invalid credentials!'),
|
2021-10-20 09:43:35 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_endpoint_settings(username, possword)
|
|
|
|
Setting.set('import_kayako_endpoint_username', username)
|
|
|
|
Setting.set('import_kayako_endpoint_password', possword)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_endpoint_settings
|
|
|
|
save_endpoint_settings(nil, nil)
|
|
|
|
end
|
|
|
|
end
|