Merge pull request #2034 from birarda/custom_username_length

allow for custom username length via site setting
This commit is contained in:
Sam 2014-03-05 12:06:23 +11:00
commit 69b498da24
4 changed files with 43 additions and 4 deletions

View File

@ -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

View File

@ -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."

View File

@ -102,6 +102,9 @@ users:
must_approve_users:
client: true
default: false
min_username_length:
client: true
default: 3
min_password_length:
client: true
default: 8

View File

@ -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