Sha256: 9f78f88a40d9f18dc59fe6b4cbb09427a8e6bcc0a53c2bcef4620cade65892e5

Contents?: true

Size: 1.4 KB

Versions: 3

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

# This the key used to unlock the data for one payload.
# When deleted only the encryption_key field is deleted.

module LoggableActivity
  class EncryptionKey < ActiveRecord::Base
    self.table_name = 'loggable_encryption_keys'
    require 'securerandom'
    belongs_to :record, polymorphic: true, optional: true
    belongs_to :parrent_key, class_name: 'LoggableActivity::EncryptionKey', optional: true,
                             foreign_key: 'parrent_key_id'

    def mark_as_deleted
      update(key: nil)
      parrent_key.mark_as_deleted if parrent_key.present?
    end

    def self.for_record_by_type_and_id(record_type, record_id, parrent_key = nil)
      enctyption_key = find_by(record_type:, record_id:)

      return enctyption_key if enctyption_key

      create_encryption_key(record_type, record_id, parrent_key)
    end

    def self.for_record(record, parrent_key = nil)
      encryption_key = find_by(record:)
      return encryption_key if encryption_key

      create_encryption_key(record.class.name, record.id, parrent_key)
    end

    def self.create_encryption_key(record_type, record_id, parrent_key = nil)
      if parrent_key
        create(record_type:, record_id:, key: random_key, parrent_key_id: parrent_key.id)
      else
        create(record_type:, record_id:, key: random_key)
      end
    end

    def self.random_key
      SecureRandom.hex(16)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
loggable_activity-0.1.39 lib/loggable_activity/encryption_key.rb
loggable_activity-0.1.38b lib/loggable_activity/encryption_key.rb
loggable_activity-0.1.36 lib/loggable_activity/encryption_key.rb