channels_google_controller.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. class ChannelsGoogleController < 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: 'google').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: 'Google::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('google'),
  31. }
  32. end
  33. def enable
  34. channel = Channel.find_by(id: params[:id], area: 'Google::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: 'Google::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: 'Google::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: 'Google::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: 'Google::Account')
  60. channel.refresh_xoauth2!(force: true)
  61. channel.options[:inbound] ||= {}
  62. channel.options[:inbound][:options] ||= {}
  63. %w[folder keep_on_server].each do |key|
  64. next if params.dig(:options, key).nil?
  65. channel.options[:inbound][:options][key] = params[:options][key]
  66. end
  67. result = EmailHelper::Probe.inbound(channel.options[:inbound])
  68. raise Exceptions::UnprocessableEntity, (result[:message_human] || result[:message]) if result[:result] == 'invalid'
  69. render json: result
  70. end
  71. def verify
  72. channel = Channel.find_by(id: params[:id], area: 'Google::Account')
  73. verify_prepare_channel(channel, params)
  74. channel.save!
  75. render json: {}
  76. end
  77. def rollback_migration
  78. channel = Channel.find_by!(id: params[:id], area: 'Google::Account')
  79. raise __('Failed to find backup on channel!') if !channel.options[:backup_imap_classic]
  80. channel.update!(channel.options[:backup_imap_classic][:attributes])
  81. render json: {}
  82. end
  83. private
  84. def verify_prepare_channel(channel, params)
  85. channel.group_id = params[:group_id] if params[:group_id].present?
  86. channel.active = params[:active] if params.key?(:active)
  87. channel.options[:inbound] ||= {}
  88. channel.options[:inbound][:options] ||= {}
  89. %w[folder keep_on_server archive archive_before archive_state_id].each do |key|
  90. next if params.dig(:options, key).nil?
  91. channel.options[:inbound][:options][key] = params[:options][key]
  92. end
  93. channel.status_in = 'ok'
  94. channel.status_out = 'ok'
  95. channel.last_log_in = nil
  96. channel.last_log_out = nil
  97. end
  98. end