Class: DoorMat::AttrSymmetricStore::AttrSymmetricStoreWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/door_mat/attr_symmetric_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute, actor_column) ⇒ AttrSymmetricStoreWrapper

Returns a new instance of AttrSymmetricStoreWrapper.



5
6
7
8
# File 'lib/door_mat/attr_symmetric_store.rb', line 5

def initialize(attribute, actor_column)
  @attribute = attribute
  @actor_column = actor_column
end

Instance Method Details

#after_find(record) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/door_mat/attr_symmetric_store.rb', line 10

def after_find(record)
  return if DoorMat::Crypto.current_skip_crypto_callback.skip?

  encrypted_attribute = record.send("#{@attribute}")
  actor = record.send("#{@actor_column}")
  clear_attribute = nil

  DoorMat::Session.current_session.autoload_sesion_for(actor)
  DoorMat::Session.current_session.with_session_for_actor(actor) do |session|
    clear_attribute = session.decrypt(encrypted_attribute)
  end

  if clear_attribute.nil?
    clear_attribute = '[ENCRYPTED]'
    record.readonly!
  end

  record.send("#{@attribute}=", clear_attribute)
  DoorMat.configuration.logger.debug "DEBUG: Decrypt #{@attribute}: #{encrypted_attribute} -> #{clear_attribute}" if Rails.env.development?
end

#around_save(record) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/door_mat/attr_symmetric_store.rb', line 31

def around_save(record)
  return yield if DoorMat::Crypto.current_skip_crypto_callback.skip?

  raise ActiveRecord::Rollback, "Record is read-only" if record.readonly?

  clear_attribute = record.send("#{@attribute}")
  actor = record.send("#{@actor_column}")
  encrypted_attribute = nil

  DoorMat::Session.current_session.autoload_sesion_for(actor)
  DoorMat::Session.current_session.with_session_for_actor(actor) do |session|
    encrypted_attribute = session.encrypt(clear_attribute)
  end
  raise ActiveRecord::Rollback, "DoorMat::Session is not valid" if encrypted_attribute.nil?

  DoorMat.configuration.logger.debug "DEBUG: Encrypt #{@attribute}: #{clear_attribute} -> #{encrypted_attribute}" if Rails.env.development?
  record.send("#{@attribute}=", encrypted_attribute)
  yield
  record.send("#{@attribute}=", clear_attribute)
  DoorMat.configuration.logger.debug "DEBUG: Decrypt #{@attribute}: #{encrypted_attribute} -> #{clear_attribute}" if Rails.env.development?
end