import_freshdesk_controller.rb 3.3 KB

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