import_otrs_controller.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://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: __('The provided URL is invalid.'),
  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' => __('The hostname could not be found.'),
  17. 'No route to host' => __('There is no route to this host.'),
  18. 'Connection refused' => __('The connection was refused.'),
  19. }
  20. response = UserAgent.request(params[:url])
  21. if !response.success? && response.code.to_s !~ %r{^40.$}
  22. message_human = ''
  23. translation_map.each do |key, message|
  24. if response.error.to_s.match?(%r{#{Regexp.escape(key)}}i)
  25. message_human = message
  26. end
  27. end
  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.include?('zammad migrator')
  37. migrator_response = JSON.parse(response.body)
  38. if migrator_response['Success'] == 1
  39. # set url and key for import endpoint
  40. url = migrator_response['URL']
  41. key = migrator_response['Key']
  42. # get first part url, used for import_otrs_endpoint
  43. if !url || !key
  44. url_parts = params[:url].split(';')
  45. if !url_parts[1] # in case of & instead of ;
  46. url_parts = params[:url].split('&')
  47. end
  48. key_parts = url_parts[1].split('=')
  49. if !key_parts[1]
  50. render json: {
  51. result: 'invalid',
  52. message_human: __('Import API key could not be extracted from URL.')
  53. }
  54. return
  55. end
  56. if !url
  57. url = url_parts[0]
  58. end
  59. if !key
  60. key = key_parts[1]
  61. end
  62. end
  63. Setting.set('import_backend', 'otrs')
  64. Setting.set('import_otrs_endpoint', url)
  65. Setting.set('import_otrs_endpoint_key', key)
  66. result = {
  67. result: 'ok',
  68. url: params[:url],
  69. }
  70. else
  71. result = {
  72. result: 'invalid',
  73. message_human: migrator_response['Error']
  74. }
  75. end
  76. elsif response.body.match?(%r{(otrs\sag|otrs\.com|otrs\.org)}i)
  77. result = {
  78. result: 'invalid',
  79. message_human: __('Host found, but no OTRS migrator is installed!')
  80. }
  81. else
  82. result = {
  83. result: 'invalid',
  84. message_human: __('Host found, but it seems to be no OTRS installation!'),
  85. }
  86. end
  87. render json: result
  88. end
  89. def import_start
  90. return if setup_done_response
  91. Setting.set('import_mode', true)
  92. welcome = Import::OTRS.connection_test
  93. if !welcome
  94. render json: {
  95. message: __('Migrator can\'t read OTRS output!'),
  96. result: 'invalid',
  97. }
  98. return
  99. end
  100. # start migration
  101. AsyncOtrsImportJob.perform_later
  102. render json: {
  103. result: 'ok',
  104. }
  105. end
  106. def import_check
  107. Import::OTRS::Requester.list
  108. issues = []
  109. # check count of dynamic fields
  110. dynamic_field_count = 0
  111. dynamic_fields = Import::OTRS::Requester.load('DynamicField')
  112. dynamic_fields.each do |dynamic_field|
  113. next if dynamic_field['ValidID'].to_i != 1
  114. dynamic_field_count += 1
  115. end
  116. if dynamic_field_count > 20
  117. issues.push 'otrsDynamicFields'
  118. end
  119. # check if process exsists
  120. sys_configs = Import::OTRS::Requester.load('SysConfig')
  121. sys_configs.each do |sys_config|
  122. next if sys_config['Key'] != 'Process'
  123. issues.push 'otrsProcesses'
  124. end
  125. result = 'ok'
  126. if issues.present?
  127. result = 'failed'
  128. end
  129. render json: {
  130. result: result,
  131. issues: issues,
  132. }
  133. end
  134. def import_status
  135. result = Import::OTRS.status_bg
  136. if result[:result] == 'import_done'
  137. Setting.reload
  138. end
  139. render json: result
  140. end
  141. private
  142. def setup_done
  143. count = User.count
  144. done = true
  145. if count <= 2
  146. done = false
  147. end
  148. done
  149. end
  150. def setup_done_response
  151. if !setup_done
  152. return false
  153. end
  154. render json: {
  155. setup_done: true,
  156. }
  157. true
  158. end
  159. end