【问题标题】:How to get encrypted password from database to show (still as::password) on update/edit?如何从数据库中获取加密密码以在更新/编辑时显示(仍为::password)?
【发布时间】:2018-10-18 02:19:36
【问题描述】:

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

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

我该怎么做?

【问题讨论】:

  • 如何使用输入字段的提示/占位符属性来指示密码已经到位?您可以在此处显示“留空以保留旧密码”之类的文字。
  • @ma_il 尽管我很想这样做,但 QA 坚持认为它应该显示出来。所以尽力想办法

标签: ruby-on-rails ruby-on-rails-4 activeadmin


【解决方案1】:

因此,如果将来有人可能需要此功能,请进行更新。

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

在 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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • hmm readonly 最好用于查看(但仍然不建议将其显示为字符串,对吧?),而在我是更新表单时,我仍然需要它显示为可更新的密码类型字段。所以我试图不从中创造另一个领域。由于某种原因,密码并没有真正显示为字段上的密码
    • 如果您指的是显示纯文本,将其解密为纯文本将在计算上很昂贵。 Devise 也是这样设计的。
    • > “出于某种原因,密码并没有真正显示为字段上的密码” 是的,我也尝试过,密码字段不接受默认或预定义的值属性。
    • 我实际上并没有尝试以纯文本形式显示它。基本上f.input :password, as: :password 不会在字段中显示任何内容。我至少希望加密密码显示as::password 而不是字符串。但它甚至不会显示我是否做了f.input :encrypted_password, label: "Current Password", as: :password
    猜你喜欢
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-11
    • 2022-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多