2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2020-10-02 12:46:19 +00:00
|
|
|
|
2020-10-27 14:05:10 +00:00
|
|
|
class ChannelsMicrosoft365Controller < ApplicationController
|
2020-10-02 12:46:19 +00:00
|
|
|
prepend_before_action -> { authentication_check && authorize! }
|
|
|
|
|
|
|
|
def index
|
|
|
|
system_online_service = Setting.get('system_online_service')
|
|
|
|
|
|
|
|
assets = {}
|
|
|
|
external_credential_ids = []
|
2020-10-27 14:05:10 +00:00
|
|
|
ExternalCredential.where(name: 'microsoft365').each do |external_credential|
|
2020-10-02 12:46:19 +00:00
|
|
|
assets = external_credential.assets(assets)
|
|
|
|
external_credential_ids.push external_credential.id
|
|
|
|
end
|
|
|
|
|
|
|
|
channel_ids = []
|
2020-10-27 14:05:10 +00:00
|
|
|
Channel.where(area: 'Microsoft365::Account').order(:id).each do |channel|
|
2020-10-02 12:46:19 +00:00
|
|
|
assets = channel.assets(assets)
|
|
|
|
channel_ids.push channel.id
|
|
|
|
end
|
|
|
|
|
|
|
|
not_used_email_address_ids = []
|
|
|
|
EmailAddress.find_each do |email_address|
|
|
|
|
next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
|
|
|
|
|
|
|
|
assets = email_address.assets(assets)
|
|
|
|
if !email_address.channel_id || !email_address.active || !Channel.exists?(email_address.channel_id)
|
|
|
|
not_used_email_address_ids.push email_address.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: {
|
|
|
|
assets: assets,
|
|
|
|
not_used_email_address_ids: not_used_email_address_ids,
|
|
|
|
channel_ids: channel_ids,
|
|
|
|
external_credential_ids: external_credential_ids,
|
2020-10-27 14:05:10 +00:00
|
|
|
callback_url: ExternalCredential.callback_url('microsoft365'),
|
2020-10-02 12:46:19 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable
|
2020-10-27 14:05:10 +00:00
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
|
2020-10-02 12:46:19 +00:00
|
|
|
channel.active = true
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable
|
2020-10-27 14:05:10 +00:00
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
|
2020-10-02 12:46:19 +00:00
|
|
|
channel.active = false
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-10-27 14:05:10 +00:00
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
|
2020-10-02 12:46:19 +00:00
|
|
|
email = EmailAddress.find_by(channel_id: channel.id)
|
|
|
|
email.destroy!
|
|
|
|
channel.destroy!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def group
|
2020-10-27 14:05:10 +00:00
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
|
2020-10-02 12:46:19 +00:00
|
|
|
channel.group_id = params[:group_id]
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def inbound
|
2020-10-27 14:05:10 +00:00
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
|
2020-10-02 12:46:19 +00:00
|
|
|
%w[folder keep_on_server].each do |key|
|
|
|
|
channel.options[:inbound][:options][key] = params[:options][key]
|
|
|
|
end
|
|
|
|
|
2021-01-06 09:45:01 +00:00
|
|
|
channel.refresh_xoauth2!(force: true)
|
|
|
|
|
2020-10-02 12:46:19 +00:00
|
|
|
result = EmailHelper::Probe.inbound(channel.options[:inbound])
|
2021-07-16 13:38:01 +00:00
|
|
|
raise Exceptions::UnprocessableEntity, (result[:message_human] || result[:message]) if result[:result] == 'invalid'
|
2020-10-02 12:46:19 +00:00
|
|
|
|
|
|
|
channel.status_in = 'ok'
|
|
|
|
channel.status_out = 'ok'
|
|
|
|
channel.last_log_in = nil
|
|
|
|
channel.last_log_out = nil
|
|
|
|
if params.key?(:active)
|
|
|
|
channel.active = params[:active]
|
|
|
|
end
|
|
|
|
|
|
|
|
channel.save!
|
|
|
|
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def rollback_migration
|
2020-10-27 14:05:10 +00:00
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Microsoft365::Account')
|
2020-10-02 12:46:19 +00:00
|
|
|
raise 'Failed to find backup on channel!' if !channel.options[:backup_imap_classic]
|
|
|
|
|
|
|
|
channel.update!(channel.options[:backup_imap_classic][:attributes])
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|