import_otrs_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class ImportOtrsController < ApplicationController
  3. def url_check
  4. return if setup_done_response
  5. # validate
  6. if !params[:url] || params[:url] !~ %r{^(http|https)://.+?$}
  7. render json: {
  8. result: 'invalid',
  9. message: 'Invalid URL!',
  10. }
  11. return
  12. end
  13. # connection test
  14. translation_map = {
  15. 'authentication failed' => 'Authentication failed!',
  16. 'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
  17. 'No route to host' => 'No route to host!',
  18. 'Connection refused' => 'Connection refused!',
  19. }
  20. response = UserAgent.request( params[:url] )
  21. if !response.success? && response.code.to_s !~ /^40.$/
  22. message_human = ''
  23. translation_map.each { |key, message|
  24. if response.error.to_s =~ /#{Regexp.escape(key)}/i
  25. message_human = message
  26. end
  27. }
  28. render json: {
  29. result: 'invalid',
  30. message_human: message_human,
  31. message: response.error.to_s,
  32. }
  33. return
  34. end
  35. result = {}
  36. if response.body =~ /zammad migrator/
  37. migrator_response = JSON.parse(response.body)
  38. if migrator_response['Success'] == 1
  39. url_parts = params[:url].split(';')
  40. key_parts = url_parts[1].split('=')
  41. Setting.set('import_backend', 'otrs')
  42. Setting.set('import_otrs_endpoint', url_parts[0])
  43. Setting.set('import_otrs_endpoint_key', key_parts[1])
  44. result = {
  45. result: 'ok',
  46. url: params[:url],
  47. }
  48. else
  49. result = {
  50. result: 'invalid',
  51. message_human: migrator_response['Error']
  52. }
  53. end
  54. elsif response.body =~ /(otrs\sag|otrs\.com|otrs\.org)/i
  55. result = {
  56. result: 'invalid',
  57. message_human: 'Host found, but no OTRS migrator is installed!'
  58. }
  59. else
  60. result = {
  61. result: 'invalid',
  62. message_human: 'Host found, but it seems to be no OTRS installation!',
  63. }
  64. end
  65. render json: result
  66. end
  67. def import_start
  68. return if setup_done_response
  69. Setting.set('import_mode', true)
  70. welcome = Import::OTRS.connection_test
  71. if !welcome
  72. render json: {
  73. message: 'Migrator can\'t read OTRS output!',
  74. result: 'invalid',
  75. }
  76. return
  77. end
  78. # start migration
  79. Import::OTRS.delay.start_bg
  80. render json: {
  81. result: 'ok',
  82. }
  83. end
  84. def import_status
  85. result = Import::OTRS.status_bg
  86. if result[:result] == 'import_done'
  87. Setting.reload
  88. end
  89. render json: result
  90. end
  91. private
  92. def setup_done
  93. count = User.all.count()
  94. done = true
  95. if count <= 2
  96. done = false
  97. end
  98. done
  99. end
  100. def setup_done_response
  101. if !setup_done
  102. return false
  103. end
  104. render json: {
  105. setup_done: true,
  106. }
  107. true
  108. end
  109. end