如何从数据库中获取加密密码以在 update/edit 上显示(仍 as::password)?

How to get encrypted password from database to show (still as::password) on update/edit?

我可以在 ActiveAdmin 中添加新用户。我可以将其视为加密的密码字符串,但当我编辑它时,密码字段为空。

我知道它们保持不变,但在用户体验方面,我需要向编辑用户的人表明密码已经到位。我正在使用设计验证,因此使用加密密码。

我该怎么做?

我修改了 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

因此,如果将来有人可能需要它,请更新一下。

由于您通常无法将加密的密码放回密码字段以进行更新,我认为另一种解决方法是能够在更新时隐藏两个密码字段并对其执行单独的操作只有用户可以通过 his/her 电子邮件实际更改密码。

在 ActiveAdmin 中我写了这样的表格:

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

结束

此解决方案的障碍是验证。我必须能够允许用户更新其他字段而无需每次都更新密码。这不是一个完美的解决方案,但我只在创建时验证它们的存在。

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

是的,它通过传递令牌通过电子邮件发送更改密码指令