apply_freshdesk_configuration.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::System::Import::ApplyFreshdeskConfiguration < Service::System::Import::ApplyConfigurationBase
  3. def execute
  4. super()
  5. Setting.set('import_freshdesk_endpoint', @endpoint)
  6. Setting.set('import_freshdesk_endpoint_key', @secret)
  7. Setting.set('import_backend', 'freshdesk')
  8. end
  9. private
  10. def build_endpoint
  11. "#{@url}/api/v2".gsub(%r{([^:])//+}, '\\1/')
  12. end
  13. def reachable!
  14. response = request("#{@endpoint}/contacts", { verify_ssl: @tls_verify })
  15. return if response.header&.fetch('x-freshdesk-api-version', nil).present?
  16. message = response.error.to_s.presence || __('The hostname could not be found.')
  17. raise_unreachable_error(message)
  18. end
  19. def accessible!
  20. result = check_accessibility { Sequencer.process('Import::Freshdesk::ConnectionTest') }
  21. raise InaccessibleError, __('The provided credentials are invalid.') if !result[:connected]
  22. result = check_accessibility { Sequencer.process('Import::Freshdesk::PermissionCheck') }
  23. raise InaccessibleError, __('You do not have administrator permission.') if !result[:permission_present]
  24. end
  25. def check_accessibility(&)
  26. Setting.set('import_freshdesk_endpoint', @endpoint)
  27. Setting.set('import_freshdesk_endpoint_key', @secret)
  28. result = yield
  29. Setting.set('import_freshdesk_endpoint', nil)
  30. Setting.set('import_freshdesk_endpoint_key', nil)
  31. result
  32. end
  33. def raise_unreachable_error(message)
  34. messages = {
  35. 'No such file' => __('The hostname could not be found.'),
  36. 'getaddrinfo: nodename nor servname provided, or not known' => __('The hostname could not be found.'),
  37. '503 Service Temporarily Unavailable' => __('The hostname could not be found.'),
  38. 'No route to host' => __('There is no route to this host.'),
  39. 'Connection refused' => __('The connection was refused.'),
  40. }
  41. human_message = messages.find { |key, _| message.match?(%r{#{Regexp.escape(key)}}i) }&.last
  42. raise UnreachableError, human_message.presence || message
  43. end
  44. end