microsoft_graph_controller.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. class ChannelsAdmin::MicrosoftGraphController < ChannelsAdmin::BaseController
  3. include CanXoauth2EmailChannel
  4. def area
  5. 'MicrosoftGraph::Account'.freeze
  6. end
  7. def external_credential_name
  8. 'microsoft_graph'.freeze
  9. end
  10. def folders
  11. channel = Channel.find_by(id: params[:id], area:)
  12. raise Exceptions::UnprocessableEntity, __('Could not find the channel.') if channel.nil?
  13. channel_mailbox = channel.options.dig('inbound', 'options', 'shared_mailbox') || channel.options.dig('inbound', 'options', 'user')
  14. raise Exceptions::UnprocessableEntity, __('Could not identify the channel mailbox.') if channel_mailbox.nil?
  15. channel.refresh_xoauth2!(force: true)
  16. graph = ::MicrosoftGraph.new access_token: channel.options.dig('auth', 'access_token'), mailbox: channel_mailbox
  17. begin
  18. folders = graph.get_message_folders_tree
  19. rescue ::MicrosoftGraph::ApiError => e
  20. error = {
  21. message: e.message,
  22. code: e.error_code,
  23. }
  24. end
  25. render json: { folders:, error: }
  26. end
  27. end