diff --git a/app/models/user.rb b/app/models/user.rb index fdce215e4f0..4509e236147 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -93,9 +93,15 @@ class User < ActiveRecord::Base ALWAYS = -1 LAST_VISIT = -2 end + + GLOBAL_USERNAME_LENGTH_RANGE = 3..15 def self.username_length - 3..15 + if SiteSetting.enforce_global_nicknames + GLOBAL_USERNAME_LENGTH_RANGE + else + SiteSetting.min_username_length.to_i..GLOBAL_USERNAME_LENGTH_RANGE.end + end end def custom_groups diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 8e1159ed27c..eddaba38d18 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -672,7 +672,9 @@ en: invite_only: "Public registration is disabled, new users must be invited" login_required: "Require authentication to read posts" - + + min_username_length: "Minimum username length. (Does not apply if global nickname uniqueness is forced)" + min_password_length: "Minimum password length." block_common_passwords: "Don't allow passwords that are in the 5000 most common passwords." diff --git a/config/site_settings.yml b/config/site_settings.yml index e740fdf862d..74cdf3d04c2 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -93,6 +93,9 @@ users: must_approve_users: client: true default: false + min_username_length: + client: true + default: 3 min_password_length: client: true default: 8 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 656a8791d9a..e24ca1a1c2c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -174,7 +174,35 @@ describe User do myself.reload.username.should == 'HanSolo' end end - + + describe 'allow custom minimum username length from site settings' do + before do + @custom_min = User::GLOBAL_USERNAME_LENGTH_RANGE.begin - 1 + SiteSetting.stubs("min_username_length").returns(@custom_min) + end + + it 'should allow a shorter username than default' do + result = user.change_username('a' * @custom_min) + result.should_not be_false + end + + it 'should not allow a shorter username than limit' do + result = user.change_username('a' * (@custom_min - 1)) + result.should be_false + end + + it 'should not allow a longer username than limit' do + result = user.change_username('a' * (User::GLOBAL_USERNAME_LENGTH_RANGE.end + 1)) + result.should be_false + end + + it 'should use default length for validation if enforce_global_nicknames is true' do + SiteSetting.stubs('enforce_global_nicknames').returns(true) + + User::username_length.begin.should == User::GLOBAL_USERNAME_LENGTH_RANGE.begin + User::username_length.end.should == User::GLOBAL_USERNAME_LENGTH_RANGE.end + end + end end describe 'delete posts' do @@ -422,7 +450,7 @@ describe User do end describe 'username format' do - it "should always be 3 chars or longer" do + it "should be #{SiteSetting.min_username_length} chars or longer" do @user = Fabricate.build(:user) @user.username = 'ss' @user.save.should == false