Sha256: 8713042439a526d434a8232a39c14b799f9b9303fdf5475d499169c5a7adf8c4

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

require 'oauth/models/consumers/token'
class ConsumerToken
  include Mongoid::Document
  include Mongoid::Timestamps
  include Oauth::Models::Consumers::Token
  
  # You can safely remove this callback if you don't allow login from any of your services
  before_create :create_user

  field :token, :type => String
  field :secret, :type => String

  index :token

  # Add the following to your user model:
  # 
  #   embeds_many :consumer_tokens  
  #   index "consumer_tokens.token"
  #
  embedded_in :user, :inverse_of => :consumer_tokens

  def self.find_or_create_from_access_token(user,access_token)
    if user
      user.consumer_tokens.first(:conditions=>{:_type=>self.to_s,:token=>access_token.token}) ||
        user.consumer_tokens.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
    else
      # Is there a better way of doing this in mongoid?
      # Please submit a patch
      user = User.first(:conditions=>{:_type=>self.to_s,"consumer_tokens.token"=>access_token.token})
      if user
        user.consumer_tokens.detect{|t| t.token==access_token.token && t.is_a?(self)} 
      else
        user = User.create
        user.consumer_tokens.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>access_token.secret)
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
oauth-plugin-0.4.0.pre4 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth-plugin-0.4.0.pre3 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb