Created
October 13, 2010 04:14
-
-
Save jakcharlton/623433 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserAccountsController < ApplicationController | |
def signin | |
auth = request.env['rack.auth'] | |
user_for_provider_and_uid = UserAccount.find_by_provider_and_uid(auth['provider'], auth['uid']).first() | |
new_auth = Authorization.new(:user_id => auth['user_id'], :provider => auth['provider'], :uid => auth['uid'], :user_info => auth['user_info'], :credentials => auth['credentials']) | |
if user_for_provider_and_uid.nil? | |
user = signed_in? ? self.current_user : UserAccount.new(:name => auth['user_info']['name']) | |
user.authorizations << new_auth | |
user.save! | |
self.current_user = user | |
else | |
self.current_user = user_for_provider_and_uid | |
end | |
redirect_to :root | |
end | |
end | |
class UserAccount | |
include Mongoid::Document | |
field :name, :type => String | |
field :admin, :type => Boolean, :default => false | |
references_many :lists, :stored_as => :array, :inverse_of => :users | |
embeds_many :authorizations, :class_name => "Authorization" | |
def self.find_by_provider_and_uid(provider, uid) | |
UserAccount.where("authorizations.provider" => provider).and("authorizations.uid" => uid) | |
end | |
end | |
class Authorization | |
include Mongoid::Document | |
field :provider, :type => String | |
field :uid, :type => String | |
field :user_id, :type => Integer | |
field :user_info, :type => Hash | |
field :user_credentials, :type => Hash | |
embedded_in :useraccount, :inverse_of => :authorizations # Remove this and it starts behaving badly! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment