diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 1a7472f8c88..e277b057ce6 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 c65007bec0e..6f18624af6f 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