From f850fe1e750d6621857c1d96762666a46b4fc9cd Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 13 Feb 2013 12:28:23 -0500 Subject: [PATCH] Implement the email_blacklist_regexp ssite setting --- app/models/user.rb | 10 ++++++++++ config/locales/en.yml | 2 ++ spec/models/user_spec.rb | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 777187df26c..70c1e096eee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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") diff --git a/config/locales/en.yml b/config/locales/en.yml index f8c2db0a056..c603865f6ae 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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}" diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8b46184b342..e1712d60d25 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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)