Sha256: 89cb2540604b7b7bc0e4e3763857952e675b8f07ed8b617c05ee43950561bc85

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Cadenero
  # Defines that a Cadenero::User is member of an Cadenero::V1::Account
  class Member < ActiveRecord::Base
    attr_accessible :account_id, :user_id
    belongs_to :account,  :class_name => "Cadenero::V1::Account"
    belongs_to :user,  :class_name => "Cadenero::User"
    after_create :ensure_auth_token!

    # Generate authentication token unless already exists.
    def ensure_auth_token
      reset_auth_token if auth_token.blank?
    end

    # Generate authentication token unless already exists and save the record.
    def ensure_auth_token!
      reset_auth_token! if auth_token.blank?
    end

    # Generate new authentication token (a.k.a. "single access token").
    def reset_auth_token
      self.auth_token = self.class.auth_token
    end

    # Generate new authentication token and save the record.
    def reset_auth_token!
      reset_auth_token
      save(:validate => false)
    end

    class << self
      # Generate a token checking if one does not already exist in the database.
      def auth_token
        generate_token(:auth_token)
      end

      protected
      # Generate a token by looping and ensuring does not already exist.
      # @param [String] column is the name of the column that has the authentication token
      # @return {String]} a unique generated auth_token
      def generate_token(column)
        loop do
          token = SecureRandom.base64(15).tr('+/=lIO0', 'pqrsxyz')
          break token unless Member.where({ column => token }).first
        end
      end
    end

  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cadenero-0.0.2.b6 app/models/cadenero/member.rb
cadenero-0.0.2.b5 app/models/cadenero/member.rb