2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-16 11:57:33 +00:00
|
|
|
class Authorization < ApplicationModel
|
2015-05-05 13:33:39 +00:00
|
|
|
belongs_to :user
|
|
|
|
after_create :delete_user_cache
|
|
|
|
after_update :delete_user_cache
|
|
|
|
after_destroy :delete_user_cache
|
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :uid, presence: true, uniqueness: { scope: :provider }
|
|
|
|
validates :provider, presence: true
|
2013-02-12 22:37:04 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.find_from_hash(hash)
|
2016-03-08 06:32:58 +00:00
|
|
|
auth = Authorization.find_by(provider: hash['provider'], uid: hash['uid'])
|
2012-04-18 07:40:37 +00:00
|
|
|
if auth
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
# update auth tokens
|
2017-09-11 11:16:08 +00:00
|
|
|
auth.update!(
|
2018-12-19 17:31:51 +00:00
|
|
|
token: hash['credentials']['token'],
|
2015-04-27 13:42:53 +00:00
|
|
|
secret: hash['credentials']['secret']
|
2012-04-10 14:06:46 +00:00
|
|
|
)
|
2012-04-18 07:40:37 +00:00
|
|
|
|
2013-02-08 10:32:51 +00:00
|
|
|
# update username of auth entry if empty
|
2018-10-11 12:40:12 +00:00
|
|
|
if !auth.username && hash['info']['nickname'].present?
|
2017-09-11 11:16:08 +00:00
|
|
|
auth.update!(
|
2015-04-27 13:42:53 +00:00
|
|
|
username: hash['info']['nickname'],
|
2013-02-08 10:32:51 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-10-11 12:40:12 +00:00
|
|
|
# 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
|
2014-12-01 07:32:35 +00:00
|
|
|
|
2018-10-11 12:40:12 +00:00
|
|
|
# update image if needed
|
|
|
|
if hash['info']['image'].present?
|
2014-12-01 07:32:35 +00:00
|
|
|
avatar = Avatar.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'User',
|
|
|
|
o_id: user.id,
|
|
|
|
url: hash['info']['image'],
|
|
|
|
source: hash['provider'],
|
|
|
|
deletable: true,
|
2015-04-27 13:42:53 +00:00
|
|
|
updated_by_id: user.id,
|
|
|
|
created_by_id: user.id,
|
2012-04-10 14:06:46 +00:00
|
|
|
)
|
2015-07-06 13:50:13 +00:00
|
|
|
if avatar && user.image != avatar.store_hash
|
|
|
|
user.image = avatar.store_hash
|
2014-12-01 07:32:35 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2018-10-11 12:40:12 +00:00
|
|
|
|
|
|
|
if user.changed?
|
|
|
|
user.save
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2014-12-01 07:32:35 +00:00
|
|
|
auth
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.create_from_hash(hash, user = nil)
|
2014-12-01 07:32:35 +00:00
|
|
|
|
2018-04-18 09:17:29 +00:00
|
|
|
if !user && Setting.get('auth_third_party_auto_link_at_inital_login')
|
|
|
|
if hash['info'] && hash['info']['email'].present?
|
|
|
|
user = User.find_by(email: hash['info']['email'].downcase)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-06 13:50:13 +00:00
|
|
|
if !user
|
|
|
|
user = User.create_from_hash!(hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
# save/update avatar
|
2017-10-20 13:32:01 +00:00
|
|
|
if hash['info'].present? && hash['info']['image'].present?
|
2014-12-01 07:32:35 +00:00
|
|
|
avatar = Avatar.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'User',
|
|
|
|
o_id: user.id,
|
|
|
|
url: hash['info']['image'],
|
|
|
|
source: hash['provider'],
|
|
|
|
deletable: true,
|
2015-04-27 13:42:53 +00:00
|
|
|
updated_by_id: user.id,
|
|
|
|
created_by_id: user.id,
|
2012-04-10 14:06:46 +00:00
|
|
|
)
|
2012-04-18 07:40:37 +00:00
|
|
|
|
2014-12-01 07:32:35 +00:00
|
|
|
# update user link
|
2015-07-06 13:50:13 +00:00
|
|
|
if avatar && user.image != avatar.store_hash
|
|
|
|
user.image = avatar.store_hash
|
|
|
|
user.save
|
2014-12-01 07:32:35 +00:00
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-18 07:40:37 +00:00
|
|
|
|
2018-05-08 10:10:19 +00:00
|
|
|
Authorization.create!(
|
2018-12-19 17:31:51 +00:00
|
|
|
user: user,
|
|
|
|
uid: hash['uid'],
|
2015-07-06 11:48:07 +00:00
|
|
|
username: hash['info']['nickname'] || hash['info']['username'] || hash['info']['name'] || hash['info']['email'] || hash['username'],
|
2015-04-27 13:42:53 +00:00
|
|
|
provider: hash['provider'],
|
2018-12-19 17:31:51 +00:00
|
|
|
token: hash['credentials']['token'],
|
|
|
|
secret: hash['credentials']['secret']
|
2012-04-10 14:06:46 +00:00
|
|
|
)
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2013-02-12 22:37:04 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-04-27 14:56:32 +00:00
|
|
|
def delete_user_cache
|
2015-07-06 13:50:13 +00:00
|
|
|
return if !user
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
user.touch # rubocop:disable Rails/SkipsModelValidations
|
2015-04-27 14:56:32 +00:00
|
|
|
end
|
2014-12-01 07:32:35 +00:00
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|