Implement the email_blacklist_regexp ssite setting

This commit is contained in:
Neil Lalonde 2013-02-13 12:28:23 -05:00
parent 546016bfd2
commit f850fe1e75
3 changed files with 37 additions and 0 deletions

View File

@ -28,6 +28,7 @@ class User < ActiveRecord::Base
validates_presence_of :email
validates_uniqueness_of :email
validate :username_validator
validate :email_validator
validate :password_validator
before_save :cook
@ -481,6 +482,15 @@ class User < ActiveRecord::Base
end
end
def email_validator
if (setting = SiteSetting.email_blacklist_regexp.try(:strip)).present?
regexp = Regexp.new(setting, true)
if self.email =~ regexp
return errors.add(:email, I18n.t(:'user.email.not_allowed'))
end
end
end
def password_validator
if (@raw_password and @raw_password.length < 6) or (@password_required and !@raw_password)
return errors.add(:password, "must be 6 letters or longer")

View File

@ -1052,6 +1052,8 @@ en:
unique: "must be unique"
blank: "must be present"
must_begin_with_alphanumeric: "must begin with a letter or number"
email:
not_allowed: "is not allowed from that email provider. Please use another email address."
invite_mailer:
subject_template: "[%{site_name}] %{invitee_name} invited you to join a discussion on %{site_name}"

View File

@ -445,6 +445,31 @@ describe User do
end
end
describe 'email_validator' do
it 'should allow good emails' do
user = Fabricate.build(:user, email: 'good@gmail.com')
user.should be_valid
end
it 'should reject some emails based on the email_blacklist_regexp site setting' do
SiteSetting.stubs(:email_blacklist_regexp).returns('@mailinator')
Fabricate.build(:user, email: 'notgood@mailinator.com').should_not be_valid
Fabricate.build(:user, email: 'mailinator@gmail.com').should be_valid
end
it 'should reject some emails based on the email_blacklist_regexp site setting' do
SiteSetting.stubs(:email_blacklist_regexp).returns('@(mailinator|aol)\.com')
Fabricate.build(:user, email: 'notgood@mailinator.com').should_not be_valid
Fabricate.build(:user, email: 'notgood@aol.com').should_not be_valid
Fabricate.build(:user, email: 'aol+mailinator@gmail.com').should be_valid
end
it 'should reject some emails based on the email_blacklist_regexp site setting ignoring case' do
SiteSetting.stubs(:email_blacklist_regexp).returns('@mailinator')
Fabricate.build(:user, email: 'notgood@MAILINATOR.COM').should_not be_valid
end
end
describe 'passwords' do
before do
@user = Fabricate.build(:user)