FIX: Check group names when checking username availability

This commit is contained in:
Gerhard Schlager 2018-05-22 21:25:52 +02:00
parent d19d491d65
commit beed676b04
2 changed files with 7 additions and 1 deletions

View File

@ -208,7 +208,8 @@ class User < ActiveRecord::Base
def self.username_available?(username, email = nil)
lower = username.downcase
return false if reserved_username?(lower)
return true if !User.exists?(username_lower: lower)
return true if User.exec_sql(User::USERNAME_EXISTS_SQL, username: lower).count == 0
# staged users can use the same username since they will take over the account
email.present? && User.joins(:user_emails).exists?(staged: true, username_lower: lower, user_emails: { primary: true, email: email })
end

View File

@ -554,6 +554,11 @@ describe User do
user = Fabricate(:user, email: "bar@foo.com")
expect(User.username_available?(user.username, user.primary_email.email)).to eq(false)
end
it 'returns false when a username equals an existing group name' do
Fabricate(:group, name: 'foo')
expect(User.username_available?('Foo')).to eq(false)
end
end
describe '.reserved_username?' do