2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-07-25 13:01:27 -04:00
|
|
|
class EmailValidator < ActiveModel::EachValidator
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
2020-06-02 22:13:25 -04:00
|
|
|
unless value =~ EmailValidator.email_regex
|
2021-04-15 07:46:32 -04:00
|
|
|
if Invite === record && attribute == :email
|
|
|
|
record.errors.add(:base, I18n.t(:'invite.invalid_email', email: value))
|
|
|
|
else
|
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.invalid'))
|
|
|
|
end
|
|
|
|
invalid = true
|
2020-06-02 22:13:25 -04:00
|
|
|
end
|
|
|
|
|
2017-10-03 05:23:18 -04:00
|
|
|
unless EmailValidator.allowed?(value)
|
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.not_allowed'))
|
2021-04-15 07:46:32 -04:00
|
|
|
invalid = true
|
2013-07-25 13:01:27 -04:00
|
|
|
end
|
2017-10-03 05:23:18 -04:00
|
|
|
|
2021-04-15 07:46:32 -04:00
|
|
|
if !invalid && ScreenedEmail.should_block?(value)
|
2013-07-25 13:01:27 -04:00
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.blocked'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-03 05:23:18 -04:00
|
|
|
def self.allowed?(email)
|
2020-07-26 20:23:54 -04:00
|
|
|
if (setting = SiteSetting.allowed_email_domains).present?
|
2017-10-03 05:23:18 -04:00
|
|
|
return email_in_restriction_setting?(setting, email) || is_developer?(email)
|
2020-07-26 20:23:54 -04:00
|
|
|
elsif (setting = SiteSetting.blocked_email_domains).present?
|
2017-10-03 05:23:18 -04:00
|
|
|
return !(email_in_restriction_setting?(setting, email) && !is_developer?(email))
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-03-31 14:29:15 -04:00
|
|
|
def self.can_auto_approve_user?(email)
|
|
|
|
if (setting = SiteSetting.auto_approve_email_domains).present?
|
|
|
|
return !!(EmailValidator.allowed?(email) && email_in_restriction_setting?(setting, email))
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-10-03 05:23:18 -04:00
|
|
|
def self.email_in_restriction_setting?(setting, value)
|
2013-07-25 13:01:27 -04:00
|
|
|
domains = setting.gsub('.', '\.')
|
2018-01-17 15:45:32 -05:00
|
|
|
regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
|
2013-07-25 13:01:27 -04:00
|
|
|
value =~ regexp
|
|
|
|
end
|
|
|
|
|
2017-10-03 05:23:18 -04:00
|
|
|
def self.is_developer?(value)
|
2015-01-29 12:52:59 -05:00
|
|
|
Rails.configuration.respond_to?(:developer_emails) && Rails.configuration.developer_emails.include?(value)
|
|
|
|
end
|
|
|
|
|
2014-06-09 15:17:36 -04:00
|
|
|
def self.email_regex
|
2016-12-21 04:00:45 -05:00
|
|
|
/\A[a-zA-Z0-9!#\$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#\$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$\z/
|
2014-06-09 15:17:36 -04:00
|
|
|
end
|
|
|
|
|
2014-10-23 13:25:49 -04:00
|
|
|
end
|