Class: EmailAddress

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::SafeAttributes
Defined in:
app/models/email_address.rb

Overview

Redmine - project management software Copyright © 2006-2022 Jean-Philippe Lang

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Constant Summary collapse

EMAIL_REGEXP =
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+(?:(?:xn--[-a-z0-9]+)|(?:[a-z]{2,})))\z/i

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, included, #safe_attribute?, #safe_attribute_names, #safe_attributes=

Class Method Details

.domain_in?(domain, domains) ⇒ Boolean

Returns true if domain belongs to domains list.

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'app/models/email_address.rb', line 70

def self.domain_in?(domain, domains)
  domain = domain.downcase
  domains = domains.to_s.split(/[\s,]+/) unless domains.is_a?(Array)
  domains.reject(&:blank?).map(&:downcase).any? do |s|
    s.start_with?('.') ? domain.end_with?(s) : domain == s
  end
end

.valid_domain?(domain_or_email) ⇒ Boolean

Returns true if the email domain is allowed regarding allowed/denied domains defined in application settings, otherwise false

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/email_address.rb', line 57

def self.valid_domain?(domain_or_email)
  denied, allowed =
    [:email_domains_denied, :email_domains_allowed].map do |setting|
      Setting.__send__(setting)
    end
  domain = domain_or_email.split('@').last
  return false if denied.present? && domain_in?(domain, denied)
  return false if allowed.present? && !domain_in?(domain, allowed)

  true
end

Instance Method Details

#address=(arg) ⇒ Object



43
44
45
# File 'app/models/email_address.rb', line 43

def address=(arg)
  write_attribute(:address, arg.to_s.strip)
end

#destroyObject



47
48
49
50
51
52
53
# File 'app/models/email_address.rb', line 47

def destroy
  if is_default?
    false
  else
    super
  end
end