Sha256: 5bd2bb02b8967fad743a147767a79dbc1058467ab05b127c2e418d0141569db6

Contents?: true

Size: 1.28 KB

Versions: 10

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)
    secret = access_token.respond_to?(:secret) ? access_token.secret : nil

    if user
      user.consumer_tokens.where(:_type=>self.to_s,:token=>access_token.token).first ||
        self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
    else
      user = User.where("consumer_tokens._type"=>self.to_s,"consumer_tokens.token"=>access_token.token).first
      if user
        user.consumer_tokens.detect{|t| t.token==access_token.token && t.is_a?(self)}
      else
        user = User.new
        self.create!(:_type=>self.to_s,:token=>access_token.token, :secret=>secret, :user=>user)
        user.save!
        user.consumer_tokens.last
      end
    end
  end

end

Version data entries

10 entries across 10 versions & 4 rubygems

Version Path
houston-oauth-plugin-0.5.1 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth_consumer-0.1.3 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
panjiva-oauth-plugin-0.4.1 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth_consumer-0.1.2 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth_consumer-0.1.1 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth_consumer-0.1.0 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth-plugin-0.5.1 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth-plugin-0.5.0 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth-plugin-0.4.1 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb
oauth-plugin-0.4.0 lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb