简体   繁体   English

laravel 5,更新用户密码

[英]laravel 5,update User password

I'm using laravel 5 to develop an app that allow every user to update his profile. 我正在使用laravel 5开发一个应用程序,允许每个用户更新他的个人资料。
in order to update password, the user needs to first enter his old password and if the old password matched then his newly entered password will be hashed and stored in DB. 为了更新密码,用户需要先输入旧密码,如果旧密码匹配,则新输入的密码将被散列并存储在DB中。 how can I validate this, using laravel form request validation ? 如何使用laravel表单请求验证来验证这一点?

I created a custom validator and added it to AppServiceProvider like this: 我创建了一个自定义验证器,并将其添加到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]) ;
        });
    }

then I used it in my form request validator like this: 然后我在我的表单请求验证器中使用它,如下所示:

<?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',
        ];
    }

When you want to check a Hashed value generated by 当您想要检查生成的哈希值时

Hash::make()

you need to use 你需要使用

Hash::check('unhashed', $hashed)

Every time you run Hash::make('string') , a different hash is made and will not match the previous one. 每次运行Hash::make('string') ,都会生成一个不同的哈希值,并且与前一个哈希值不匹配。 For example: 例如:

// 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 

So Use Hash::make() method of Hash class. 所以使用Hash类的Hash :: make()方法。

I'm not sure but I think that there is no native way to do this in Laravel. 我不确定,但我认为在Laravel中没有本地方法可以做到这一点。 If so, you can implement a custom "hash" validator: 如果是这样,您可以实现自定义“哈希”验证器:

class CustomValidator extends \Illuminate\Validation\Validator {

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

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

Register it in a provider: 在提供商中注册:

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);
        });
    }

    // ...
}

And use it in a form request: 并在表单请求中使用它:

class MyFormRequest extends FormRequest {

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

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

    // ...

}

Link to documentation: http://laravel.com/docs/5.0/validation#custom-validation-rules 链接到文档: http//laravel.com/docs/5.0/validation#custom-validation-rules

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

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