Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Suggest current username for staged users #13706

Merged
merged 1 commit into from Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/auth/result.rb
Expand Up @@ -137,9 +137,18 @@ def to_client_hash
return result
end

suggested_username = UserNameSuggester.suggest(username_suggester_attributes)
if email_valid && email.present?
if username.present? && User.username_available?(username, email)
suggested_username = username
elsif staged_user = User.where(staged: true).find_by_email(email)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this branch... it's already covered by the logic in User#username_available?

email.present? && User.joins(:user_emails).exists?(staged: true, username_lower: lower, user_emails: { primary: true, email: email })

So I think the whole thing could be reduced to something like:

suggested_username = UserNameSuggester.suggest(username_suggester_attributes)
if email_valid && email.present? && username.present? && User.username_available?(username, email)
  suggested_username = username
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked it, I think that we need that fallback in case the username is not provided by the authenticator

suggested_username = staged_user.username
end
end

result = {
email: email,
username: UserNameSuggester.suggest(username_suggester_attributes),
username: suggested_username,
auth_provider: authenticator_name,
email_valid: !!email_valid,
can_edit_username: can_edit_username,
Expand Down
23 changes: 23 additions & 0 deletions spec/requests/omniauth_callbacks_controller_spec.rb
Expand Up @@ -213,6 +213,29 @@ def enabled?
expect(data["destination_url"]).to eq(destination_url)
end

it 'should return the right response for staged users' do
Fabricate(:user, username: "Staged_User", email: email, staged: true)

destination_url = '/somepath'
Rails.application.env_config["omniauth.origin"] = destination_url

events = DiscourseEvent.track_events { get "/auth/google_oauth2/callback.json" }
expect(events.any? { |e| e[:event_name] == :before_auth }).to eq(true)
expect(events.any? { |e| e[:event_name] === :after_auth && Auth::GoogleOAuth2Authenticator === e[:params][0] && !e[:params][1].failed? }).to eq(true)

expect(response.status).to eq(302)

data = JSON.parse(cookies[:authentication_data])

expect(data["email"]).to eq(email)
expect(data["username"]).to eq("Staged_User")
expect(data["auth_provider"]).to eq("google_oauth2")
expect(data["email_valid"]).to eq(true)
expect(data["can_edit_username"]).to eq(true)
expect(data["name"]).to eq("Some Name")
expect(data["destination_url"]).to eq(destination_url)
end

it 'should include destination url in response' do
destination_url = '/cookiepath'
cookies[:destination_url] = destination_url
Expand Down