From 7e8b3adc3b03bd1dd5fcc55a09adc206bd73c94c Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 28 Aug 2015 10:19:27 +0200 Subject: [PATCH] Some small improvements to email channel management. --- .../app/controllers/_channel/email.js.coffee | 4 +-- .../channel/email_account_overview.jst.eco | 7 ++-- app/controllers/channels_controller.rb | 28 ++++++++------- app/models/channel.rb | 12 +++++++ app/models/email_address.rb | 35 ++++++++++++++++--- lib/auto_wizard.rb | 2 +- lib/email_helper/probe.rb | 19 +++++----- 7 files changed, 77 insertions(+), 30 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_channel/email.js.coffee b/app/assets/javascripts/app/controllers/_channel/email.js.coffee index 173e6ed49..79553cf53 100644 --- a/app/assets/javascripts/app/controllers/_channel/email.js.coffee +++ b/app/assets/javascripts/app/controllers/_channel/email.js.coffee @@ -7,8 +7,8 @@ class App.ChannelEmail extends App.ControllerTabs @tabs = [ { - name: 'Email Accounts', - target: 'c-channel', + name: 'Accounts', + target: 'c-account', controller: App.ChannelEmailAccountOverview, }, { diff --git a/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco b/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco index c9771e8a3..48908298b 100644 --- a/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco +++ b/app/assets/javascripts/app/views/channel/email_account_overview.jst.eco @@ -23,8 +23,10 @@ <%- @T('State') %>: <%- @T(channel.status_in || 'unknown') %>
+ <%= channel.options.inbound.options.user %>
-
<%= channel.options.inbound.options.host %> (<%= channel.options.inbound.adapter %>) + <%= channel.options.inbound.options.host %> (<%= channel.options.inbound.adapter %>) + <%- @T('State') %>: <%- @T(channel.status_out || 'unknown') %>
@@ -79,9 +81,10 @@ <%- @T('State') %>: <%- @T(@channel.status_out || 'unknown') %>
+ <% if @channel.options.outbound && @channel.options.outbound.options: %> <%= @channel.options.outbound.options.user %>
-
<%= @channel.options.outbound.options.host %> + <%= @channel.options.outbound.options.host %> <% end %> (<%= @channel.options.outbound.adapter %>) diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb index 5d6b32221..961f005e1 100644 --- a/app/controllers/channels_controller.rb +++ b/app/controllers/channels_controller.rb @@ -341,10 +341,10 @@ curl http://localhost/api/v1/channels.json -v -u #{login}:#{password} -H "Conten inbound: params[:inbound], outbound: params[:outbound], }, - last_log_in: '', - last_log_out: '', - status_in: nil, - status_out: nil, + last_log_in: nil, + last_log_out: nil, + status_in: 'ok', + status_out: 'ok', ) render json: { result: 'ok', @@ -399,6 +399,15 @@ curl http://localhost/api/v1/channels.json -v -u #{login}:#{password} -H "Conten adapter = params[:adapter].downcase + # validate adapter + if adapter !~ /^(smtp|sendmail)$/ + render json: { + result: 'failed', + message: "Unknown adapter '#{adapter}'", + } + return + end + email = Setting.get('notification_sender') # connection test @@ -407,15 +416,6 @@ curl http://localhost/api/v1/channels.json -v -u #{login}:#{password} -H "Conten # save settings if result[:result] == 'ok' - # validate adapter - if adapter !~ /^(smtp|sendmail)$/ - render json: { - result: 'failed', - message: "Unknown adapter '#{adapter}'", - } - return - end - Channel.where(area: 'Email::Notification').each {|channel| active = false if adapter =~ /^#{channel.options[:outbound][:adapter]}$/i @@ -426,6 +426,8 @@ curl http://localhost/api/v1/channels.json -v -u #{login}:#{password} -H "Conten options: params[:options], }, } + channel.status_out = 'ok' + channel.last_log_out = nil end channel.active = active channel.save diff --git a/app/models/channel.rb b/app/models/channel.rb index c7c58240d..ed41bf220 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -3,6 +3,10 @@ class Channel < ApplicationModel store :options + after_create :email_address_check + after_update :email_address_check + after_destroy :email_address_check + =begin fetch all accounts @@ -108,4 +112,12 @@ send via account result end + private + + def email_address_check + + # reset non existing channel_ids + EmailAddress.channel_cleanup + end + end diff --git a/app/models/email_address.rb b/app/models/email_address.rb index 0545eef3e..691654d3e 100644 --- a/app/models/email_address.rb +++ b/app/models/email_address.rb @@ -1,11 +1,38 @@ # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/ class EmailAddress < ApplicationModel - has_many :groups, after_add: :cache_update, after_remove: :cache_update - belongs_to :channel - validates :realname, presence: true - validates :email, presence: true + has_many :groups, after_add: :cache_update, after_remove: :cache_update + belongs_to :channel + validates :realname, presence: true + validates :email, presence: true + + before_create :channel_check + before_update :channel_check latest_change_support +=begin + +check and if channel not exists reset configured channels for email addresses + + EmailAddress.channel_cleanup + +=end + + def self.channel_cleanup + EmailAddress.all.each {|email_address| + next if !email_address.channel_id + next if Channel.find_by(id: email_address.channel_id) + email_address.channel_id = nil + email_address.save + } + end + + private + + def channel_check + return if Channel.find_by(id: channel_id) + self.channel_id = nil + end + end diff --git a/lib/auto_wizard.rb b/lib/auto_wizard.rb index b1cfdb703..9c0954a2a 100644 --- a/lib/auto_wizard.rb +++ b/lib/auto_wizard.rb @@ -118,8 +118,8 @@ returns # create EmailAddresses/Channels/Signatures model_map = { - 'EmailAddresses' => 'EmailAddress', 'Channels' => 'Channel', + 'EmailAddresses' => 'EmailAddress', 'Signatures' => 'Signature', } model_map.each {|map_name, model| diff --git a/lib/email_helper/probe.rb b/lib/email_helper/probe.rb index 27608aa9d..21195de54 100644 --- a/lib/email_helper/probe.rb +++ b/lib/email_helper/probe.rb @@ -272,20 +272,23 @@ returns on fail # prepare test email if subject mail = { - :from => email, - :to => email, - :subject => "Zammad Getting started Test Email #{subject}", - :body => "This is a Test Email of Zammad to check if sending and receiving is working correctly.\n\nYou can ignore or delete this email.", - 'x-zammad-ignore' => 'true', + from: email, + to: email, + subject: "Zammad Getting started Test Email #{subject}", + body: "This is a Test Email of Zammad to check if sending and receiving is working correctly.\n\nYou can ignore or delete this email.", } else mail = { - from: email, - to: 'emailtrytest@znuny.com', + from: email, + to: 'emailtrytest@znuny.com', subject: 'This is a Test Email', - body: "This is a Test Email of Zammad to verify if Zammad can send emails to an external address.\n\nIf you see this email, you can ignore and delete it.", + body: "This is a Test Email of Zammad to verify if Zammad can send emails to an external address.\n\nIf you see this email, you can ignore and delete it.", } end + mail['X-Zammad-Ignore'] = 'true' + mail['X-Loop'] = 'yes' + mail['Precedence'] = 'bulk' + mail['Auto-Submitted'] = 'auto-generated' # test connection begin