channels_microsoft365_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ChannelsMicrosoft365Controller < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def index
  5. system_online_service = Setting.get('system_online_service')
  6. assets = {}
  7. external_credential_ids = []
  8. ExternalCredential.where(name: 'microsoft365').each do |external_credential|
  9. assets = external_credential.assets(assets)
  10. external_credential_ids.push external_credential.id
  11. end
  12. channel_ids = []
  13. Channel.where(area: 'Microsoft365::Account').reorder(:id).each do |channel|
  14. assets = channel.assets(assets)
  15. channel_ids.push channel.id
  16. end
  17. not_used_email_address_ids = []
  18. EmailAddress.find_each do |email_address|
  19. next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
  20. assets = email_address.assets(assets)
  21. if !email_address.channel_id || !email_address.active || !Channel.exists?(email_address.channel_id)
  22. not_used_email_address_ids.push email_address.id
  23. end
  24. end
  25. render json: {
  26. assets: assets,
  27. not_used_email_address_ids: not_used_email_address_ids,
  28. channel_ids: channel_ids,
  29. external_credential_ids: external_credential_ids,
  30. callback_url: ExternalCredential.callback_url('microsoft365'),
  31. }
  32. end
  33. def enable
  34. channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
  35. channel.active = true
  36. channel.save!
  37. render json: {}
  38. end
  39. def disable
  40. channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
  41. channel.active = false
  42. channel.save!
  43. render json: {}
  44. end
  45. def destroy
  46. channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
  47. email = EmailAddress.find_by(channel_id: channel.id)
  48. email&.destroy!
  49. channel.destroy!
  50. render json: {}
  51. end
  52. def group
  53. channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
  54. channel.group_id = params[:group_id]
  55. channel.save!
  56. render json: {}
  57. end
  58. def inbound
  59. channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
  60. %w[folder keep_on_server].each do |key|
  61. channel.options[:inbound][:options][key] = params[:options][key]
  62. end
  63. channel.refresh_xoauth2!(force: true)
  64. result = EmailHelper::Probe.inbound(channel.options[:inbound])
  65. raise Exceptions::UnprocessableEntity, (result[:message_human] || result[:message]) if result[:result] == 'invalid'
  66. channel.status_in = 'ok'
  67. channel.status_out = 'ok'
  68. channel.last_log_in = nil
  69. channel.last_log_out = nil
  70. if params.key?(:active)
  71. channel.active = params[:active]
  72. end
  73. channel.save!
  74. render json: {}
  75. end
  76. def rollback_migration
  77. channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
  78. raise __('Failed to find backup on channel!') if !channel.options[:backup_imap_classic]
  79. channel.update!(channel.options[:backup_imap_classic][:attributes])
  80. render json: {}
  81. end
  82. end