diff --git a/app/models/user.rb b/app/models/user.rb index 47d5eb24a..e9ae855b7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,7 +27,7 @@ class User < ActiveRecord::Base validates_presence_of :email validates_uniqueness_of :email validate :username_validator - validate :email_validator + validate :email_validator, :if => :email_changed? validate :password_validator before_save :cook @@ -506,7 +506,7 @@ class User < ActiveRecord::Base end def email_validator - if new_record? and (setting = SiteSetting.email_domains_blacklist).present? + if (setting = SiteSetting.email_domains_blacklist).present? domains = setting.gsub('.', '\.') regexp = Regexp.new("@(#{domains})", true) if self.email =~ regexp diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a8b740340..1771f9b2e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -484,6 +484,13 @@ describe User do SiteSetting.stubs(:email_domains_blacklist).returns('fakemail.com') u.should be_valid end + + it 'should be used when email is being changed' do + SiteSetting.stubs(:email_domains_blacklist).returns('mailinator.com') + u = Fabricate(:user, email: 'good@gmail.com') + u.email = 'nope@mailinator.com' + u.should_not be_valid + end end describe 'passwords' do