mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 01:47:22 +08:00
FIX: wasn't able to use the same username when taking over a staged account
This commit is contained in:
parent
7d8cd84fa6
commit
62a5b174e1
|
@ -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)
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user