Laravel 5,更新用户密码

6

我正在使用laravel 5开发一个应用程序,允许每个用户更新自己的个人资料。
为了更新密码,用户需要首先输入旧密码,如果旧密码匹配,则新输入的密码将被哈希并存储在数据库中。 如何使用laravel表单请求验证对此进行验证?


这可能是与此问题重复了。请参考此链接 http://stackoverflow.com/questions/28399899/laravel-update-password-passes-validation-but-doesnt-update-record - HawkEye
我曾经询问过如何在更新场景中使用Laravel表单请求验证来验证用户密码,而不是在控制器操作中手动执行。 - Salar
3个回答

4
我已经创建了一个自定义验证器,并像这样将其添加到 AppServiceProvider 中:
<?php

namespace App\Providers;

use Validator;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Hash ;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('password_hash_check', function($attribute, $value, $parameters, $validator) {
            return Hash::check($value , $parameters[0]) ;
        });
    }

然后我在我的表单请求验证器中这样使用它:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UpdateUserProfileRequest extends Request
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $hashed_password = $this->user()->password ;
        return [
            'oldPassword'=> "password_hash_check:$hashed_password|string|min:6",
            'newPassword' => 'required_with:oldPassword|confirmed|min:6',
        ];
    }

旧密码规则不应该包含$hashed_password的适当连接吗?像这样:"password_hash_check:'.$hashed_password.'|string|min:6" - mhlsf

3

我不确定,但我认为在Laravel中没有本地方法来实现这一点。如果是这样,您可以实现自定义的“hash”验证器:

class CustomValidator extends \Illuminate\Validation\Validator {

    public function validateHash($attribute, $value, $parameters)
    {
        $expected = $parameters[0];

        return Hash::check($value, $expected);
    }
}

在提供程序中注册它:

class AppServiceProvider extends ServiceProvider {

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        require_once __DIR__ . '/../Http/helpers.php';

        Validator::resolver(function($translator, $data, $rules, $messages)
        {
            return new CustomValidator($translator, $data, $rules, $messages);
        });
    }

    // ...
}

并将其用于表单请求:

class MyFormRequest extends FormRequest {

    public function rules()
    {
        $password = Auth::user()->password;

        return [
            'old_password' => "required|hash:" . $password
        ]
    }

    // ...

}

链接到文档: http://laravel.com/docs/5.0/validation#custom-validation-rules 自定义验证规则。

3

当你想要检查由某个哈希算法生成的值时,

Hash::make()

你需要使用 标签。
Hash::check('unhashed', $hashed)

每次运行Hash::make('string')都会生成一个不同的哈希值,与之前的哈希值不匹配。例如:
// Generate a hash
$password = Hash::make('password');

// $password == $2y$08$T9r9qUxrr6ejs9Ne.nLzMet8l0A8BM5QvLjhaaJasgsbMBdX4JjRu

// Generate a new hash
$new_password = Hash::make('password');

// $new_password ==  $2y$08$3KBlYKIMpIvk.TWwim9oPuwGA.Pzv1iF7BsDyYkz7kQlhkA/ueULe

// Compare hashes the WRONG way
$password === $new_password; // false

// Compare hash the RIGHT way
Hash::check('password', $password); // true
Hash::check('password', $new_password); // true 

所以请使用Hash类的Hash::make()方法。

谢谢。但是我需要通过Laravel表单请求来进行验证,而不是手动验证。 - Salar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接