import_zendesk_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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] || 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 { |key, message|
  25. if response.error.to_s =~ /#{Regexp.escape(key)}/i
  26. message_human = message
  27. end
  28. }
  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 signup page has been implemented
  37. if response.body =~ /(Take it for a risk-free|Take it for a risk-free)/i
  38. render json: {
  39. result: 'invalid',
  40. message_human: 'Hostname not found!',
  41. }
  42. return
  43. end
  44. Setting.set('import_zendesk_endpoint', "#{params[:url]}api/v2")
  45. render json: {
  46. result: 'ok',
  47. url: params[:url],
  48. }
  49. end
  50. def credentials_check
  51. return if setup_done_response
  52. if !params[:username] || !params[:token]
  53. render json: {
  54. result: 'invalid',
  55. message_human: 'Incomplete credentials',
  56. }
  57. return
  58. end
  59. Setting.set('import_zendesk_endpoint_username', params[:username])
  60. Setting.set('import_zendesk_endpoint_key', params[:token])
  61. if !Import::Zendesk.connection_test
  62. Setting.set('import_zendesk_endpoint_username', nil)
  63. Setting.set('import_zendesk_endpoint_key', nil)
  64. render json: {
  65. result: 'invalid',
  66. message_human: 'Invalid credentials!',
  67. }
  68. return
  69. end
  70. render json: {
  71. result: 'ok',
  72. }
  73. end
  74. def import_start
  75. return if setup_done_response
  76. Setting.set('import_mode', true)
  77. # start migration
  78. Import::Zendesk.delay.start_bg
  79. render json: {
  80. result: 'ok',
  81. }
  82. end
  83. def import_status
  84. result = Import::Zendesk.status_bg
  85. if result[:result] == 'import_done'
  86. Setting.reload
  87. end
  88. render json: result
  89. end
  90. private
  91. def setup_done
  92. count = User.all.count()
  93. done = true
  94. if count <= 2
  95. done = false
  96. end
  97. done
  98. end
  99. def setup_done_response
  100. if !setup_done
  101. return false
  102. end
  103. render json: {
  104. setup_done: true,
  105. }
  106. true
  107. end
  108. end