personal_access_token.rb 612 字节
Newer Older
1
class PersonalAccessToken < ActiveRecord::Base
2
  include Expirable
3 4 5
  include TokenAuthenticatable
  add_authentication_token_field :token

6 7
  serialize :scopes, Array

8 9
  belongs_to :user

10 11
  scope :active, -> { where(revoked: false).where("expires_at >= NOW() OR expires_at IS NULL") }
  scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
12

13 14
  def self.generate(params)
    personal_access_token = self.new(params)
15
    personal_access_token.ensure_token
16 17
    personal_access_token
  end
18 19 20 21 22

  def revoke!
    self.revoked = true
    self.save
  end
23 24 25 26

  def active?
    !revoked? && !expired?
  end
27
end