2014-02-03 19:23:00 +00:00
|
|
|
# Copyright (C) 2012-2014 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
|
2012-07-29 19:32:37 +00:00
|
|
|
belongs_to :user
|
2013-02-12 22:37:04 +00:00
|
|
|
after_create :delete_user_cache
|
|
|
|
after_update :delete_user_cache
|
|
|
|
after_destroy :delete_user_cache
|
2012-04-16 08:04:49 +00:00
|
|
|
validates_presence_of :user_id, :uid, :provider
|
|
|
|
validates_uniqueness_of :uid, :scope => :provider
|
2013-02-12 22:37:04 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
def self.find_from_hash(hash)
|
2012-04-18 07:40:37 +00:00
|
|
|
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ).first
|
|
|
|
if auth
|
2012-04-10 14:06:46 +00:00
|
|
|
|
|
|
|
# update auth tokens
|
2012-04-18 07:40:37 +00:00
|
|
|
auth.update_attributes(
|
2014-12-01 07:32:35 +00:00
|
|
|
:token => hash['credentials']['token'],
|
|
|
|
: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
|
|
|
|
if !auth.username && hash['info']['nickname']
|
|
|
|
auth.update_attributes(
|
2014-12-01 07:32:35 +00:00
|
|
|
:username => hash['info']['nickname'],
|
2013-02-08 10:32:51 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
# update image if needed
|
|
|
|
if hash['info']['image']
|
2012-04-18 07:40:37 +00:00
|
|
|
user = User.find( auth.user_id )
|
2014-12-01 07:32:35 +00:00
|
|
|
|
|
|
|
# save/update avatar
|
|
|
|
avatar = Avatar.add(
|
|
|
|
:object => 'User',
|
|
|
|
:o_id => user.id,
|
|
|
|
:url => hash['info']['image'],
|
|
|
|
:source => hash['provider'],
|
|
|
|
:deletable => true,
|
|
|
|
:updated_by_id => user.id,
|
|
|
|
:created_by_id => user.id,
|
2012-04-10 14:06:46 +00:00
|
|
|
)
|
2014-12-01 07:32:35 +00:00
|
|
|
|
|
|
|
# update user link
|
|
|
|
if avatar
|
|
|
|
user.update_column( :image, avatar.store_hash )
|
|
|
|
end
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
|
|
|
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
|
|
|
if user
|
|
|
|
|
|
|
|
# save/update avatar
|
|
|
|
avatar = Avatar.add(
|
|
|
|
:object => 'User',
|
|
|
|
:o_id => user.id,
|
|
|
|
:url => hash['info']['image'],
|
|
|
|
:source => hash['provider'],
|
|
|
|
:deletable => true,
|
|
|
|
: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
|
|
|
|
if avatar
|
|
|
|
user.update_column( :image, avatar.store_hash )
|
|
|
|
end
|
|
|
|
|
2012-04-18 07:40:37 +00:00
|
|
|
# fillup empty attributes
|
|
|
|
# TODO
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-04-10 14:06:46 +00:00
|
|
|
else
|
2013-06-12 15:59:58 +00:00
|
|
|
user = User.create_from_hash!(hash)
|
2012-04-10 14:06:46 +00:00
|
|
|
end
|
2012-04-18 07:40:37 +00:00
|
|
|
|
2014-12-01 07:32:35 +00:00
|
|
|
Authorization.create(
|
2012-04-10 14:06:46 +00:00
|
|
|
:user => user,
|
|
|
|
:uid => hash['uid'],
|
2013-02-08 10:32:51 +00:00
|
|
|
:username => hash['info']['nickname'] || hash['username'],
|
2012-04-10 14:06:46 +00:00
|
|
|
:provider => hash['provider'],
|
|
|
|
:token => hash['credentials']['token'],
|
|
|
|
:secret => hash['credentials']['secret']
|
|
|
|
)
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2013-02-12 22:37:04 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-12-01 07:32:35 +00:00
|
|
|
def delete_user_cache
|
|
|
|
self.user.cache_delete
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|