2013-02-28 67 views
1

我想直接在數據庫中重置用戶的密碼。我可以看到密碼通常以加密哈希的形式存儲 - 我的選擇是什麼?在數據庫中設置重置密碼?

我正在使用Devise。

+2

'update user set password = crypt_function('new password here')'? – 2013-02-28 17:29:30

回答

2

只是注意到你說'直接在數據庫中'。然後,你得到的第一條評論最有效。

如果您仍然可以通過軌道做(例如,在遷移),你可以試試這個:

user = User.find(...) 
# set plain text password, it will run 'encrypted_password=' under the hood 
user.password = "new password" 
user.save      
之後,你可能想要發送電子郵件通知或重置authentication_token,根據您的情況

1

@ cjm2671,簡短答案是否定的,你不應該。見設計是怎麼做的https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb#L4

# 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 

爲什麼你想直接做在DB?

如果您需要,您需要在數據庫上使用BCrypt(例如PostgreSQL的pgcrypto)和值self.class.peper。我假設bcrypt.salt將由BCrypt提供。

UPDATE:

我開始懷疑是可能的,我跳到迅速pgcrypto,但它似乎並沒有做你想做的。