Sha256: 58a6ccf78af1ad138b6641b6f9256974500ceeccdf1774120b06845d184aeb03
Contents?: true
Size: 1.75 KB
Versions: 2
Compression:
Stored size: 1.75 KB
Contents
class Authorization < ActiveRecord::Base belongs_to :user validates_presence_of :user_id, :uid, :provider validates_uniqueness_of :uid, :scope => :provider validates_associated :user before_destroy :allow_destroy? def self.find_or_create_from_hash(hash, existing_user = nil) if (auth = find_from_hash(hash)) auth.assign_account_info(hash) auth.save auth else create_from_hash(hash, existing_user) end end def self.create_from_hash(hash, existing_user = nil) create do |authorization| authorization.assign_account_info(hash) authorization.find_or_create_or_associate_user(existing_user) end end def self.find_from_hash(hash) find_by_provider_and_uid(hash['provider'], hash['uid']) end def find_or_create_or_associate_user(existing_user = nil) if existing_user self.user = existing_user elsif self.user self.user else self.user = User.find_or_create_from_authorization(self) end end def allow_destroy? if user.authorizations.count.eql?(1) errors.add(:base, "You must have at least one authorization provider.") raise ActiveRecord::Rollback end end def assign_account_info(auth_hash) self.uid = auth_hash['uid'] self.provider = auth_hash['provider'] self.nickname = auth_hash['info']['nickname'] self.email = auth_hash['info']['email'] self.picture_url = auth_hash['info']['image'] self.name = auth_hash['info']['name'] if auth_hash['credentials'] self.access_token = auth_hash['credentials']['token'] self.access_token_secret = auth_hash['credentials']['secret'] end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
community_engine-2.3.1 | app/models/authorization.rb |
community_engine-2.3.0 | app/models/authorization.rb |