gitlab-org--gitlab-foss/app/models/personal_access_token.rb

34 lines
800 B
Ruby
Raw Normal View History

class PersonalAccessToken < ActiveRecord::Base
include Expirable
include TokenAuthenticatable
add_authentication_token_field :token
serialize :scopes, Array
belongs_to :user
2016-12-28 16:19:08 +00:00
default_scope { where(impersonation: false) }
scope :active, -> { where(revoked: false).where("expires_at >= NOW() OR expires_at IS NULL") }
scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
2016-12-28 16:19:08 +00:00
scope :impersonation, -> { where(impersonation: true) }
2016-04-15 15:24:20 +00:00
2016-12-28 16:19:08 +00:00
class << self
alias_method :and_impersonation_tokens, :unscoped
def generate(params)
personal_access_token = self.new(params)
personal_access_token.ensure_token
personal_access_token
end
end
2016-04-15 15:24:20 +00:00
def revoke!
self.revoked = true
self.save
end
def active?
!revoked? && !expired?
end
end