2014-06-18 16:58:30 +03:00
|
|
|
class Openid < ActiveRecord::Base
|
|
|
|
|
belongs_to :user
|
2014-06-18 17:09:14 +03:00
|
|
|
validates :provider, :uid, presence: true
|
2014-06-18 16:58:30 +03:00
|
|
|
|
|
|
|
|
# Searches for openid based on provider and uid.
|
|
|
|
|
# Returns found openid or a new openid.
|
|
|
|
|
# ====Returns
|
|
|
|
|
# * Openid::ActiveRecord_Relation -> openid
|
|
|
|
|
def self.find_for_oauth(auth)
|
|
|
|
|
openid = Openid.where(provider: auth.provider, uid: auth.uid).first_or_initialize
|
|
|
|
|
|
|
|
|
|
if openid.new_record?
|
|
|
|
|
openid.email = auth.info.email
|
2014-07-21 14:27:32 +02:00
|
|
|
if (existing_openid = Openid.where(email: openid.email).first)
|
2014-06-18 16:58:30 +03:00
|
|
|
openid.user_id = existing_openid.user_id
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
openid
|
|
|
|
|
end
|
|
|
|
|
end
|