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

5 6
  serialize :scopes, Array

7 8
  belongs_to :user

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

12 13
  validate :validate_scopes

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

  def revoke!
    self.revoked = true
    self.save
  end
24 25 26 27 28 29 30 31

  protected

  def validate_scopes
    unless Set.new(scopes.map(&:to_sym)).subset?(Set.new(Gitlab::Auth::API_SCOPES))
      errors.add :scopes, "can only contain API scopes"
    end
  end
32
end