Module: EncryptionConcern::ClassMethods

Defined in:
app/models/concerns/encryption_concern.rb

Instance Method Summary collapse

Instance Method Details

#encrypt_column(column) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/concerns/encryption_concern.rb', line 11

def encrypt_column(column)
  # Given a column of "password", create 4 instance methods:
  #   password            : Get the password in plain text
  #   password=           : Set the password in plain text
  #   password_encrypted  : Get the password in cryptext
  #   password_encrypted= : Set the password in cryptext

  encrypted_columns << column.to_s
  encrypted_columns << "#{column}_encrypted"

  mod = generated_methods_for_password_mixin

  mod.send(:define_method, column) do
    val = send("#{column}_encrypted")
    val.blank? ? val : ManageIQ::Password.decrypt(val)
  end

  mod.send(:define_method, "#{column}=") do |val|
    val = ManageIQ::Password.try_encrypt(val) if val.present?
    send("#{column}_encrypted=", val)
  end

  mod.send(:define_method, "#{column}_encrypted") do
    self[column]
  end

  mod.send(:define_method, "#{column}_encrypted=") do |val|
    self[column] = val
  end
end