trabajo-afectivo/app/models/authorization.rb

58 lines
1.5 KiB
Ruby
Raw Normal View History

class Authorization < ApplicationModel
2012-07-29 19:32:37 +00:00
belongs_to :user
2012-04-16 08:04:49 +00:00
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider
2012-04-10 14:06:46 +00:00
def self.find_from_hash(hash)
auth = Authorization.where( :provider => hash['provider'], :uid => hash['uid'] ).first
if auth
2012-04-10 14:06:46 +00:00
# update auth tokens
auth.update_attributes(
2012-04-10 14:06:46 +00:00
:token => hash['credentials']['token'],
:secret => hash['credentials']['secret']
)
# update username of auth entry if empty
if !auth.username && hash['info']['nickname']
auth.update_attributes(
:username => hash['info']['nickname'],
)
end
2012-04-10 14:06:46 +00:00
# update image if needed
if hash['info']['image']
user = User.find( auth.user_id )
2012-04-10 14:06:46 +00:00
user.update_attributes(
2012-04-16 08:04:49 +00:00
:image => hash['info']['image']
2012-04-10 14:06:46 +00:00
)
end
end
return auth
2012-04-10 14:06:46 +00:00
end
def self.create_from_hash(hash, user = nil)
if user then
user.update_attributes(
# :username => hash['username'],
:image => hash['info']['image']
2012-04-10 14:06:46 +00:00
)
# fillup empty attributes
# TODO
2012-04-10 14:06:46 +00:00
else
user = User.create_from_hash!(hash)
end
auth = Authorization.create(
2012-04-10 14:06:46 +00:00
:user => user,
:uid => hash['uid'],
: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']
)
return auth
2012-04-16 08:04:49 +00:00
end
2012-04-10 14:06:46 +00:00
end