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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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]+\.)+(?:(?:xn--[-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. validate :validate_email_domain, :if => proc {|email| email.address.present?}
  33. safe_attributes 'address'
  34. def address=(arg)
  35. write_attribute(:address, arg.to_s.strip)
  36. end
  37. def destroy
  38. if is_default?
  39. false
  40. else
  41. super
  42. end
  43. end
  44. # Returns true if the email domain is allowed regarding allowed/denied
  45. # domains defined in application settings, otherwise false
  46. def self.valid_domain?(domain_or_email)
  47. denied, allowed =
  48. [:email_domains_denied, :email_domains_allowed].map do |setting|
  49. Setting.__send__(setting)
  50. end
  51. domain = domain_or_email.split('@').last
  52. return false if denied.present? && domain_in?(domain, denied)
  53. return false if allowed.present? && !domain_in?(domain, allowed)
  54. true
  55. end
  56. # Returns true if domain belongs to domains list.
  57. def self.domain_in?(domain, domains)
  58. domain = domain.downcase
  59. domains = domains.to_s.split(/[\s,]+/) unless domains.is_a?(Array)
  60. domains.reject(&:blank?).map(&:downcase).any? do |s|
  61. s.start_with?('.') ? domain.end_with?(s) : domain == s
  62. end
  63. end
  64. private
  65. # send a security notification to user that a new email address was added
  66. def deliver_security_notification_create
  67. # only deliver if this isn't the only address.
  68. # in that case, the user is just being created and
  69. # should not receive this email.
  70. if user.mails != [address]
  71. deliver_security_notification(
  72. message: :mail_body_security_notification_add,
  73. field: :field_mail,
  74. value: address
  75. )
  76. end
  77. end
  78. # send a security notification to user that an email has been changed (notified/not notified)
  79. def deliver_security_notification_update
  80. if saved_change_to_address?
  81. options = {
  82. recipients: [address_before_last_save],
  83. message: :mail_body_security_notification_change_to,
  84. field: :field_mail,
  85. value: address
  86. }
  87. elsif saved_change_to_notify?
  88. options = {
  89. recipients: [address],
  90. message: notify_before_last_save ? :mail_body_security_notification_notify_disabled : :mail_body_security_notification_notify_enabled,
  91. value: address
  92. }
  93. end
  94. deliver_security_notification(options)
  95. end
  96. # send a security notification to user that an email address was deleted
  97. def deliver_security_notification_destroy
  98. deliver_security_notification(
  99. recipients: [address],
  100. message: :mail_body_security_notification_remove,
  101. field: :field_mail,
  102. value: address
  103. )
  104. end
  105. # generic method to send security notifications for email addresses
  106. def deliver_security_notification(options={})
  107. Mailer.deliver_security_notification(
  108. user,
  109. User.current,
  110. options.merge(
  111. title: :label_my_account,
  112. url: {controller: 'my', action: 'account'}
  113. )
  114. )
  115. end
  116. # Delete all outstanding password reset tokens on email change.
  117. # This helps to keep the account secure in case the associated email account
  118. # was compromised.
  119. def destroy_tokens
  120. if saved_change_to_address? || destroyed?
  121. tokens = ['recovery']
  122. Token.where(:user_id => user_id, :action => tokens).delete_all
  123. end
  124. end
  125. def validate_email_domain
  126. errors.add(:address, :invalid) unless self.class.valid_domain?(address)
  127. end
  128. end