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

FIX: Suggest current username for staged users #13706

merged 1 commit into from Jul 12, 2021

Conversation

udan11
Copy link
Contributor

@udan11 udan11 commented Jul 12, 2021

If user had a staged account and logged in using a third party service
a different username was suggested. This change will try to use the
username given by the authentication provider first, then the current
staged username and last suggest a new one.

If user had a staged account and logged in using a third party service
a different username was suggested. This change will try to use the
username given by the authentication provider first, then the current
staged username and last suggest a new one.
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

@lis2 lis2 merged commit 49090c3 into master Jul 12, 2021
@lis2 lis2 deleted the staged-username branch July 12, 2021 23:15
AndrewPrigorshnev added a commit that referenced this pull request Dec 16, 2021
…15320)

Some time ago, we made this fix to external authentication –  #13706. We didn't address Discourse Connect (https://meta.discourse.org/t/discourseconnect-official-single-sign-on-for-discourse-sso/13045) at that moment, so I wanted to fix it for Discourse Connect as well.

Turned out though that Discourse Connect doesn't contain this problem and already handles staged users correctly. This PR adds tests that confirm it. Also, I've extracted two functions in Discourse Connect implementation along the way and decided to merge this refactoring too (the refactoring is supported with tests).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants