From e087e379074d3a9d5a079bd9beee431b66d83302 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 2 Jun 2014 17:32:39 +1000 Subject: [PATCH] BUGFIX: blank name causes SSO to explode --- app/models/discourse_single_sign_on.rb | 12 ++++++++---- spec/models/discourse_single_sign_on_spec.rb | 12 ++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 1a7472f8c..e277b057c 100644 --- a/app/models/discourse_single_sign_on.rb +++ b/app/models/discourse_single_sign_on.rb @@ -1,6 +1,7 @@ require_dependency 'single_sign_on' class DiscourseSingleSignOn < SingleSignOn + def self.sso_url SiteSetting.sso_url end @@ -57,7 +58,7 @@ class DiscourseSingleSignOn < SingleSignOn if sso_record && (user = sso_record.user) && !user.active user.active = true - user.save + user.save! user.enqueue_welcome_message('welcome_user') end @@ -77,13 +78,16 @@ class DiscourseSingleSignOn < SingleSignOn def match_email_or_create_user user = User.find_by(email: Email.downcase(email)) + try_name = name.blank? ? nil : name + try_username = username.blank? ? nil : username + user_params = { email: email, - name: User.suggest_name(name || username || email), - username: UserNameSuggester.suggest(username || name || email), + name: User.suggest_name(try_name || try_username || email), + username: UserNameSuggester.suggest(try_username || try_name || email), } - if user || user = User.create(user_params) + if user || user = User.create!(user_params) if sso_record = user.single_sign_on_record sso_record.last_payload = unsigned_payload sso_record.external_id = external_id diff --git a/spec/models/discourse_single_sign_on_spec.rb b/spec/models/discourse_single_sign_on_spec.rb index c65007bec..6f18624af 100644 --- a/spec/models/discourse_single_sign_on_spec.rb +++ b/spec/models/discourse_single_sign_on_spec.rb @@ -34,6 +34,18 @@ describe DiscourseSingleSignOn do parsed.custom_fields["b.b"].should == "B.b" end + it "can lookup or create user when name is blank" do + # so we can create system messages + Fabricate(:admin) + sso = DiscourseSingleSignOn.new + sso.username = "test" + sso.name = "" + sso.email = "test@test.com" + sso.external_id = "A" + user = sso.lookup_or_create_user + user.should_not == nil + end + it "can fill in data on way back" do sso = make_sso