2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-07-26 01:01:27 +08:00
|
|
|
class EmailValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2022-02-18 09:12:51 +08:00
|
|
|
if !EmailAddressValidator.valid_value?(value)
|
2021-04-15 19:46:32 +08:00
|
|
|
if Invite === record && attribute == :email
|
2022-01-18 20:38:31 +08:00
|
|
|
record.errors.add(:base, I18n.t(:"invite.invalid_email", email: CGI.escapeHTML(value)))
|
2021-04-15 19:46:32 +08:00
|
|
|
else
|
|
|
|
record.errors.add(attribute, I18n.t(:"user.email.invalid"))
|
|
|
|
end
|
|
|
|
invalid = true
|
2020-06-03 10:13:25 +08:00
|
|
|
end
|
|
|
|
|
2022-02-18 09:12:51 +08:00
|
|
|
if !EmailValidator.allowed?(value)
|
2017-10-03 17:23:18 +08:00
|
|
|
record.errors.add(attribute, I18n.t(:"user.email.not_allowed"))
|
2021-04-15 19:46:32 +08:00
|
|
|
invalid = true
|
2013-07-26 01:01:27 +08:00
|
|
|
end
|
2017-10-03 17:23:18 +08:00
|
|
|
|
2021-04-15 19:46:32 +08:00
|
|
|
if !invalid && ScreenedEmail.should_block?(value)
|
2013-07-26 01:01:27 +08:00
|
|
|
record.errors.add(attribute, I18n.t(:"user.email.blocked"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-03 17:23:18 +08:00
|
|
|
def self.allowed?(email)
|
2020-07-27 08:23:54 +08:00
|
|
|
if (setting = SiteSetting.allowed_email_domains).present?
|
2017-10-03 17:23:18 +08:00
|
|
|
return email_in_restriction_setting?(setting, email) || is_developer?(email)
|
2020-07-27 08:23:54 +08:00
|
|
|
elsif (setting = SiteSetting.blocked_email_domains).present?
|
2017-10-03 17:23:18 +08:00
|
|
|
return !(email_in_restriction_setting?(setting, email) && !is_developer?(email))
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-04-01 02:29:15 +08: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 17:23:18 +08:00
|
|
|
def self.email_in_restriction_setting?(setting, value)
|
2013-07-26 01:01:27 +08:00
|
|
|
domains = setting.gsub(".", '\.')
|
2018-01-18 04:45:32 +08:00
|
|
|
regexp = Regexp.new("@(.+\\.)?(#{domains})$", true)
|
2013-07-26 01:01:27 +08:00
|
|
|
value =~ regexp
|
|
|
|
end
|
|
|
|
|
2017-10-03 17:23:18 +08:00
|
|
|
def self.is_developer?(value)
|
2015-01-30 01:52:59 +08:00
|
|
|
Rails.configuration.respond_to?(:developer_emails) &&
|
|
|
|
Rails.configuration.developer_emails.include?(value)
|
|
|
|
end
|
|
|
|
|
2014-06-10 03:17:36 +08:00
|
|
|
def self.email_regex
|
2022-02-18 09:12:51 +08:00
|
|
|
Discourse.deprecate(
|
|
|
|
"EmailValidator.email_regex is deprecated. Please use EmailAddressValidator instead.",
|
|
|
|
output_in_test: true,
|
|
|
|
drop_from: "2.9.0",
|
|
|
|
)
|
|
|
|
EmailAddressValidator.email_regex
|
2014-06-10 03:17:36 +08:00
|
|
|
end
|
2014-10-24 01:25:49 +08:00
|
|
|
end
|