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

36 lines
925 B
Ruby
Raw Normal View History

class PersonalAccessToken < ActiveRecord::Base
include Expirable
include TokenAuthenticatable
add_authentication_token_field :token
serialize :scopes, Array # rubocop:disable Cop/ActiveRecordSerialize
belongs_to :user
before_save :ensure_token
2017-02-27 13:56:54 -05:00
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
scope :inactive, -> { where("revoked = true OR expires_at < NOW()") }
2017-02-27 13:56:54 -05:00
scope :with_impersonation, -> { where(impersonation: true) }
scope :without_impersonation, -> { where(impersonation: false) }
2016-04-15 11:24:20 -04:00
validates :scopes, presence: true
validate :validate_scopes
2016-04-15 11:24:20 -04:00
def revoke!
update!(revoked: true)
2016-04-15 11:24:20 -04:00
end
def active?
!revoked? && !expired?
end
protected
def validate_scopes
unless scopes.all? { |scope| Gitlab::Auth::AVAILABLE_SCOPES.include?(scope.to_sym) }
errors.add :scopes, "can only contain available scopes"
end
end
end