From aaf96f1e29fe4a06c8877fe91e21a12b5c422481 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 13 Feb 2013 22:24:32 -0500 Subject: [PATCH] Replace email_blacklist_regexp with email_domains_blacklist site setting --- app/models/site_setting.rb | 6 +----- app/models/user.rb | 5 +++-- config/locales/en.yml | 3 +-- spec/models/user_spec.rb | 23 ++++++++++++++--------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index d0b3f0c8b30..e7f9ce208b1 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -30,6 +30,7 @@ class SiteSetting < ActiveRecord::Base client_setting(:max_topic_title_length, 255) client_setting(:flush_timings_secs, 5) client_setting(:supress_reply_directly_below, true) + client_setting(:email_domains_blacklist, 'mailinator.com') # settings only available server side setting(:auto_track_topics_after, 60000) @@ -134,13 +135,8 @@ class SiteSetting < ActiveRecord::Base setting(:body_min_entropy, 7) setting(:max_word_length, 30) - # Ways to catch griefers and other nasties - setting(:email_blacklist_regexp, '') - setting(:new_user_period_days, 10) - - def self.call_mothership? self.enforce_global_nicknames? and self.discourse_org_access_key.present? end diff --git a/app/models/user.rb b/app/models/user.rb index 56cdff20ade..2be9c8d0715 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -486,8 +486,9 @@ class User < ActiveRecord::Base end def email_validator - if (setting = SiteSetting.email_blacklist_regexp.try(:strip)).present? - regexp = Regexp.new(setting, true) + if (setting = SiteSetting.email_domains_blacklist).present? + domains = setting.gsub('.', '\.') + regexp = Regexp.new("@(#{domains})", true) if self.email =~ regexp return errors.add(:email, I18n.t(:'user.email.not_allowed')) end diff --git a/config/locales/en.yml b/config/locales/en.yml index 215c05f3702..c12c68b8625 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -261,6 +261,7 @@ en: send_welcome_message: "Do new users get a welcome private message?" supress_reply_directly_below: "Don't show replies button below a post when the reply is directly below" allow_index_in_robots_txt: "Site should be indexed by search engines (update robots.txt)" + email_domains_blacklist: "A pipe-delimited list of email domains that are not allowed. Example: mailinator.com|trashmail.net" port: "If you'd like to specify a port in the URL. Useful in development mode. Leave blank for none." force_hostname: "If you'd like to specify a hostname in the URL. Useful in development mode. Leave blank for none." @@ -312,8 +313,6 @@ en: title_min_entropy: "The minimum entropy for a topic title" body_min_entropy: "The minimum entropy for post body" - email_blacklist_regexp: "A regexp that finds email addresses to block" - new_user_period_days: "How long a user is highlighted as being new, in days." # This section is exported to the javascript for i18n in the admin section diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 2332011ac00..a5174b50368 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -456,22 +456,27 @@ describe User do 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') + it 'should reject some emails based on the email_domains_blacklist site setting' do + SiteSetting.stubs(:email_domains_blacklist).returns('mailinator.com') 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') + it 'should reject some emails based on the email_domains_blacklist site setting' do + SiteSetting.stubs(:email_domains_blacklist).returns('mailinator.com|trashmail.net') 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 + Fabricate.build(:user, email: 'notgood@trashmail.net').should_not be_valid + Fabricate.build(:user, email: 'mailinator.com@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 + it 'should reject some emails based on the email_domains_blacklist site setting ignoring case' do + SiteSetting.stubs(:email_domains_blacklist).returns('trashmail.net') + Fabricate.build(:user, email: 'notgood@TRASHMAIL.NET').should_not be_valid + end + + it 'should not interpret a period as a wildcard' do + SiteSetting.stubs(:email_domains_blacklist).returns('trashmail.net') + Fabricate.build(:user, email: 'good@trashmailinet.com').should be_valid end end