Merge pull request #2896 from evazion/fix-username-whitespace

Disallow unicode whitespace in usernames (#2894).
This commit is contained in:
Albert Yi
2017-02-27 16:47:44 -08:00
committed by GitHub
4 changed files with 26 additions and 19 deletions

View File

@@ -0,0 +1,10 @@
class UserNameValidator < ActiveModel::EachValidator
def validate_each(rec, attr, value)
name = User.normalize_name(value)
rec.errors[attr] << "already exists" if User.find_by_name(name).present?
rec.errors[attr] << "must be 2 to 100 characters long" if !name.length.between?(2, 100)
rec.errors[attr] << "cannot have whitespace or colons" if name =~ /[[:space:]]|:/
rec.errors[attr] << "cannot begin or end with an underscore" if name =~ /\A_|_\z/
end
end