microsoft_graph_controller.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 = inbound_prepare_channel(params)
  38. channel.refresh_xoauth2!
  39. result = EmailHelper::Probe.inbound(channel.options[:inbound])
  40. raise Exceptions::UnprocessableEntity, (result[:message_human] || result[:message]) if result[:result] == 'invalid'
  41. channel.status_in = 'ok'
  42. channel.status_out = 'ok'
  43. channel.last_log_in = nil
  44. channel.last_log_out = nil
  45. if params.key?(:active)
  46. channel.active = params[:active]
  47. end
  48. channel.save!
  49. render json: {}
  50. end
  51. def group
  52. channel = Channel.find_by(id: params[:id], area:)
  53. channel.group_id = params[:group_id]
  54. channel.save!
  55. render json: {}
  56. end
  57. def folders
  58. channel = Channel.find_by(id: params[:id], area:)
  59. raise Exceptions::UnprocessableEntity, __('Could not find the channel.') if channel.nil?
  60. channel_mailbox = channel.options.dig('inbound', 'options', 'shared_mailbox') || channel.options.dig('inbound', 'options', 'user')
  61. raise Exceptions::UnprocessableEntity, __('Could not identify the channel mailbox.') if channel_mailbox.nil?
  62. channel.refresh_xoauth2!(force: true)
  63. graph = ::MicrosoftGraph.new access_token: channel.options.dig('auth', 'access_token'), mailbox: channel_mailbox
  64. begin
  65. folders = graph.get_message_folders_tree
  66. rescue ::MicrosoftGraph::ApiError => e
  67. error = {
  68. message: e.message,
  69. code: e.error_code,
  70. }
  71. end
  72. render json: { folders:, error: }
  73. end
  74. private
  75. def inbound_prepare_channel(params)
  76. channel = Channel.find_by(id: params[:id], area:)
  77. channel.group_id = params[:group_id] if params[:group_id].present?
  78. %w[folder_id keep_on_server].each do |key|
  79. channel.options[:inbound][:options][key] = params[:options][key]
  80. end
  81. channel
  82. end
  83. end