1
0
mirror of https://github.com/meineerde/redmine.git synced 2025-12-31 12:49:38 +00:00
redmine/app/models/email_address.rb
Jean-Philippe Lang 015ca36634 Send individual emails for each mail recipient (#26791).
We are creating multipe mails per class notification event, one per
recipient, wrapped in a Mailer::MultiMessage object to send them all at
once.

We keep the existing interface of all class methods intended to be used
by external code the same as they were before, with one exception:

We provide additional recipient addresses in options[:recipients] for
Mailer.security_notification. Since the first-class recipients have to
be users to render individual mails for them, additional recipient
addresses have to be provided with some other channel.

By providing additional recipients in options[:recipients], we can solve
the use-case for address change notifications for users, which probably
is the only real use-case for having to use a plain email address
instead of a User as a notification recipient.

Patch by Holger Just and Marius BALTEANU.

git-svn-id: http://svn.redmine.org/redmine/trunk@17583 e93f8b46-1217-0410-a6f0-8f06a7374b81
2018-10-06 13:08:52 +00:00

112 lines
3.6 KiB
Ruby

# Redmine - project management software
# Copyright (C) 2006-2017 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class EmailAddress < ActiveRecord::Base
include Redmine::SafeAttributes
belongs_to :user
after_create :deliver_security_notification_create
after_update :destroy_tokens, :deliver_security_notification_update
after_destroy :destroy_tokens, :deliver_security_notification_destroy
validates_presence_of :address
validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true
validates_length_of :address, :maximum => User::MAIL_LENGTH_LIMIT, :allow_nil => true
validates_uniqueness_of :address, :case_sensitive => false,
:if => Proc.new {|email| email.address_changed? && email.address.present?}
safe_attributes 'address'
def address=(arg)
write_attribute(:address, arg.to_s.strip)
end
def destroy
if is_default?
false
else
super
end
end
private
# send a security notification to user that a new email address was added
def deliver_security_notification_create
# only deliver if this isn't the only address.
# in that case, the user is just being created and
# should not receive this email.
if user.mails != [address]
deliver_security_notification(
message: :mail_body_security_notification_add,
field: :field_mail,
value: address
)
end
end
# send a security notification to user that an email has been changed (notified/not notified)
def deliver_security_notification_update
if saved_change_to_address?
options = {
recipients: [address_before_last_save],
message: :mail_body_security_notification_change_to,
field: :field_mail,
value: address
}
elsif saved_change_to_notify?
options = {
recipients: [address],
message: notify_before_last_save ? :mail_body_security_notification_notify_disabled : :mail_body_security_notification_notify_enabled,
value: address
}
end
deliver_security_notification(options)
end
# send a security notification to user that an email address was deleted
def deliver_security_notification_destroy
deliver_security_notification(
recipients: [address],
message: :mail_body_security_notification_remove,
field: :field_mail,
value: address
)
end
# generic method to send security notifications for email addresses
def deliver_security_notification(options={})
Mailer.security_notification(user,
options.merge(
title: :label_my_account,
url: {controller: 'my', action: 'account'}
)
).deliver
end
# Delete all outstanding password reset tokens on email change.
# This helps to keep the account secure in case the associated email account
# was compromised.
def destroy_tokens
if saved_change_to_address? || destroyed?
tokens = ['recovery']
Token.where(:user_id => user_id, :action => tokens).delete_all
end
end
end