FIX: wasn't able to use the same username when taking over a staged account

This commit is contained in:
Régis Hanol 2017-12-12 11:26:00 +01:00
parent 7d8cd84fa6
commit 62a5b174e1
3 changed files with 14 additions and 4 deletions

View File

@ -166,9 +166,12 @@ class User < ActiveRecord::Base
SiteSetting.min_username_length.to_i..SiteSetting.max_username_length.to_i
end
def self.username_available?(username)
def self.username_available?(username, email=nil)
lower = username.downcase
!reserved_username?(lower) && !User.where(username_lower: lower).exists?
return false if reserved_username?(lower)
return true if !User.exists?(username_lower: lower)
# 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
def self.reserved_username?(username)

View File

@ -12,7 +12,7 @@ class UsernameCheckerService
end
def check_username_availability(username, email)
if User.username_available?(username)
if User.username_available?(username, email)
{ available: true, is_developer: is_developer?(email) }
else
{ available: false, suggestion: UserNameSuggester.suggest(username) }

View File

@ -484,9 +484,16 @@ describe User do
it 'returns false when a username is reserved' do
SiteSetting.reserved_usernames = 'test|donkey'
expect(User.username_available?('tESt')).to eq(false)
end
it "returns true when username is associated to a staged user of the same email" do
staged = Fabricate(:user, staged: true, email: "foo@bar.com")
expect(User.username_available?(staged.username, staged.primary_email.email)).to eq(true)
user = Fabricate(:user, email: "bar@foo.com")
expect(User.username_available?(user.username, user.primary_email.email)).to eq(false)
end
end
describe '.reserved_username?' do