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.7KB

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