You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

email_address.rb 3.8KB

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