From 4566a1e30aa9333831ed3468548ec4900ef64d30 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 27 Mar 2015 09:39:35 +1100 Subject: [PATCH] FIX: sso override code not triggered when attaching to existing user --- app/models/discourse_single_sign_on.rb | 11 ++------ spec/models/discourse_single_sign_on_spec.rb | 29 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 377cb2102..a9784f52a 100644 --- a/app/models/discourse_single_sign_on.rb +++ b/app/models/discourse_single_sign_on.rb @@ -110,20 +110,15 @@ class DiscourseSingleSignOn < SingleSignOn end def change_external_attributes_and_override(sso_record, user) - if SiteSetting.sso_overrides_email && email != sso_record.external_email - # set the user's email to whatever came in the payload + if SiteSetting.sso_overrides_email && user.email != email user.email = email end - if SiteSetting.sso_overrides_username && username != sso_record.external_username && user.username != username - # we have an external username change, and the user's current username doesn't match - # run it through the UserNameSuggester to override it + if SiteSetting.sso_overrides_username && user.username != username user.username = UserNameSuggester.suggest(username || name || email) end - if SiteSetting.sso_overrides_name && name != sso_record.external_name && user.name != name - # we have an external name change, and the user's current name doesn't match - # run it through the name suggester to override it + if SiteSetting.sso_overrides_name && user.name != name user.name = User.suggest_name(name || username || email) end diff --git a/spec/models/discourse_single_sign_on_spec.rb b/spec/models/discourse_single_sign_on_spec.rb index 8c5017bf9..173f4efe8 100644 --- a/spec/models/discourse_single_sign_on_spec.rb +++ b/spec/models/discourse_single_sign_on_spec.rb @@ -62,6 +62,35 @@ describe DiscourseSingleSignOn do expect(user).to_not be_nil end + it "can override name / email / username" do + admin = Fabricate(:admin) + + SiteSetting.sso_overrides_name = true + SiteSetting.sso_overrides_email = true + SiteSetting.sso_overrides_username = true + + sso = DiscourseSingleSignOn.new + sso.username = "bob%the$admin" + sso.name = "Bob Admin" + sso.email = admin.email + sso.external_id = "A" + + sso.lookup_or_create_user(ip_address) + + admin.reload + + expect(admin.name).to eq "Bob Admin" + expect(admin.username).to eq "bob_the_admin" + expect(admin.email).to eq admin.email + + sso.email = "TEST@bob.com" + + sso.lookup_or_create_user(ip_address) + + admin.reload + expect(admin.email).to eq("test@bob.com") + end + it "can fill in data on way back" do sso = make_sso