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?
|
|
|
|
unless email_in_restriction_setting?(setting, value)
|
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.not_allowed'))
|
|
|
|
end
|
|
|
|
elsif (setting = SiteSetting.email_domains_blacklist).present?
|
|
|
|
if email_in_restriction_setting?(setting, value)
|
|
|
|
record.errors.add(attribute, I18n.t(:'user.email.not_allowed'))
|
|
|
|
end
|
|
|
|
end
|
2013-08-14 23:05:53 +08:00
|
|
|
if record.errors[attribute].blank? and 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('.', '\.')
|
|
|
|
regexp = Regexp.new("@(#{domains})", true)
|
|
|
|
value =~ regexp
|
|
|
|
end
|
|
|
|
|
2014-06-10 03:17:36 +08:00
|
|
|
def self.email_regex
|
|
|
|
/^[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])?$/
|
|
|
|
end
|
|
|
|
|
2013-07-26 01:01:27 +08:00
|
|
|
end
|