From 6439203900024c524969b5654c52c0bdc0038d0b Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 11 Oct 2018 14:40:12 +0200 Subject: [PATCH] Fixed issue #2281 - O365/Office365 authentication missing given- and surname. --- app/controllers/sessions_controller.rb | 2 +- app/models/authorization.rb | 25 +++++++++++++++++-------- app/models/user.rb | 14 +++++++++----- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 7f394a64c..e207b717b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -69,7 +69,7 @@ class SessionsController < ApplicationController user_id = session[:user_id] end - if !user_id + if !user_id || !User.exists?(user_id) # get models models = SessionHelper.models() diff --git a/app/models/authorization.rb b/app/models/authorization.rb index 2d70ca492..6591013cb 100644 --- a/app/models/authorization.rb +++ b/app/models/authorization.rb @@ -20,17 +20,25 @@ class Authorization < ApplicationModel ) # update username of auth entry if empty - if !auth.username && hash['info']['nickname'] + if !auth.username && hash['info']['nickname'].present? auth.update!( username: hash['info']['nickname'], ) end - # update image if needed - if hash['info']['image'] - user = User.find(auth.user_id) + # update firstname/lastname if needed + user = User.find(auth.user_id) + if user.firstname.blank? && user.lastname.blank? + if hash['info']['first_name'].present? && hash['info']['last_name'].present? + user.firstname = hash['info']['first_name'] + user.lastname = hash['info']['last_name'] + elsif hash['info']['display_name'].present? + user.firstname = hash['info']['display_name'] + end + end - # save/update avatar + # update image if needed + if hash['info']['image'].present? avatar = Avatar.add( object: 'User', o_id: user.id, @@ -40,13 +48,14 @@ class Authorization < ApplicationModel updated_by_id: user.id, created_by_id: user.id, ) - - # update user link if avatar && user.image != avatar.store_hash user.image = avatar.store_hash - user.save end end + + if user.changed? + user.save + end end auth end diff --git a/app/models/user.rb b/app/models/user.rb index c7febd6c8..75b6b1be5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -359,7 +359,6 @@ returns def self.create_from_hash!(hash) - role_ids = Role.signup_role_ids url = '' hash['info']['urls']&.each_value do |local_url| next if local_url.blank? @@ -367,19 +366,24 @@ returns url = local_url end begin - create!( + data = { login: hash['info']['nickname'] || hash['uid'], - firstname: hash['info']['name'], + firstname: hash['info']['name'] || hash['info']['display_name'], email: hash['info']['email'], image_source: hash['info']['image'], web: url, address: hash['info']['location'], note: hash['info']['description'], source: hash['provider'], - role_ids: role_ids, + role_ids: Role.signup_role_ids, updated_by_id: 1, created_by_id: 1, - ) + } + if hash['info']['first_name'].present? && hash['info']['last_name'].present? + data[:firstname] = hash['info']['first_name'] + data[:lastname] = hash['info']['last_name'] + end + create!(data) rescue => e logger.error e raise Exceptions::UnprocessableEntity, e.message