【问题标题】:Laravel 4.2 Validation Rules - Current Password Must Match DB ValueLaravel 4.2 验证规则 - 当前密码必须匹配数据库值
【发布时间】:2014-07-18 16:49:16
【问题描述】:

在密码重置表单上,用户提供current_passwordpasswordpassword-confirmation。有没有办法在验证规则中指定current_password(它的哈希值)必须与数据库值匹配?

目前我有这个:

$rules = array(
    'current_password' => 'required',
    'password'         => 'required|confirmed|min:22'
); 

谢谢。

更新

感谢@ChrisForrence 和@Ben,我想出了以下方法,效果很好!非常感激。希望这对其他人有帮助:

Validator::extend('hashmatch', function($attribute, $value, $parameters)
{
    return Hash::check($value, Auth::user()->$parameters[0]);
});
$messages = array(
    'hashmatch' => 'Your current password must match your account password.'
);
$rules = array(
    'current_password' => 'required|hashmatch:password',
    'password'         => 'required|confirmed|min:4|different:current_password'
);

$validation = Validator::make( Input::all(), $rules, $messages );

【问题讨论】:

  • 这可能会有所帮助:Custom validation rules
  • 好点!让我试一试,我们会在那里试一试。谢谢。

标签: php validation laravel-4 bcrypt


【解决方案1】:

你不能,bcrypt 哈希是唯一的(它们有自己的随机盐),所以即使你知道用户的纯文本密码,你也无法进行哈希到哈希的比较。

实际上,您可以通过在控制器上执行 Hash::check('plain text password', 'bcrypt hash') 来对照 bcrypt 哈希检查纯文本密码。

【讨论】:

  • 谢谢。我希望check 的返回值是布尔值——这应该会有很大帮助!
  • @user3558931 是的,它是布尔值。
  • 是否可以在模型中做到这一点?
猜你喜欢
  • 2015-10-10
  • 1970-01-01
  • 2015-08-30
  • 2020-07-17
  • 1970-01-01
  • 2014-12-29
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
相关资源
最近更新 更多