Module: ManageIQ::Password::PasswordMixin::ClassMethods

Defined in:
lib/manageiq/password/password_mixin.rb

Instance Method Summary collapse

Instance Method Details

#encrypt_column(column) ⇒ Object



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 'lib/manageiq/password/password_mixin.rb', line 12

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

  mod = generated_methods_for_password_mixin

  mod.send(:define_method, column) do
    val = send("#{column}_encrypted")
    (val.nil? || val.empty?) ? val : MiqPassword.decrypt(val)
  end

  mod.send(:define_method, "#{column}=") do |val|
    val = MiqPassword.try_encrypt(val) unless val.nil? || val.empty?
    send("#{column}_encrypted=", val)
  end

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

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