2018-03-19 12:11:12 -04:00
|
|
|
class DeployToken < ActiveRecord::Base
|
|
|
|
include Expirable
|
|
|
|
include TokenAuthenticatable
|
|
|
|
add_authentication_token_field :token
|
|
|
|
|
2018-04-04 19:43:41 -04:00
|
|
|
AVAILABLE_SCOPES = %w(read_repository read_registry).freeze
|
2018-03-19 12:11:12 -04:00
|
|
|
|
|
|
|
serialize :scopes, Array # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
|
|
|
|
validates :scopes, presence: true
|
2018-04-04 19:43:41 -04:00
|
|
|
validates :project, presence: true
|
2018-03-19 12:11:12 -04:00
|
|
|
|
|
|
|
belongs_to :project
|
|
|
|
|
|
|
|
before_save :ensure_token
|
|
|
|
|
|
|
|
scope :active, -> { where("revoked = false AND (expires_at >= NOW() OR expires_at IS NULL)") }
|
|
|
|
|
|
|
|
def revoke!
|
|
|
|
update!(revoked: true)
|
|
|
|
end
|
|
|
|
|
2018-03-29 22:11:36 -04:00
|
|
|
def redis_shared_state_key(user_id)
|
|
|
|
"gitlab:deploy_token:#{project_id}:#{user_id}"
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|
2018-03-29 18:56:35 -04:00
|
|
|
|
|
|
|
def active?
|
|
|
|
!revoked
|
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
|
|
|
User.ghost.username
|
|
|
|
end
|
2018-03-19 12:11:12 -04:00
|
|
|
end
|