Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

email_address.rb 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. belongs_to :user
  21. after_update :destroy_tokens
  22. after_destroy :destroy_tokens
  23. after_create_commit :deliver_security_notification_create
  24. after_update_commit :deliver_security_notification_update
  25. after_destroy_commit :deliver_security_notification_destroy
  26. validates_presence_of :address
  27. validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true
  28. validates_length_of :address, :maximum => User::MAIL_LENGTH_LIMIT, :allow_nil => true
  29. validates_uniqueness_of :address, :case_sensitive => false,
  30. :if => Proc.new {|email| email.address_changed? && email.address.present?}
  31. safe_attributes 'address'
  32. def address=(arg)
  33. write_attribute(:address, arg.to_s.strip)
  34. end
  35. def destroy
  36. if is_default?
  37. false
  38. else
  39. super
  40. end
  41. end
  42. private
  43. # send a security notification to user that a new email address was added
  44. def deliver_security_notification_create
  45. # only deliver if this isn't the only address.
  46. # in that case, the user is just being created and
  47. # should not receive this email.
  48. if user.mails != [address]
  49. deliver_security_notification(
  50. message: :mail_body_security_notification_add,
  51. field: :field_mail,
  52. value: address
  53. )
  54. end
  55. end
  56. # send a security notification to user that an email has been changed (notified/not notified)
  57. def deliver_security_notification_update
  58. if saved_change_to_address?
  59. options = {
  60. recipients: [address_before_last_save],
  61. message: :mail_body_security_notification_change_to,
  62. field: :field_mail,
  63. value: address
  64. }
  65. elsif saved_change_to_notify?
  66. options = {
  67. recipients: [address],
  68. message: notify_before_last_save ? :mail_body_security_notification_notify_disabled : :mail_body_security_notification_notify_enabled,
  69. value: address
  70. }
  71. end
  72. deliver_security_notification(options)
  73. end
  74. # send a security notification to user that an email address was deleted
  75. def deliver_security_notification_destroy
  76. deliver_security_notification(
  77. recipients: [address],
  78. message: :mail_body_security_notification_remove,
  79. field: :field_mail,
  80. value: address
  81. )
  82. end
  83. # generic method to send security notifications for email addresses
  84. def deliver_security_notification(options={})
  85. Mailer.deliver_security_notification(user,
  86. User.current,
  87. options.merge(
  88. title: :label_my_account,
  89. url: {controller: 'my', action: 'account'}
  90. )
  91. )
  92. end
  93. # Delete all outstanding password reset tokens on email change.
  94. # This helps to keep the account secure in case the associated email account
  95. # was compromised.
  96. def destroy_tokens
  97. if saved_change_to_address? || destroyed?
  98. tokens = ['recovery']
  99. Token.where(:user_id => user_id, :action => tokens).delete_all
  100. end
  101. end
  102. end