mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 17:13:25 +08:00
Validate username site settings
This commit is contained in:
@ -915,6 +915,7 @@ en:
|
|||||||
|
|
||||||
errors:
|
errors:
|
||||||
invalid_email: "Invalid email address."
|
invalid_email: "Invalid email address."
|
||||||
|
invalid_username: "There's no user with that username."
|
||||||
|
|
||||||
notification_types:
|
notification_types:
|
||||||
mentioned: "%{display_username} mentioned you in %{link}"
|
mentioned: "%{display_username} mentioned you in %{link}"
|
||||||
|
@ -9,7 +9,9 @@ required:
|
|||||||
notification_email:
|
notification_email:
|
||||||
default: 'info@discourse.org'
|
default: 'info@discourse.org'
|
||||||
validator: 'EmailSettingValidator'
|
validator: 'EmailSettingValidator'
|
||||||
site_contact_username: ''
|
site_contact_username:
|
||||||
|
default: ''
|
||||||
|
validator: 'UsernameSettingValidator'
|
||||||
logo_url:
|
logo_url:
|
||||||
client: true
|
client: true
|
||||||
default: '/images/d-logo-sketch.png'
|
default: '/images/d-logo-sketch.png'
|
||||||
@ -316,7 +318,9 @@ email:
|
|||||||
pop3s_polling_enabled: false
|
pop3s_polling_enabled: false
|
||||||
pop3s_polling_host: ''
|
pop3s_polling_host: ''
|
||||||
pop3s_polling_port: 995
|
pop3s_polling_port: 995
|
||||||
pop3s_polling_username: ''
|
pop3s_polling_username:
|
||||||
|
default: ''
|
||||||
|
validator: 'UsernameSettingValidator'
|
||||||
pop3s_polling_password: ''
|
pop3s_polling_password: ''
|
||||||
email_in:
|
email_in:
|
||||||
default: false
|
default: false
|
||||||
@ -484,7 +488,9 @@ embedding:
|
|||||||
embeddable_host: ''
|
embeddable_host: ''
|
||||||
feed_polling_enabled: false
|
feed_polling_enabled: false
|
||||||
feed_polling_url: ''
|
feed_polling_url: ''
|
||||||
embed_by_username: ''
|
embed_by_username:
|
||||||
|
default: ''
|
||||||
|
validator: 'UsernameSettingValidator'
|
||||||
embed_category: ''
|
embed_category: ''
|
||||||
embed_post_limit: 100
|
embed_post_limit: 100
|
||||||
embed_truncate: false
|
embed_truncate: false
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
class EmailSettingValidator
|
class EmailSettingValidator
|
||||||
def self.valid_value?(val)
|
def self.valid_value?(val)
|
||||||
val == '' || EmailValidator.email_regex =~ val
|
!val.present? || !!(EmailValidator.email_regex =~ val)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.error_message(val)
|
def self.error_message(val)
|
||||||
|
9
lib/validators/username_setting_validator.rb
Normal file
9
lib/validators/username_setting_validator.rb
Normal 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
|
18
spec/components/validators/email_setting_validator_spec.rb
Normal file
18
spec/components/validators/email_setting_validator_spec.rb
Normal 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
|
@ -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
|
Reference in New Issue
Block a user