2013-07-26 01:01:27 +08:00
|
|
|
class EmailValidator < ActiveModel::EachValidator
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
if (setting = SiteSetting.email_domains_whitelist).present?
|
2015-01-30 01:52:59 +08:00
|
|
|
unless email_in_restriction_setting?(setting, value) || is_developer?(value)
|
2013-07-26 01:01:27 +08:00
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.not_allowed'))
|
|
|
|
end
|
|
|
|
elsif (setting = SiteSetting.email_domains_blacklist).present?
|
2015-01-30 01:52:59 +08:00
|
|
|
if email_in_restriction_setting?(setting, value) && !is_developer?(value)
|
2013-07-26 01:01:27 +08:00
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.not_allowed'))
|
|
|
|
end
|
|
|
|
end
|
2014-10-24 01:25:49 +08:00
|
|
|
if record.errors[attribute].blank? && value && ScreenedEmail.should_block?(value)
|
2013-07-26 01:01:27 +08:00
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.blocked'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_in_restriction_setting?(setting, value)
|
|
|
|
domains = setting.gsub('.', '\.')
|
2015-06-15 09:28:50 +08:00
|
|
|
regexp = Regexp.new("@(.+\\.)?(#{domains})", true)
|
2013-07-26 01:01:27 +08:00
|
|
|
value =~ regexp
|
|
|
|
end
|
|
|
|
|
2015-01-30 01:52:59 +08:00
|
|
|
def is_developer?(value)
|
|
|
|
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
|
2016-12-21 17:00:45 +08: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-10 03:17:36 +08:00
|
|
|
end
|
|
|
|
|
2014-10-24 01:25:49 +08:00
|
|
|
end
|