1

我想直接在数据库中重置用户的密码。我可以看到密码通常存储为加密哈希 - 我的选择是什么?

我正在使用设计。

4

2 回答 2

2

刚刚注意到您说“直接在数据库中”。然后你得到的第一条评论效果最好。

如果你仍然可以通过 rails 来实现(例如在迁移中),你可以试试这个:

user = User.find(...)
# set plain text password, it will run 'encrypted_password=' under the hood
user.password = "new password" 
user.save                      

之后,您可能需要发送电子邮件通知或重置 authentication_token,具体取决于您的情况。

于 2013-02-28T17:37:58.340 回答
1

@cjm2671,简短的回答是否定的,你不应该。在https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb#L4中查看 Devise 是如何做到的

  # Verifies whether an password (ie from sign in) is the user password.
  def valid_password?(password)
    return false if encrypted_password.blank?
    bcrypt   = ::BCrypt::Password.new(encrypted_password)
    password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
    Devise.secure_compare(password, encrypted_password)
  end

为什么要直接在数据库上做呢?

如果必须,您将需要数据库上的 BCrypt(例如 PostgreSQL 的 pgcrypto)和self.class.peper. 我假设bcrypt.salt将由 BCrypt 提供。

更新:

我开始怀疑是否有可能,我很快跳到 pgcrypto,但它似乎没有做你想做的事。

于 2013-02-28T18:19:52.460 回答