mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06: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
|
SiteSetting.min_username_length.to_i..SiteSetting.max_username_length.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.username_available?(username)
|
def self.username_available?(username, email=nil)
|
||||||
lower = username.downcase
|
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
|
end
|
||||||
|
|
||||||
def self.reserved_username?(username)
|
def self.reserved_username?(username)
|
||||||
|
@ -12,7 +12,7 @@ class UsernameCheckerService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_username_availability(username, email)
|
def check_username_availability(username, email)
|
||||||
if User.username_available?(username)
|
if User.username_available?(username, email)
|
||||||
{ available: true, is_developer: is_developer?(email) }
|
{ available: true, is_developer: is_developer?(email) }
|
||||||
else
|
else
|
||||||
{ available: false, suggestion: UserNameSuggester.suggest(username) }
|
{ available: false, suggestion: UserNameSuggester.suggest(username) }
|
||||||
|
@ -484,9 +484,16 @@ describe User do
|
|||||||
|
|
||||||
it 'returns false when a username is reserved' do
|
it 'returns false when a username is reserved' do
|
||||||
SiteSetting.reserved_usernames = 'test|donkey'
|
SiteSetting.reserved_usernames = 'test|donkey'
|
||||||
|
|
||||||
expect(User.username_available?('tESt')).to eq(false)
|
expect(User.username_available?('tESt')).to eq(false)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe '.reserved_username?' do
|
describe '.reserved_username?' do
|
||||||
|
Loading…
Reference in New Issue
Block a user