Validate username site settings

This commit is contained in:
Neil Lalonde 2014-06-11 14:42:41 -04:00
parent afb178ba1f
commit 9611a1ac47
6 changed files with 57 additions and 4 deletions

View file

@ -915,6 +915,7 @@ en:
errors:
invalid_email: "Invalid email address."
invalid_username: "There's no user with that username."
notification_types:
mentioned: "%{display_username} mentioned you in %{link}"

View file

@ -9,7 +9,9 @@ required:
notification_email:
default: 'info@discourse.org'
validator: 'EmailSettingValidator'
site_contact_username: ''
site_contact_username:
default: ''
validator: 'UsernameSettingValidator'
logo_url:
client: true
default: '/images/d-logo-sketch.png'
@ -316,7 +318,9 @@ email:
pop3s_polling_enabled: false
pop3s_polling_host: ''
pop3s_polling_port: 995
pop3s_polling_username: ''
pop3s_polling_username:
default: ''
validator: 'UsernameSettingValidator'
pop3s_polling_password: ''
email_in:
default: false
@ -484,7 +488,9 @@ embedding:
embeddable_host: ''
feed_polling_enabled: false
feed_polling_url: ''
embed_by_username: ''
embed_by_username:
default: ''
validator: 'UsernameSettingValidator'
embed_category: ''
embed_post_limit: 100
embed_truncate: false

View file

@ -1,6 +1,6 @@
class EmailSettingValidator
def self.valid_value?(val)
val == '' || EmailValidator.email_regex =~ val
!val.present? || !!(EmailValidator.email_regex =~ val)
end
def self.error_message(val)

View file

@ -0,0 +1,9 @@
class UsernameSettingValidator
def self.valid_value?(val)
!val.present? || User.where(username: val).exists?
end
def self.error_message(val)
I18n.t('site_settings.errors.invalid_username')
end
end

View file

@ -0,0 +1,18 @@
require 'spec_helper'
describe EmailSettingValidator do
describe '#valid_value?' do
it "returns true for blank values" do
described_class.valid_value?('').should == true
described_class.valid_value?(nil).should == true
end
it "returns true if value is a valid email address" do
described_class.valid_value?('vader@example.com').should == true
end
it "returns false if value is not a valid email address" do
described_class.valid_value?('my house').should == false
end
end
end

View file

@ -0,0 +1,19 @@
require 'spec_helper'
describe UsernameSettingValidator do
describe '#valid_value?' do
it "returns true for blank values" do
described_class.valid_value?('').should == true
described_class.valid_value?(nil).should == true
end
it "returns true if value matches an existing user's username" do
Fabricate(:user, username: 'vader')
described_class.valid_value?('vader').should == true
end
it "returns false if value does not match a user's username" do
described_class.valid_value?('no way').should == false
end
end
end