2020-07-20 05:09:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
class AgentToken < ApplicationRecord
|
2021-03-22 20:09:09 -04:00
|
|
|
include RedisCacheable
|
2020-07-20 05:09:22 -04:00
|
|
|
include TokenAuthenticatable
|
2021-03-22 20:09:09 -04:00
|
|
|
|
2020-10-22 23:08:37 -04:00
|
|
|
add_authentication_token_field :token, encrypted: :required, token_generator: -> { Devise.friendly_token(50) }
|
2021-04-21 05:09:15 -04:00
|
|
|
cached_attr_reader :last_used_at
|
2020-07-20 05:09:22 -04:00
|
|
|
|
|
|
|
self.table_name = 'cluster_agent_tokens'
|
|
|
|
|
2021-02-19 19:10:55 -05:00
|
|
|
belongs_to :agent, class_name: 'Clusters::Agent', optional: false
|
2021-02-16 13:09:24 -05:00
|
|
|
belongs_to :created_by_user, class_name: 'User', optional: true
|
2020-07-20 05:09:22 -04:00
|
|
|
|
|
|
|
before_save :ensure_token
|
2021-02-19 19:10:55 -05:00
|
|
|
|
|
|
|
validates :description, length: { maximum: 1024 }
|
2021-03-26 14:09:16 -04:00
|
|
|
validates :name, presence: true, length: { maximum: 255 }
|
2021-03-22 20:09:09 -04:00
|
|
|
|
2022-04-19 05:08:55 -04:00
|
|
|
scope :order_last_used_at_desc, -> { order(arel_table[:last_used_at].desc.nulls_last) }
|
2022-01-11 10:15:55 -05:00
|
|
|
scope :with_status, -> (status) { where(status: status) }
|
2021-04-21 05:09:15 -04:00
|
|
|
|
2021-12-21 22:15:20 -05:00
|
|
|
enum status: {
|
|
|
|
active: 0,
|
|
|
|
revoked: 1
|
|
|
|
}
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
end
|