Sha256: 2b1101f4ce8cd251acad3434fb967eaa2d2be6b9ab0163ee4f38fc1fd8b91bab

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# encoding: UTF-8

module Spontaneous::Permissions
  class AccessKey < Sequel::Model(:spontaneous_access_keys)
    plugin :timestamps
    many_to_one :user, :class => :'Spontaneous::Permissions::User', :reciprocal => :access_keys

    def self.authenticate(key_id, ip_address = nil)
      if key = self.for_id(key_id)
        key.access!(ip_address)
        return key
      end
      nil
    end

    def self.valid?(key_id, user)
      (key = self.for_id(key_id)) && (key.user == user) && (key.user.enabled?)
    end

    def self.for_id(key_id)
      key_dataset.call(:key_id => key_id).first
    end

    def self.key_dataset
      @key_dataset ||= self.where(:key_id => :$key_id).
        eager_graph(:user).
        prepare(:select, :select_access_key_by_key)
    end

    def before_create
      self.key_id = Spontaneous::Permissions.random_string(44)
      self.last_access_at = Time.now
      super
    end

    def access!(ip_address = nil)
      self.update(:last_access_at => Time.now, :last_access_ip => ip_address)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spontaneous-0.2.0.beta1 lib/spontaneous/permissions/access_key.rb