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

36 lines
925 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-09-10 17:25:29 +05:30
serialize :scopes, Array # rubocop:disable Cop/ActiveRecordSerialize
2017-08-17 22:00:37 +05:30
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
2017-09-10 17:25:29 +05:30
validate :validate_scopes
2016-06-22 15:30:34 +05:30
def revoke!
2017-09-10 17:25:29 +05:30
update!(revoked: true)
2016-06-22 15:30:34 +05:30
end
2017-08-17 22:00:37 +05:30
def active?
!revoked? && !expired?
end
protected
2017-09-10 17:25:29 +05:30
def validate_scopes
unless scopes.all? { |scope| Gitlab::Auth::AVAILABLE_SCOPES.include?(scope.to_sym) }
errors.add :scopes, "can only contain available scopes"
2017-08-17 22:00:37 +05:30
end
end
2016-06-22 15:30:34 +05:30
end