diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index a34b31b7044..eaf37bcd9fd 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -117,7 +117,14 @@ class SessionController < ApplicationController SingleSignOn::ACCESSORS.each do |a| details[a] = sso.send(a) end - Rails.logger.error "Failed to create or lookup user: #{e}\n\n#{details.map{|k,v| "#{k}: #{v}"}.join("\n")}" + + message = "Failed to create or lookup user: #{e}." + message << "\n\n" << "-" * 100 + message << details.map { |k,v| "#{k}: #{v}" }.join("\n") + message << "\n\n" << "-" * 100 + message << e.backtrace.join("\n") + + Rails.logger.error(message) render text: I18n.t("sso.unknown_error"), status: 500 end diff --git a/app/models/discourse_single_sign_on.rb b/app/models/discourse_single_sign_on.rb index 894d545adae..ba03249bd0f 100644 --- a/app/models/discourse_single_sign_on.rb +++ b/app/models/discourse_single_sign_on.rb @@ -81,19 +81,21 @@ class DiscourseSingleSignOn < SingleSignOn private def match_email_or_create_user(ip_address) - user = User.find_by_email(email) + unless user = User.find_by_email(email) + try_name = name.presence + try_username = username.presence - try_name = name.blank? ? nil : name - try_username = username.blank? ? nil : username + user_params = { + email: email, + name: try_name || User.suggest_name(try_username || email), + username: UserNameSuggester.suggest(try_username || try_name || email), + ip_address: ip_address + } - user_params = { - email: email, - name: try_name || User.suggest_name(try_username || email), - username: UserNameSuggester.suggest(try_username || try_name || email), - ip_address: ip_address - } + user = User.create!(user_params) + end - if user || user = User.create!(user_params) + if user if sso_record = user.single_sign_on_record sso_record.last_payload = unsigned_payload sso_record.external_id = external_id