import_kayako_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class ImportKayakoController < ApplicationController
  3. def url_check
  4. return if setup_done_response
  5. url = params[:url]
  6. # validate
  7. if !valid_url_syntax?(url)
  8. render json: {
  9. result: 'invalid',
  10. message: __('The provided URL is invalid.'),
  11. }
  12. return
  13. end
  14. endpoint = build_endpoint_url(url)
  15. return if !valid_endpoint?(endpoint)
  16. Setting.set('import_kayako_endpoint', endpoint)
  17. render json: {
  18. result: 'ok',
  19. url: url,
  20. }
  21. end
  22. def credentials_check # rubocop:disable Metrics/AbcSize
  23. return if setup_done_response
  24. if !params[:username] || !params[:password]
  25. render json: {
  26. result: 'invalid',
  27. message_human: __('Incomplete credentials'),
  28. }
  29. return
  30. end
  31. save_endpoint_settings(params[:username], params[:password])
  32. return if !valid_connection?
  33. render json: {
  34. result: 'ok',
  35. }
  36. end
  37. def import_start
  38. return if setup_done_response
  39. Setting.set('import_mode', true)
  40. Setting.set('import_backend', 'kayako')
  41. job = ImportJob.create(name: 'Import::Kayako')
  42. AsyncImportJob.perform_later(job)
  43. render json: {
  44. result: 'ok',
  45. }
  46. end
  47. def import_status
  48. job = ImportJob.find_by(name: 'Import::Kayako')
  49. if job.finished_at.present?
  50. Setting.reload
  51. end
  52. model_show_render_item(job)
  53. end
  54. private
  55. def setup_done
  56. count = User.all.count
  57. done = true
  58. if count <= 2
  59. done = false
  60. end
  61. done
  62. end
  63. def setup_done_response
  64. if !setup_done
  65. return false
  66. end
  67. render json: {
  68. setup_done: true,
  69. }
  70. true
  71. end
  72. def valid_url_syntax?(url)
  73. return false if url.blank? || url !~ %r{^(http|https)://.+?$}
  74. true
  75. end
  76. def valid_endpoint?(endpoint)
  77. response = UserAgent.request("#{endpoint}/teams", verify_ssl: true)
  78. if response.header.nil? || !response.header['x-api-version']
  79. render json: {
  80. result: 'invalid',
  81. message: response.error.to_s,
  82. message_human: __('The hostname could not be found.'),
  83. }
  84. return false
  85. end
  86. true
  87. end
  88. def build_endpoint_url(url)
  89. endpoint = "#{url}/api/v1"
  90. endpoint.gsub(%r{([^:])//+}, '\\1/')
  91. end
  92. def valid_connection?
  93. result = Sequencer.process('Import::Kayako::ConnectionTest')
  94. if !result[:connected]
  95. reset_endpoint_settings
  96. render json: {
  97. result: 'invalid',
  98. message_human: __('The provided credentials are invalid.'),
  99. }
  100. return false
  101. end
  102. true
  103. end
  104. def save_endpoint_settings(username, possword)
  105. Setting.set('import_kayako_endpoint_username', username)
  106. Setting.set('import_kayako_endpoint_password', possword)
  107. end
  108. def reset_endpoint_settings
  109. save_endpoint_settings(nil, nil)
  110. end
  111. end