FEATURE: add a hidden setting that enables using email as a source for username suggestions (#14623)

This is a follow-up to https://github.com/discourse/discourse/pull/14541. This adds a hidden setting for restoring the old behavior for those users who rely on it. We'll likely deprecate this setting at some point in the future.
This commit is contained in:
Andrei Prigorshnev 2021-10-19 14:37:39 +04:00 committed by GitHub
parent a5eba43a65
commit b9164d22ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 4 deletions

View File

@ -240,10 +240,17 @@ class DiscourseSingleSignOn < SingleSignOn
try_name = name.presence try_name = name.presence
try_username = username.presence try_username = username.presence
name_suggester_input = try_username
username_suggester_input = try_username || try_name
if SiteSetting.use_email_for_username_and_name_suggestions
name_suggester_input = name_suggester_input || email
username_suggester_input = username_suggester_input || email
end
user_params = { user_params = {
primary_email: UserEmail.new(email: email, primary: true), primary_email: UserEmail.new(email: email, primary: true),
name: try_name || User.suggest_name(try_username), name: try_name || User.suggest_name(name_suggester_input),
username: UserNameSuggester.suggest(try_username || try_name), username: UserNameSuggester.suggest(username_suggester_input),
ip_address: ip_address ip_address: ip_address
} }

View File

@ -685,6 +685,9 @@ users:
max_bookmarks_per_user: max_bookmarks_per_user:
default: 2000 default: 2000
hidden: true hidden: true
use_email_for_username_and_name_suggestions:
hidden: true
default: false
groups: groups:
enable_group_directory: enable_group_directory:

View File

@ -347,7 +347,7 @@ describe DiscourseSingleSignOn do
expect(admin.name).to eq "Louis C.K." expect(admin.name).to eq "Louis C.K."
end end
it "doesn't use email as a source for username suggestions" do it "doesn't use email as a source for username suggestions by default" do
sso = new_discourse_sso sso = new_discourse_sso
sso.external_id = "100" sso.external_id = "100"
@ -360,7 +360,21 @@ describe DiscourseSingleSignOn do
expect(user.username).to eq I18n.t('fallback_username') expect(user.username).to eq I18n.t('fallback_username')
end end
it "doesn't use email as a source for name suggestions" do it "use email as a source for username suggestions if enabled" do
SiteSetting.use_email_for_username_and_name_suggestions = true
sso = new_discourse_sso
sso.external_id = "100"
# set username and name to nil, so they cannot be used as a source for suggestions
sso.username = nil
sso.name = nil
sso.email = "mail@mail.com"
user = sso.lookup_or_create_user(ip_address)
expect(user.username).to eq "mail"
end
it "doesn't use email as a source for name suggestions by default" do
sso = new_discourse_sso sso = new_discourse_sso
sso.external_id = "100" sso.external_id = "100"
@ -373,6 +387,20 @@ describe DiscourseSingleSignOn do
expect(user.name).to eq "" expect(user.name).to eq ""
end end
it "use email as a source for name suggestions if enabled" do
SiteSetting.use_email_for_username_and_name_suggestions = true
sso = new_discourse_sso
sso.external_id = "100"
# set username and name to nil, so they cannot be used as a source for suggestions
sso.username = nil
sso.name = nil
sso.email = "mail@mail.com"
user = sso.lookup_or_create_user(ip_address)
expect(user.name).to eq "Mail"
end
it "can override username with a number at the end to a simpler username without a number" do it "can override username with a number at the end to a simpler username without a number" do
SiteSetting.auth_overrides_username = true SiteSetting.auth_overrides_username = true