can_xoauth2_email_channel.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. module CanXoauth2EmailChannel
  3. extend ActiveSupport::Concern
  4. def area
  5. raise NotImplementedError
  6. end
  7. def external_credential_name
  8. raise NotImplementedError
  9. end
  10. def index
  11. system_online_service = Setting.get('system_online_service')
  12. assets = {}
  13. external_credential_ids = []
  14. ExternalCredential.where(name: external_credential_name).each do |external_credential|
  15. assets = external_credential.assets(assets)
  16. external_credential_ids.push external_credential.id
  17. end
  18. channel_ids = []
  19. Channel.where(area:).reorder(:id).each do |channel|
  20. assets = channel.assets(assets)
  21. channel_ids.push channel.id
  22. end
  23. not_used_email_address_ids = []
  24. EmailAddress.find_each do |email_address|
  25. next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
  26. assets = email_address.assets(assets)
  27. if !email_address.channel_id || !email_address.active || !Channel.exists?(email_address.channel_id)
  28. not_used_email_address_ids.push email_address.id
  29. end
  30. end
  31. render json: {
  32. assets: assets,
  33. not_used_email_address_ids: not_used_email_address_ids,
  34. channel_ids: channel_ids,
  35. external_credential_ids: external_credential_ids,
  36. callback_url: ExternalCredential.callback_url(external_credential_name),
  37. }
  38. end
  39. def group
  40. channel = Channel.find_by(id: params[:id], area:)
  41. channel.group_id = params[:group_id]
  42. channel.save!
  43. handle_group_email_address(channel)
  44. render json: {}
  45. end
  46. def inbound
  47. channel = Channel.find_by(id: params[:id], area:)
  48. channel.refresh_xoauth2!(force: true)
  49. inbound_prepare_channel(channel)
  50. result = EmailHelper::Probe.inbound(channel.options[:inbound])
  51. raise Exceptions::UnprocessableEntity, (result[:message_human] || result[:message]) if result[:result] == 'invalid'
  52. render json: result
  53. end
  54. def verify
  55. channel = Channel.find_by(id: params[:id], area:)
  56. verify_prepare_channel(channel)
  57. channel.save!
  58. handle_group_email_address(channel)
  59. render json: {}
  60. end
  61. private
  62. def inbound_prepare_channel(channel)
  63. channel.group_id = params[:group_id] if params[:group_id].present?
  64. channel.active = params[:active] if params.key?(:active)
  65. channel.options[:inbound] ||= {}
  66. channel.options[:inbound][:options] ||= {}
  67. %w[folder folder_id keep_on_server].each do |key|
  68. next if params.dig(:options, key).nil?
  69. channel.options[:inbound][:options][key] = params[:options][key]
  70. end
  71. end
  72. def verify_prepare_channel(channel)
  73. inbound_prepare_channel(channel)
  74. %w[archive archive_before archive_state_id].each do |key|
  75. next if params.dig(:options, key).nil?
  76. channel.options[:inbound][:options][key] = params[:options][key]
  77. end
  78. channel.status_in = 'ok'
  79. channel.status_out = 'ok'
  80. channel.last_log_in = nil
  81. channel.last_log_out = nil
  82. end
  83. def handle_group_email_address(channel)
  84. return if !handle_group_email_address?
  85. if params[:group_email_address_id]
  86. email_address = EmailAddress.find(params[:group_email_address_id])
  87. end
  88. Service::Channel::Email::UpdateDestinationGroupEmail.new(
  89. group: Group.find(params[:group_id]),
  90. channel: channel,
  91. email_address:,
  92. ).execute
  93. end
  94. def handle_group_email_address?
  95. ActiveModel::Type::Boolean.new.cast params[:group_email_address]
  96. end
  97. end