From 9611a1ac47116bd345d64fa95ebbdb29f40bcfd0 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 11 Jun 2014 14:42:41 -0400 Subject: [PATCH] Validate username site settings --- config/locales/server.en.yml | 1 + config/site_settings.yml | 12 +++++++++--- lib/validators/email_setting_validator.rb | 2 +- lib/validators/username_setting_validator.rb | 9 +++++++++ .../email_setting_validator_spec.rb | 18 ++++++++++++++++++ .../username_setting_validator_spec.rb | 19 +++++++++++++++++++ 6 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 lib/validators/username_setting_validator.rb create mode 100644 spec/components/validators/email_setting_validator_spec.rb create mode 100644 spec/components/validators/username_setting_validator_spec.rb diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index faa880d16..8118d7b34 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -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}" diff --git a/config/site_settings.yml b/config/site_settings.yml index e1bb6d2de..addcc309d 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -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 diff --git a/lib/validators/email_setting_validator.rb b/lib/validators/email_setting_validator.rb index fae1c78a5..f499fb225 100644 --- a/lib/validators/email_setting_validator.rb +++ b/lib/validators/email_setting_validator.rb @@ -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) diff --git a/lib/validators/username_setting_validator.rb b/lib/validators/username_setting_validator.rb new file mode 100644 index 000000000..ce05cae5c --- /dev/null +++ b/lib/validators/username_setting_validator.rb @@ -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 diff --git a/spec/components/validators/email_setting_validator_spec.rb b/spec/components/validators/email_setting_validator_spec.rb new file mode 100644 index 000000000..c7ba6e5dc --- /dev/null +++ b/spec/components/validators/email_setting_validator_spec.rb @@ -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 diff --git a/spec/components/validators/username_setting_validator_spec.rb b/spec/components/validators/username_setting_validator_spec.rb new file mode 100644 index 000000000..52da3c9a5 --- /dev/null +++ b/spec/components/validators/username_setting_validator_spec.rb @@ -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