"Fossies" - the Fresh Open Source Software Archive

Member "redmine-4.1.1/app/models/email_address.rb" (6 Apr 2020, 3889 Bytes) of package /linux/www/redmine-4.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "email_address.rb": 4.1.0_vs_4.1.1.

    1 # frozen_string_literal: true
    2 
    3 # Redmine - project management software
    4 # Copyright (C) 2006-2019  Jean-Philippe Lang
    5 #
    6 # This program is free software; you can redistribute it and/or
    7 # modify it under the terms of the GNU General Public License
    8 # as published by the Free Software Foundation; either version 2
    9 # of the License, or (at your option) any later version.
   10 #
   11 # This program is distributed in the hope that it will be useful,
   12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14 # GNU General Public License for more details.
   15 #
   16 # You should have received a copy of the GNU General Public License
   17 # along with this program; if not, write to the Free Software
   18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19 
   20 class EmailAddress < ActiveRecord::Base
   21   include Redmine::SafeAttributes
   22 
   23   EMAIL_REGEXP = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+(?:(?:xn--[-a-z0-9]+)|(?:[a-z]{2,})))\z/i
   24 
   25   belongs_to :user
   26 
   27   after_update :destroy_tokens
   28   after_destroy :destroy_tokens
   29 
   30   after_create_commit :deliver_security_notification_create
   31   after_update_commit :deliver_security_notification_update
   32   after_destroy_commit :deliver_security_notification_destroy
   33 
   34   validates_presence_of :address
   35   validates_format_of :address, :with => EMAIL_REGEXP, :allow_blank => true
   36   validates_length_of :address, :maximum => User::MAIL_LENGTH_LIMIT, :allow_nil => true
   37   validates_uniqueness_of :address, :case_sensitive => false,
   38     :if => Proc.new {|email| email.address_changed? && email.address.present?}
   39 
   40   safe_attributes 'address'
   41 
   42   def address=(arg)
   43     write_attribute(:address, arg.to_s.strip)
   44   end
   45 
   46   def destroy
   47     if is_default?
   48       false
   49     else
   50       super
   51     end
   52   end
   53 
   54   private
   55 
   56   # send a security notification to user that a new email address was added
   57   def deliver_security_notification_create
   58     # only deliver if this isn't the only address.
   59     # in that case, the user is just being created and
   60     # should not receive this email.
   61     if user.mails != [address]
   62       deliver_security_notification(
   63         message: :mail_body_security_notification_add,
   64         field: :field_mail,
   65         value: address
   66       )
   67     end
   68   end
   69 
   70   # send a security notification to user that an email has been changed (notified/not notified)
   71   def deliver_security_notification_update
   72     if saved_change_to_address?
   73       options = {
   74         recipients: [address_before_last_save],
   75         message: :mail_body_security_notification_change_to,
   76         field: :field_mail,
   77         value: address
   78       }
   79     elsif saved_change_to_notify?
   80       options = {
   81         recipients: [address],
   82         message: notify_before_last_save ? :mail_body_security_notification_notify_disabled : :mail_body_security_notification_notify_enabled,
   83         value: address
   84       }
   85     end
   86     deliver_security_notification(options)
   87   end
   88 
   89   # send a security notification to user that an email address was deleted
   90   def deliver_security_notification_destroy
   91     deliver_security_notification(
   92       recipients: [address],
   93       message: :mail_body_security_notification_remove,
   94       field: :field_mail,
   95       value: address
   96     )
   97   end
   98 
   99   # generic method to send security notifications for email addresses
  100   def deliver_security_notification(options={})
  101     Mailer.deliver_security_notification(
  102       user,
  103       User.current,
  104       options.merge(
  105         title: :label_my_account,
  106         url: {controller: 'my', action: 'account'}
  107       )
  108     )
  109   end
  110 
  111   # Delete all outstanding password reset tokens on email change.
  112   # This helps to keep the account secure in case the associated email account
  113   # was compromised.
  114   def destroy_tokens
  115     if saved_change_to_address? || destroyed?
  116       tokens = ['recovery']
  117       Token.where(:user_id => user_id, :action => tokens).delete_all
  118     end
  119   end
  120 end