BUGFIX: blank name causes SSO to explode

This commit is contained in:
Sam 2014-06-02 17:32:39 +10:00
parent 9e9c41ac52
commit e087e37907
2 changed files with 20 additions and 4 deletions

View File

@ -1,6 +1,7 @@
require_dependency 'single_sign_on' require_dependency 'single_sign_on'
class DiscourseSingleSignOn < SingleSignOn class DiscourseSingleSignOn < SingleSignOn
def self.sso_url def self.sso_url
SiteSetting.sso_url SiteSetting.sso_url
end end
@ -57,7 +58,7 @@ class DiscourseSingleSignOn < SingleSignOn
if sso_record && (user = sso_record.user) && !user.active if sso_record && (user = sso_record.user) && !user.active
user.active = true user.active = true
user.save user.save!
user.enqueue_welcome_message('welcome_user') user.enqueue_welcome_message('welcome_user')
end end
@ -77,13 +78,16 @@ class DiscourseSingleSignOn < SingleSignOn
def match_email_or_create_user def match_email_or_create_user
user = User.find_by(email: Email.downcase(email)) user = User.find_by(email: Email.downcase(email))
try_name = name.blank? ? nil : name
try_username = username.blank? ? nil : username
user_params = { user_params = {
email: email, email: email,
name: User.suggest_name(name || username || email), name: User.suggest_name(try_name || try_username || email),
username: UserNameSuggester.suggest(username || name || 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 if sso_record = user.single_sign_on_record
sso_record.last_payload = unsigned_payload sso_record.last_payload = unsigned_payload
sso_record.external_id = external_id sso_record.external_id = external_id

View File

@ -34,6 +34,18 @@ describe DiscourseSingleSignOn do
parsed.custom_fields["b.b"].should == "B.b" parsed.custom_fields["b.b"].should == "B.b"
end 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 it "can fill in data on way back" do
sso = make_sso sso = make_sso