Sha256: fd4a30c8738dec444fb7b0d46d3688f9fde326a3e3fdef66780dc51814051daf

Contents?: true

Size: 949 Bytes

Versions: 1

Compression:

Stored size: 949 Bytes

Contents

module Landable
  class AccessToken < ActiveRecord::Base
    include Landable::TableName

    # Maximum token age, in hours
    MAX_AGE = ((Landable.configuration['ldap'] &&
                Landable.configuration['ldap'][:access_token_max_age]) || 8).hours

    belongs_to :author
    validates_presence_of :author_id
    validates_presence_of :expires_at
    validates_presence_of :permissions

    before_validation do |token|
      token.expires_at ||= expiration
    end

    scope :fresh,   -> { where('expires_at > ?',  Time.zone.now) }
    scope :expired, -> { where('expires_at <= ?', Time.zone.now) }

    def refresh!
      update_column :expires_at, expiration
    end

    def can_publish?
      permissions['publish'] == 'true'
    end

    def can_edit?
      permissions['edit'] == 'true'
    end

    def can_read?
      permissions['read'] == 'true'
    end

    private

    def expiration
      MAX_AGE.from_now
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
landable-1.14.0 app/models/landable/access_token.rb