繁体   English   中英

laravel 5,更新用户密码

[英]laravel 5,update User password

我正在使用laravel 5开发一个应用程序,允许每个用户更新他的个人资料。
为了更新密码,用户需要先输入旧密码,如果旧密码匹配,则新输入的密码将被散列并存储在DB中。 如何使用laravel表单请求验证来验证这一点?

我创建了一个自定义验证器,并将其添加到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',
        ];
    }

当您想要检查生成的哈希值时

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中没有本地方法可以做到这一点。 如果是这样,您可以实现自定义“哈希”验证器:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM