diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 377cb210287..a9784f52a91 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 8c5017bf97b..173f4efe8f0 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