lib/symmetric_encryption/mongoid.rb in symmetric-encryption-3.3 vs lib/symmetric_encryption/mongoid.rb in symmetric-encryption-3.4.0
- old
+ new
@@ -93,11 +93,13 @@
encrypted_field_name = field.name
decrypted_field_name = options.delete(:decrypt_as)
if decrypted_field_name.nil? && encrypted_field_name.to_s.start_with?('encrypted_')
decrypted_field_name = encrypted_field_name.to_s['encrypted_'.length..-1]
- else
+ end
+
+ if decrypted_field_name.nil?
raise "SymmetricEncryption for Mongoid. Encryption enabled for field #{encrypted_field_name}. It must either start with 'encrypted_' or the option :decrypt_as must be supplied"
end
random_iv = options.delete(:random_iv) || false
compress = options.delete(:compress) || false
@@ -117,11 +119,12 @@
mod.module_eval(<<-EOS, __FILE__, __LINE__ + 1)
# Set the un-encrypted field
# Also updates the encrypted field with the encrypted value
# Freeze the decrypted field value so that it is not modified directly
def #{decrypted_field_name}=(value)
- self.#{encrypted_field_name} = @stored_#{encrypted_field_name} = ::SymmetricEncryption.encrypt(value,#{random_iv},#{compress},:#{type})
- @#{decrypted_field_name} = value.freeze
+ v = SymmetricEncryption::coerce(value, :#{type})
+ self.#{encrypted_field_name} = @stored_#{encrypted_field_name} = ::SymmetricEncryption.encrypt(v,#{random_iv},#{compress},:#{type})
+ @#{decrypted_field_name} = v.freeze
end
# Returns the decrypted value for the encrypted field
# The decrypted value is cached and is only decrypted if the encrypted value has changed
# If this method is not called, then the encrypted value is never decrypted