microsoft_graph_controller.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. class ChannelsAdmin::MicrosoftGraphController < ChannelsAdmin::BaseController
  3. def area
  4. 'MicrosoftGraph::Account'.freeze
  5. end
  6. def index
  7. system_online_service = Setting.get('system_online_service')
  8. assets = {}
  9. external_credential_ids = []
  10. ExternalCredential.where(name: 'microsoft_graph').each do |external_credential|
  11. assets = external_credential.assets(assets)
  12. external_credential_ids.push external_credential.id
  13. end
  14. channels = Service::Channel::Admin::List.new(area: area).execute
  15. channel_ids = []
  16. channels.each do |channel|
  17. assets = channel.assets(assets)
  18. channel_ids.push channel.id
  19. end
  20. not_used_email_address_ids = []
  21. EmailAddress.find_each do |email_address|
  22. next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
  23. assets = email_address.assets(assets)
  24. if !email_address.channel_id || !email_address.active || !Channel.exists?(email_address.channel_id)
  25. not_used_email_address_ids.push email_address.id
  26. end
  27. end
  28. render json: {
  29. assets: assets,
  30. not_used_email_address_ids: not_used_email_address_ids,
  31. channel_ids: channel_ids,
  32. external_credential_ids: external_credential_ids,
  33. callback_url: ExternalCredential.callback_url('microsoft_graph'),
  34. }
  35. end
  36. def inbound
  37. channel = Channel.find_by(id: params[:id], area:)
  38. channel.refresh_xoauth2!
  39. inbound_prepare_channel(channel, params)
  40. result = EmailHelper::Probe.inbound(channel.options[:inbound])
  41. raise Exceptions::UnprocessableEntity, (result[:message_human] || result[:message]) if result[:result] == 'invalid'
  42. render json: result
  43. end
  44. def verify
  45. channel = Channel.find_by(id: params[:id], area:)
  46. verify_prepare_channel(channel, params)
  47. channel.save!
  48. render json: {}
  49. end
  50. def group
  51. channel = Channel.find_by(id: params[:id], area:)
  52. channel.group_id = params[:group_id]
  53. channel.save!
  54. render json: {}
  55. end
  56. def folders
  57. channel = Channel.find_by(id: params[:id], area:)
  58. raise Exceptions::UnprocessableEntity, __('Could not find the channel.') if channel.nil?
  59. channel_mailbox = channel.options.dig('inbound', 'options', 'shared_mailbox') || channel.options.dig('inbound', 'options', 'user')
  60. raise Exceptions::UnprocessableEntity, __('Could not identify the channel mailbox.') if channel_mailbox.nil?
  61. channel.refresh_xoauth2!(force: true)
  62. graph = ::MicrosoftGraph.new access_token: channel.options.dig('auth', 'access_token'), mailbox: channel_mailbox
  63. begin
  64. folders = graph.get_message_folders_tree
  65. rescue ::MicrosoftGraph::ApiError => e
  66. error = {
  67. message: e.message,
  68. code: e.error_code,
  69. }
  70. end
  71. render json: { folders:, error: }
  72. end
  73. private
  74. def inbound_prepare_channel(channel, params)
  75. channel.group_id = params[:group_id] if params[:group_id].present?
  76. channel.active = params[:active] if params.key?(:active)
  77. channel.options[:inbound] ||= {}
  78. channel.options[:inbound][:options] ||= {}
  79. %w[folder_id keep_on_server].each do |key|
  80. next if params.dig(:options, key).nil?
  81. channel.options[:inbound][:options][key] = params[:options][key]
  82. end
  83. end
  84. def verify_prepare_channel(channel, params)
  85. inbound_prepare_channel(channel, params)
  86. %w[archive archive_before archive_state_id].each do |key|
  87. next if params.dig(:options, key).nil?
  88. channel.options[:inbound][:options][key] = params[:options][key]
  89. end
  90. channel.status_in = 'ok'
  91. channel.status_out = 'ok'
  92. channel.last_log_in = nil
  93. channel.last_log_out = nil
  94. end
  95. end