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

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

This is a follow-up to FEATURE: stop using email as source for username and name suggestions for Single Sign On by AndrewPrigorshnev · Pull Request #14541 · discourse/discourse · GitHub. 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.

diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb
index 7b198d3..e268d98 100644
--- a/app/models/discourse_single_sign_on.rb
+++ b/app/models/discourse_single_sign_on.rb
@@ -240,10 +240,17 @@ class DiscourseSingleSignOn < SingleSignOn
         try_name = name.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 = {
           primary_email: UserEmail.new(email: email, primary: true),
-          name: try_name || User.suggest_name(try_username),
-          username: UserNameSuggester.suggest(try_username || try_name),
+          name: try_name || User.suggest_name(name_suggester_input),
+          username: UserNameSuggester.suggest(username_suggester_input),
           ip_address: ip_address
         }
 
diff --git a/config/site_settings.yml b/config/site_settings.yml
index b7c97ef..97c3151 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -685,6 +685,9 @@ users:
   max_bookmarks_per_user:
     default: 2000
     hidden: true
+  use_email_for_username_and_name_suggestions:
+    hidden: true
+    default: false
 
 groups:
   enable_group_directory:
diff --git a/spec/models/discourse_single_sign_on_spec.rb b/spec/models/discourse_single_sign_on_spec.rb
index 9c73977..b9fdba6 100644
--- a/spec/models/discourse_single_sign_on_spec.rb
+++ b/spec/models/discourse_single_sign_on_spec.rb
@@ -347,7 +347,7 @@ describe DiscourseSingleSignOn do
     expect(admin.name).to eq "Louis C.K."
   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.external_id = "100"
 
@@ -360,7 +360,21 @@ describe DiscourseSingleSignOn do
     expect(user.username).to eq I18n.t('fallback_username')
   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.external_id = "100"
 
@@ -373,6 +387,20 @@ describe DiscourseSingleSignOn do
     expect(user.name).to eq ""
   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
     SiteSetting.auth_overrides_username = true
 

GitHub sha: b9164d22eeaebd5c37214daeeb1469601ab37216

This commit appears in #14623 which was approved by gschlager and davidtaylorhq. It was merged by AndrewPrigorshnev.