简体   繁体   中英

Activeadmin custom filter encrypted attribute

I am trying to have the filter search encrypted fields.

For example, let's say in rails console, I can do the following:

u = User.find_by_email("someone@example.com")

and get a user.

But, if I get a user this way:

u = User.find(1)

the object returned has an encrypted email attribute. That said, I can still get the email on that user with:

u.email

However I don't know how to have Activeadmin/meta_search do its magic to filter by email.

Thoughts?

I'm not sure how you're encrypting an email field or why, is it to prevent a hacker from grabbing them on your database? If you don't want an attribute to be exposed in the database, but you want to be able to search for it in the Controller or Model layer then you should create a virtual attribute. This can be done with Ruby attr methods and the email should be stored upon creation.

attr_accessible :email, :email_encrypted

attr_reader :email

def email=(email)
  @email = email
  self.email_encrypted = ENCRYPTION.create(email)
end

Where the ENCRYPTION is whatever encryption library you're using, BCRYPT for instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM