mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-16 06:06:56 +08:00
34 lines
634 B
Ruby
34 lines
634 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
class AssetSyncToken < ApplicationRecord
|
||
|
belongs_to :user
|
||
|
belongs_to :asset
|
||
|
|
||
|
after_initialize :generate_token
|
||
|
after_initialize :set_default_expiration
|
||
|
|
||
|
validates :token, uniqueness: true, presence: true
|
||
|
|
||
|
def version_token
|
||
|
asset.updated_at.to_i.to_s
|
||
|
end
|
||
|
|
||
|
def token_valid?
|
||
|
!revoked_at? && expires_at > Time.current
|
||
|
end
|
||
|
|
||
|
def conflicts?(token)
|
||
|
version_token != token
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def generate_token
|
||
|
self.token ||= SecureRandom.urlsafe_base64(32)
|
||
|
end
|
||
|
|
||
|
def set_default_expiration
|
||
|
self.expires_at ||= Constants::ASSET_SYNC_TOKEN_EXPIRATION.from_now
|
||
|
end
|
||
|
end
|