import_zendesk_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'zendesk_api'
  3. class ImportZendeskController < ApplicationController
  4. def url_check
  5. return if setup_done_response
  6. # validate
  7. if params[:url].blank? || params[:url] !~ %r{^(http|https)://.+?$}
  8. render json: {
  9. result: 'invalid',
  10. message: 'Invalid URL!',
  11. }
  12. return
  13. end
  14. # connection test
  15. translation_map = {
  16. 'No such file' => 'Hostname not found!',
  17. 'getaddrinfo: nodename nor servname provided, or not known' => 'Hostname not found!',
  18. 'No route to host' => 'No route to host!',
  19. 'Connection refused' => 'Connection refused!',
  20. }
  21. response = UserAgent.request(params[:url])
  22. if !response.success?
  23. message_human = ''
  24. translation_map.each do |key, message|
  25. if response.error.to_s =~ /#{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. # since 2016-10-15 a redirect to a marketing page has been implemented
  37. if response.body !~ /#{params[:url]}/
  38. render json: {
  39. result: 'invalid',
  40. message_human: 'Hostname not found!',
  41. }
  42. return
  43. end
  44. endpoint = "#{params[:url]}/api/v2"
  45. endpoint.gsub!(%r{([^:])//+}, '\\1/')
  46. Setting.set('import_zendesk_endpoint', endpoint)
  47. render json: {
  48. result: 'ok',
  49. url: params[:url],
  50. }
  51. end
  52. def credentials_check
  53. return if setup_done_response
  54. if !params[:username] || !params[:token]
  55. render json: {
  56. result: 'invalid',
  57. message_human: 'Incomplete credentials',
  58. }
  59. return
  60. end
  61. Setting.set('import_zendesk_endpoint_username', params[:username])
  62. Setting.set('import_zendesk_endpoint_key', params[:token])
  63. if !Import::Zendesk.connection_test
  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: 'Invalid credentials!',
  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. # start migration
  81. Import::Zendesk.delay.start_bg
  82. render json: {
  83. result: 'ok',
  84. }
  85. end
  86. def import_status
  87. result = Import::Zendesk.status_bg
  88. if result[:result] == 'import_done'
  89. Setting.reload
  90. end
  91. render json: result
  92. end
  93. private
  94. def setup_done
  95. count = User.all.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