Merge pull request #4728 from nbianca/username-regex

Add support for username regex.
This commit is contained in:
Guo Xiang Tan 2017-03-03 22:59:23 +08:00 committed by GitHub
commit bcf634ca85
4 changed files with 23 additions and 3 deletions

View File

@ -162,7 +162,7 @@ class User < ActiveRecord::Base
lower = username.downcase lower = username.downcase
User.where(username_lower: lower).blank? && User.where(username_lower: lower).blank? &&
!SiteSetting.reserved_usernames.split("|").any? { |reserved| reserved.casecmp(username) == 0 } SiteSetting.reserved_usernames.split("|").all? { |reserved| !lower.match('^' + Regexp.escape(reserved).gsub('\*', '.*') + '$') }
end end
def self.plugin_staff_user_custom_fields def self.plugin_staff_user_custom_fields

View File

@ -1039,7 +1039,7 @@ en:
min_username_length: "Minimum username length in characters. WARNING: if any existing users or groups have names shorter than this, your site will break!" min_username_length: "Minimum username length in characters. WARNING: if any existing users or groups have names shorter than this, your site will break!"
max_username_length: "Maximum username length in characters. WARNING: if any existing users or groups have names longer than this, your site will break!" max_username_length: "Maximum username length in characters. WARNING: if any existing users or groups have names longer than this, your site will break!"
reserved_usernames: "Usernames for which signup is not allowed." reserved_usernames: "Usernames for which signup is not allowed. Wildcard symbol * can be used to match any character zero or more times."
min_password_length: "Minimum password length." min_password_length: "Minimum password length."
min_admin_password_length: "Minimum password length for Admin." min_admin_password_length: "Minimum password length for Admin."

View File

@ -21,12 +21,16 @@ module UserNameSuggester
name = fix_username(name) name = fix_username(name)
i = 1 i = 1
attempt = name attempt = name
until attempt == allow_username || User.username_available?(attempt) until attempt == allow_username || User.username_available?(attempt) || i > 100
suffix = i.to_s suffix = i.to_s
max_length = User.username_length.end - suffix.length - 1 max_length = User.username_length.end - suffix.length - 1
attempt = "#{name[0..max_length]}#{suffix}" attempt = "#{name[0..max_length]}#{suffix}"
i += 1 i += 1
end end
until attempt == allow_username || User.username_available?(attempt) || i > 200
attempt = SecureRandom.hex[0..SiteSetting.max_username_length]
i += 1
end
attempt attempt
end end

View File

@ -475,6 +475,22 @@ describe User do
expect(User.username_available?('DonKey')).to eq(false) expect(User.username_available?('DonKey')).to eq(false)
expect(User.username_available?('test')).to eq(false) expect(User.username_available?('test')).to eq(false)
end end
it 'should not allow usernames matched against an expession' do
SiteSetting.reserved_usernames = 'test)|*admin*|foo*|*bar|abc.def'
expect(User.username_available?('test')).to eq(true)
expect(User.username_available?('abc9def')).to eq(true)
expect(User.username_available?('admin')).to eq(false)
expect(User.username_available?('foo')).to eq(false)
expect(User.username_available?('bar')).to eq(false)
expect(User.username_available?('admi')).to eq(true)
expect(User.username_available?('bar.foo')).to eq(true)
expect(User.username_available?('foo.bar')).to eq(false)
expect(User.username_available?('baz.bar')).to eq(false)
end
end end
describe 'email_validator' do describe 'email_validator' do