import_zendesk_controller.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ImportZendeskController < ApplicationController
  3. def url_check
  4. return if setup_done_response
  5. # validate
  6. if params[:url].blank? || 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. 'No such file' => __('The hostname could not be found.'),
  16. 'getaddrinfo: nodename nor servname provided, or not known' => __('The hostname could not be found.'),
  17. '503 Service Temporarily Unavailable' => __('The hostname could not be found.'),
  18. 'No route to host' => __('There is no route to this host.'),
  19. 'Connection refused' => __('The connection was refused.'),
  20. }
  21. response = UserAgent.request(URI.join(params[:url], '/api/v2/users/me').to_s, verify_ssl: true)
  22. if !response.success?
  23. message_human = ''
  24. translation_map.each do |key, message|
  25. if response.error.to_s.match?(%r{#{Regexp.escape(key)}}i)
  26. message_human = message
  27. end
  28. end
  29. render json: {
  30. result: 'invalid',
  31. message_human: message_human,
  32. message: response.error.to_s,
  33. }
  34. return
  35. end
  36. if response.header['x-zendesk-api-version'].blank?
  37. render json: {
  38. result: 'invalid',
  39. message_human: __('The hostname could not be found.'),
  40. }
  41. return
  42. end
  43. endpoint = "#{params[:url]}/api/v2"
  44. endpoint.gsub!(%r{([^:])//+}, '\\1/')
  45. Setting.set('import_zendesk_endpoint', endpoint)
  46. render json: {
  47. result: 'ok',
  48. url: params[:url],
  49. }
  50. end
  51. def credentials_check
  52. return if setup_done_response
  53. if !params[:username] || !params[:token]
  54. render json: {
  55. result: 'invalid',
  56. message_human: __('Incomplete credentials'),
  57. }
  58. return
  59. end
  60. Setting.set('import_zendesk_endpoint_username', params[:username])
  61. Setting.set('import_zendesk_endpoint_key', params[:token])
  62. result = Sequencer.process('Import::Zendesk::ConnectionTest')
  63. if !result[:connected]
  64. Setting.set('import_zendesk_endpoint_username', nil)
  65. Setting.set('import_zendesk_endpoint_key', nil)
  66. render json: {
  67. result: 'invalid',
  68. message_human: __('The provided credentials are invalid.'),
  69. }
  70. return
  71. end
  72. render json: {
  73. result: 'ok',
  74. }
  75. end
  76. def import_start
  77. return if setup_done_response
  78. Setting.set('import_mode', true)
  79. Setting.set('import_backend', 'zendesk')
  80. job = ImportJob.create(name: 'Import::Zendesk')
  81. AsyncImportJob.perform_later(job)
  82. render json: {
  83. result: 'ok',
  84. }
  85. end
  86. def import_status
  87. job = ImportJob.find_by(name: 'Import::Zendesk')
  88. if job.finished_at.present?
  89. Setting.reload
  90. end
  91. model_show_render_item(job)
  92. end
  93. private
  94. def setup_done
  95. count = User.count
  96. done = true
  97. if count <= 2
  98. done = false
  99. end
  100. done
  101. end
  102. def setup_done_response
  103. if !setup_done
  104. return false
  105. end
  106. render json: {
  107. setup_done: true,
  108. }
  109. true
  110. end
  111. end