0

I am able to add new users in ActiveAdmin. I can view it as an encrypted password string but when I edit it, the password fields are blank.

I know that they remain unchanged but UX-wise, I need to show the person editing the user that a password is already in place. I'm using devise validation thus the encrypted password.

How can I do this?

enter image description here

Theresa
  • 35
  • 2
  • 13
  • What about using the hint / placeholder attribute of the input field to indicate that a password is already in place? You could show a text like "leave blank to keep old password" there. – Marcus Ilgner Oct 18 '18 at 10:05
  • @ma_il as much as I'd like to do that, QA insist that it should show. So trying my best to find a way – Theresa Oct 18 '18 at 23:01

2 Answers2

1

So just an update if someone might need this in the future.

Since the you can't normally place back an encrypted password back on the password field for update, I figured the other workaround is to be able to HIDE the two password fields on update and have a separate action for it that only the user can actually change the password through his/her email.

In ActiveAdmin I wrote the form like this:

form do |f|
f.inputs 'Admin Details' do
  f.input :email
  if f.object.new_record?
    f.input :password, as: :password
    f.input :password_confirmation, :label => "Password Confirmation"
  end
end
f.actions

end

The roadblock for this solution is the validation. I must be able to allow the user to update the other fields without the need to update the password everytime. It's not a perfect solution but I only validate their presence on create.

validates :password,
presence: {
  :message => 'Password cannot be blank'
},
:confirmation => true,
on: :create

validates :password,
    :confirmation => { 
      case_sensitive: true
     }, 
    :length => { 
      :within => 8..128, 
      too_short: "Password is too short (minimum is 8 characters)",
      too_long: "Password is too long (maximum is 128 characters)" 
    }, 
    :unless => lambda{ |adminuser| adminuser.password.blank? },
    on: :create

  validates :password_confirmation,
  presence: {
    :message => 'Field cannot be blank'
  }, 
  on: :create

So yeah, it's sending change password instructions through email by passing a token

Theresa
  • 35
  • 2
  • 13
0

I revised an answer from this and got it working like this

# app/admin/inputs/readonly_input.rb

class ReadonlyInput < Formtastic::Inputs::StringInput
  def to_html
    input_wrapping do
      label_html <<
      template.content_tag('div', @object.send(method))
    end
  end
end

# app/admin/admin_users.rb

ActiveAdmin.register AdminUser do
  # ...

  form do |f|
    f.semantic_errors
    f.inputs 'Admin Details' do
      f.input :email
      f.input :encrypted_password, label: "Current Password", as: :readonly
      f.input :password
      f.input :password_confirmation
    end
    f.actions
  end 
end
vpibano
  • 2,415
  • 1
  • 13
  • 26
  • hmm the readonly is best used if it's for viewing (but still not advisable to show it like a string right? ) and while on I'm the update form, I still need it to show as a password-type field which is updatable. So I'm trying not to make another field from it. for some reason password doesn't really show back as a password on field – Theresa Oct 18 '18 at 06:28
  • If you're referring to showing plain text, decrypting it to plain text will be computationally expensive. Devise is also kind of designed that way. – vpibano Oct 18 '18 at 07:14
  • > "for some reason password doesn't really show back as a password on field" Yes, I've also tried that, and the password field won't accept a default or predefined value attribute. – vpibano Oct 18 '18 at 07:17
  • I'm not actually trying to show it in plain text. Basically the `f.input :password, as: :password` will not show anything in the fields. I at least wanted the encrypted password to show `as::password` and not as a string. But it won't even show if I did an `f.input :encrypted_password, label: "Current Password", as: :password` – Theresa Oct 18 '18 at 08:49