exchange_controller.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Integration::ExchangeController < ApplicationController
  3. include Integration::ImportJobBase
  4. prepend_before_action { authentication_check && authorize! }
  5. def index
  6. assets = {}
  7. external_credential_ids = []
  8. ExternalCredential.where(name: 'exchange').each do |external_credential|
  9. assets = external_credential.assets(assets)
  10. external_credential_ids.push external_credential.id
  11. end
  12. render json: {
  13. assets: assets,
  14. oauth: Setting.get('exchange_oauth'),
  15. external_credential_ids: external_credential_ids,
  16. callback_url: ExternalCredential.callback_url('exchange'),
  17. }
  18. end
  19. def destroy_oauth
  20. Setting.set('exchange_oauth', {})
  21. render json: {}
  22. end
  23. def autodiscover
  24. if params[:authentication_method].present? && params[:authentication_method] == 'oauth'
  25. render json: {}
  26. return
  27. end
  28. answer_with do
  29. autodiscover_basic_auth_check
  30. end
  31. end
  32. def folders
  33. answer_with do
  34. Sequencer.process('Import::Exchange::AvailableFolders',
  35. parameters: { ews_config: ews_config })
  36. .tap do |res|
  37. raise __('No folders were found for the given user credentials.') if res[:folders].blank?
  38. end
  39. end
  40. end
  41. def mapping
  42. answer_with do
  43. raise __('Please select at least one folder.') if params[:folders].blank?
  44. Sequencer.process('Import::Exchange::AttributesExamples',
  45. parameters: { ews_folder_ids: params[:folders],
  46. ews_config: ews_config })
  47. .tap do |res|
  48. raise __('No entries were found in the selected folder(s).') if res[:attributes].blank?
  49. end
  50. end
  51. end
  52. private
  53. def payload_dry_run
  54. {
  55. ews_attributes: params[:attributes].permit!.to_h,
  56. ews_folder_ids: params[:folders],
  57. ews_config: ews_config,
  58. params: params,
  59. }
  60. end
  61. def ews_config
  62. {
  63. disable_ssl_verify: params[:disable_ssl_verify],
  64. endpoint: params[:endpoint],
  65. user: params[:user],
  66. password: params[:password],
  67. auth_type: params[:auth_type],
  68. access_token: Setting.get('exchange_oauth')[:access_token],
  69. }
  70. end
  71. def autodiscover_basic_auth_check
  72. require 'autodiscover' # Only load this gem when it is really used.
  73. client = Autodiscover::Client.new(
  74. email: params[:user],
  75. password: params[:password],
  76. )
  77. if params[:disable_ssl_verify]
  78. client.http.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
  79. end
  80. begin
  81. { endpoint: client.autodiscover&.ews_url }
  82. rescue Errno::EADDRNOTAVAIL
  83. {}
  84. end
  85. end
  86. end