2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
class ChannelsEmailController < ApplicationController
|
2020-03-19 09:39:51 +00:00
|
|
|
prepend_before_action { authentication_check && authorize! }
|
2012-04-13 13:51:10 +00:00
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def index
|
2015-08-29 22:01:38 +00:00
|
|
|
system_online_service = Setting.get('system_online_service')
|
2015-08-30 07:15:51 +00:00
|
|
|
account_channel_ids = []
|
|
|
|
notification_channel_ids = []
|
|
|
|
email_address_ids = []
|
|
|
|
not_used_email_address_ids = []
|
2015-08-29 23:31:11 +00:00
|
|
|
accounts_fixed = []
|
2015-08-28 00:53:14 +00:00
|
|
|
assets = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
Channel.order(:id).each do |channel|
|
2015-08-29 23:31:11 +00:00
|
|
|
if system_online_service && channel.preferences && channel.preferences['online_service_disable']
|
|
|
|
email_addresses = EmailAddress.where(channel_id: channel.id)
|
2017-10-01 12:25:52 +00:00
|
|
|
email_addresses.each do |email_address|
|
2015-08-29 23:31:11 +00:00
|
|
|
accounts_fixed.push email_address
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-29 23:31:11 +00:00
|
|
|
next
|
|
|
|
end
|
2020-05-28 13:28:07 +00:00
|
|
|
assets = channel.assets(assets)
|
2015-08-30 07:15:51 +00:00
|
|
|
if channel.area == 'Email::Account'
|
|
|
|
account_channel_ids.push channel.id
|
|
|
|
elsif channel.area == 'Email::Notification' && channel.active
|
|
|
|
notification_channel_ids.push channel.id
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
EmailAddress.all.each do |email_address|
|
2015-08-29 23:31:11 +00:00
|
|
|
next if system_online_service && email_address.preferences && email_address.preferences['online_service_disable']
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-08-30 07:15:51 +00:00
|
|
|
email_address_ids.push email_address.id
|
2015-08-28 00:53:14 +00:00
|
|
|
assets = email_address.assets(assets)
|
2020-08-03 08:35:43 +00:00
|
|
|
if !email_address.channel_id || !email_address.active || !Channel.exists?(id: email_address.channel_id)
|
2015-08-30 07:15:51 +00:00
|
|
|
not_used_email_address_ids.push email_address.id
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-28 00:53:14 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
accounts_fixed: accounts_fixed,
|
|
|
|
assets: assets,
|
|
|
|
account_channel_ids: account_channel_ids,
|
|
|
|
notification_channel_ids: notification_channel_ids,
|
|
|
|
email_address_ids: email_address_ids,
|
2015-08-30 07:15:51 +00:00
|
|
|
not_used_email_address_ids: not_used_email_address_ids,
|
2018-12-19 17:31:51 +00:00
|
|
|
channel_driver: {
|
2015-08-31 23:12:51 +00:00
|
|
|
email: EmailHelper.available_driver,
|
2015-09-11 15:22:58 +00:00
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
config: {
|
2015-09-11 15:22:58 +00:00
|
|
|
notification_sender: Setting.get('notification_sender'),
|
2015-08-31 23:12:51 +00:00
|
|
|
}
|
2015-08-28 00:53:14 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def probe
|
2015-08-28 00:53:14 +00:00
|
|
|
|
|
|
|
# probe settings based on email and password
|
|
|
|
result = EmailHelper::Probe.full(
|
2018-12-19 17:31:51 +00:00
|
|
|
email: params[:email],
|
2015-08-28 00:53:14 +00:00
|
|
|
password: params[:password],
|
2018-12-19 17:31:51 +00:00
|
|
|
folder: params[:folder],
|
2015-08-28 00:53:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# verify if user+host already exists
|
2020-09-30 09:07:01 +00:00
|
|
|
return if result[:result] == 'ok' && account_duplicate?(result)
|
2015-08-28 00:53:14 +00:00
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def outbound
|
2015-08-28 00:53:14 +00:00
|
|
|
|
2015-08-29 11:46:48 +00:00
|
|
|
# verify access
|
2015-11-30 13:29:23 +00:00
|
|
|
return if params[:channel_id] && !check_access(params[:channel_id])
|
2015-08-29 11:46:48 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
# connection test
|
|
|
|
render json: EmailHelper::Probe.outbound(params, params[:email])
|
|
|
|
end
|
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def inbound
|
2015-08-28 00:53:14 +00:00
|
|
|
|
2015-08-29 11:46:48 +00:00
|
|
|
# verify access
|
2015-11-30 13:29:23 +00:00
|
|
|
return if params[:channel_id] && !check_access(params[:channel_id])
|
2015-08-29 11:46:48 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
# connection test
|
|
|
|
result = EmailHelper::Probe.inbound(params)
|
|
|
|
|
|
|
|
# check account duplicate
|
2017-02-15 02:35:22 +00:00
|
|
|
return if account_duplicate?({ setting: { inbound: params } }, params[:channel_id])
|
2015-08-28 00:53:14 +00:00
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def verify
|
2017-09-12 14:38:32 +00:00
|
|
|
params.permit!
|
2015-08-28 00:53:14 +00:00
|
|
|
email = params[:email] || params[:meta][:email]
|
|
|
|
email = email.downcase
|
|
|
|
channel_id = params[:channel_id]
|
|
|
|
|
2015-08-29 11:46:48 +00:00
|
|
|
# verify access
|
2015-11-30 13:29:23 +00:00
|
|
|
return if channel_id && !check_access(channel_id)
|
2015-08-29 11:46:48 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
# check account duplicate
|
2017-02-15 02:35:22 +00:00
|
|
|
return if account_duplicate?({ setting: { inbound: params[:inbound] } }, channel_id)
|
2015-08-28 00:53:14 +00:00
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# check delivery for 30 sec.
|
2015-08-28 00:53:14 +00:00
|
|
|
result = EmailHelper::Verify.email(
|
2017-09-12 14:38:32 +00:00
|
|
|
outbound: params[:outbound].to_h,
|
2018-12-19 17:31:51 +00:00
|
|
|
inbound: params[:inbound].to_h,
|
|
|
|
sender: email,
|
|
|
|
subject: params[:subject],
|
2015-08-28 00:53:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if result[:result] != 'ok'
|
|
|
|
render json: result
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-09-11 15:22:58 +00:00
|
|
|
# fallback
|
|
|
|
if !params[:group_id]
|
|
|
|
params[:group_id] = Group.first.id
|
|
|
|
end
|
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
# update account
|
|
|
|
if channel_id
|
|
|
|
channel = Channel.find(channel_id)
|
2017-09-11 11:16:08 +00:00
|
|
|
channel.update!(
|
2018-12-19 17:31:51 +00:00
|
|
|
options: {
|
|
|
|
inbound: params[:inbound].to_h,
|
2017-09-12 14:38:32 +00:00
|
|
|
outbound: params[:outbound].to_h,
|
2015-08-28 00:53:14 +00:00
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
group_id: params[:group_id],
|
|
|
|
last_log_in: nil,
|
2015-08-28 08:19:27 +00:00
|
|
|
last_log_out: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
status_in: 'ok',
|
|
|
|
status_out: 'ok',
|
2015-08-28 00:53:14 +00:00
|
|
|
)
|
2015-09-06 07:50:51 +00:00
|
|
|
render json: result
|
2015-08-28 00:53:14 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# create new account
|
|
|
|
channel = Channel.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
area: 'Email::Account',
|
|
|
|
options: {
|
|
|
|
inbound: params[:inbound].to_h,
|
2017-09-12 14:38:32 +00:00
|
|
|
outbound: params[:outbound].to_h,
|
2015-08-28 00:53:14 +00:00
|
|
|
},
|
2018-12-19 17:31:51 +00:00
|
|
|
group_id: params[:group_id],
|
|
|
|
last_log_in: nil,
|
2015-08-29 11:46:48 +00:00
|
|
|
last_log_out: nil,
|
2018-12-19 17:31:51 +00:00
|
|
|
status_in: 'ok',
|
|
|
|
status_out: 'ok',
|
|
|
|
active: true,
|
2015-08-28 00:53:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# remember address && set channel for email address
|
|
|
|
address = EmailAddress.find_by(email: email)
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
# on initial setup, use placeholder email address
|
2015-08-28 00:53:14 +00:00
|
|
|
if Channel.count == 1
|
|
|
|
address = EmailAddress.first
|
|
|
|
end
|
|
|
|
|
|
|
|
if address
|
2017-09-11 11:16:08 +00:00
|
|
|
address.update!(
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: params[:meta][:realname],
|
|
|
|
email: email,
|
|
|
|
active: true,
|
2015-08-28 00:53:14 +00:00
|
|
|
channel_id: channel.id,
|
|
|
|
)
|
|
|
|
else
|
2019-06-28 11:38:49 +00:00
|
|
|
EmailAddress.create(
|
2018-12-19 17:31:51 +00:00
|
|
|
realname: params[:meta][:realname],
|
|
|
|
email: email,
|
|
|
|
active: true,
|
2015-08-28 00:53:14 +00:00
|
|
|
channel_id: channel.id,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-09-06 07:50:51 +00:00
|
|
|
render json: result
|
2015-08-28 00:53:14 +00:00
|
|
|
end
|
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def enable
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Email::Account')
|
|
|
|
channel.active = true
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Email::Account')
|
|
|
|
channel.active = false
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Email::Account')
|
|
|
|
channel.destroy
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def group
|
|
|
|
check_access
|
|
|
|
channel = Channel.find_by(id: params[:id], area: 'Email::Account')
|
|
|
|
channel.group_id = params[:group_id]
|
|
|
|
channel.save!
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification
|
2017-09-12 14:38:32 +00:00
|
|
|
params.permit!
|
2015-08-28 00:53:14 +00:00
|
|
|
|
2016-06-30 08:24:03 +00:00
|
|
|
check_online_service
|
2015-08-29 11:46:48 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
adapter = params[:adapter].downcase
|
|
|
|
|
|
|
|
email = Setting.get('notification_sender')
|
|
|
|
|
|
|
|
# connection test
|
|
|
|
result = EmailHelper::Probe.outbound(params, email)
|
|
|
|
|
|
|
|
# save settings
|
|
|
|
if result[:result] == 'ok'
|
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
Channel.where(area: 'Email::Notification').each do |channel|
|
2015-08-28 00:53:14 +00:00
|
|
|
active = false
|
2021-05-12 11:37:44 +00:00
|
|
|
if adapter.match?(%r{^#{channel.options[:outbound][:adapter]}$}i)
|
2015-08-28 00:53:14 +00:00
|
|
|
active = true
|
|
|
|
channel.options = {
|
|
|
|
outbound: {
|
|
|
|
adapter: adapter,
|
2017-09-12 14:38:32 +00:00
|
|
|
options: params[:options].to_h,
|
2015-08-28 00:53:14 +00:00
|
|
|
},
|
|
|
|
}
|
2015-08-28 08:19:27 +00:00
|
|
|
channel.status_out = 'ok'
|
|
|
|
channel.last_log_out = nil
|
2015-08-28 00:53:14 +00:00
|
|
|
end
|
|
|
|
channel.active = active
|
|
|
|
channel.save
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-28 00:53:14 +00:00
|
|
|
end
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-02-15 02:35:22 +00:00
|
|
|
def account_duplicate?(result, channel_id = nil)
|
2017-10-01 12:25:52 +00:00
|
|
|
Channel.where(area: 'Email::Account').each do |channel|
|
2015-08-28 00:53:14 +00:00
|
|
|
next if !channel.options
|
|
|
|
next if !channel.options[:inbound]
|
|
|
|
next if !channel.options[:inbound][:adapter]
|
|
|
|
next if channel.options[:inbound][:adapter] != result[:setting][:inbound][:adapter]
|
|
|
|
next if channel.options[:inbound][:options][:host] != result[:setting][:inbound][:options][:host]
|
|
|
|
next if channel.options[:inbound][:options][:user] != result[:setting][:inbound][:options][:user]
|
2017-03-06 11:13:05 +00:00
|
|
|
next if channel.options[:inbound][:options][:folder].to_s != result[:setting][:inbound][:options][:folder].to_s
|
2015-08-28 00:53:14 +00:00
|
|
|
next if channel.id.to_s == channel_id.to_s
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-08-28 00:53:14 +00:00
|
|
|
render json: {
|
2018-12-19 17:31:51 +00:00
|
|
|
result: 'duplicate',
|
2015-08-28 00:53:14 +00:00
|
|
|
message: 'Account already exists!',
|
|
|
|
}
|
|
|
|
return true
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-28 00:53:14 +00:00
|
|
|
false
|
|
|
|
end
|
2015-08-29 11:46:48 +00:00
|
|
|
|
|
|
|
def check_online_service
|
|
|
|
return true if !Setting.get('system_online_service')
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2021-02-04 08:28:41 +00:00
|
|
|
raise Exceptions::Forbidden
|
2015-08-29 22:58:21 +00:00
|
|
|
end
|
2015-08-29 11:46:48 +00:00
|
|
|
|
|
|
|
def check_access(id = nil)
|
|
|
|
if !id
|
|
|
|
id = params[:id]
|
|
|
|
end
|
|
|
|
return true if !Setting.get('system_online_service')
|
|
|
|
|
|
|
|
channel = Channel.find(id)
|
|
|
|
return true if channel.preferences && !channel.preferences[:online_service_disable]
|
|
|
|
|
2021-02-04 08:28:41 +00:00
|
|
|
raise Exceptions::Forbidden
|
2015-08-29 11:46:48 +00:00
|
|
|
end
|
2012-04-13 13:51:10 +00:00
|
|
|
end
|