mirror of
https://github.com/SrIzan10/hc-harbor.git
synced 2026-05-01 10:45:21 +00:00
26 lines
499 B
Ruby
26 lines
499 B
Ruby
class AdminApiKey < ApplicationRecord
|
|
belongs_to :user
|
|
|
|
validates :token, presence: true, uniqueness: true
|
|
validates :name, presence: true, uniqueness: { scope: :user_id }
|
|
|
|
before_validation :generate_token!, on: :create
|
|
|
|
scope :active, -> { where(revoked_at: nil) }
|
|
|
|
def active?
|
|
revoked_at.nil?
|
|
end
|
|
|
|
def revoke!
|
|
update!(revoked_at: Time.current)
|
|
end
|
|
|
|
private
|
|
|
|
def generate_token!
|
|
# should be jazzy enough
|
|
self.token ||= "hka_#{SecureRandom.hex(32)}"
|
|
end
|
|
end
|