debian-mirror-gitlab/app/models/personal_access_token.rb

37 lines
888 B
Ruby
Raw Normal View History

2016-06-22 15:30:34 +05:30
class PersonalAccessToken < ActiveRecord::Base
2017-08-17 22:00:37 +05:30
include Expirable
2016-06-22 15:30:34 +05:30
include TokenAuthenticatable
add_authentication_token_field :token
2017-08-17 22:00:37 +05:30
serialize :scopes, Array
2016-06-22 15:30:34 +05:30
belongs_to :user
2017-08-17 22:00:37 +05:30
before_save :ensure_token
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
2016-06-22 15:30:34 +05:30
scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
2017-08-17 22:00:37 +05:30
scope :with_impersonation, -> { where(impersonation: true) }
scope :without_impersonation, -> { where(impersonation: false) }
2016-06-22 15:30:34 +05:30
2017-08-17 22:00:37 +05:30
validates :scopes, presence: true
validate :validate_api_scopes
2016-06-22 15:30:34 +05:30
def revoke!
self.revoked = true
self.save
end
2017-08-17 22:00:37 +05:30
def active?
!revoked? && !expired?
end
protected
def validate_api_scopes
unless scopes.all? { |scope| Gitlab::Auth::API_SCOPES.include?(scope.to_sym) }
errors.add :scopes, "can only contain API scopes"
end
end
2016-06-22 15:30:34 +05:30
end