Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let Rails Admin play nicely with Lockbox #3241

Open
bo-oz opened this issue Jan 15, 2020 · 3 comments
Open

Let Rails Admin play nicely with Lockbox #3241

bo-oz opened this issue Jan 15, 2020 · 3 comments

Comments

@bo-oz
Copy link

bo-oz commented Jan 15, 2020

I have a strange issue. I'm using Lockbox to encrypt my database values for the user model. Lockbox stores the field as {field_name}_ciphertext. Besides encrypting the data, lockbox also provides instance methods with the original {field_name}. I'd expect Rails Admin to be able to display the field, by calling the instance method, but somehow this doesn't work.

# suppose my field is name, this doesn't work

rails_admin do
  list do
    fields :name
  end
end

If I manually create an instance method, that is basically an alias for the instance method Lockbox creates, it does work:

def name_decode
  name
end

rails_admin do
  list do
    fields :name_decode
  end
end

Any ideas how I could let Rails Admin call the correct method on the instance?

@bo-oz
Copy link
Author

bo-oz commented Jan 15, 2020

What is strange is, when I look at another model, that is related to the User model (that has the name_cipertext encryption), that User model's name is de decyphered value for name. So in relationships it works, but when showing the original record, it doesn't.

@incubus
Copy link

incubus commented Apr 1, 2020

Hi! Have you found any solution?

@incubus
Copy link

incubus commented Apr 2, 2020

Well, here is my solution with app/models/concerns/entity/encryptable.rb:

module Entity
  module Encryptable
    def safe_send(value)
      if self.class.lockbox_attributes.key?(value)
        send(value)
      else
        super
      end
    end
  end
end
class User < ApplicationRecord
  include Entity::Encryptable
  encrypts :email, :name
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants