用于编辑帐户中密码字段的 Laravel 4 验证器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17899755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:11:38  来源:igfitidea点击:

Laravel 4 validator for password field in edit account

phpvalidationlaravellaravel-4

提问by Mwirabua Tim

I need to check if a user has posted the same password as the one in the database. Field for old password is 'oldpass'. The custom validator i created is called 'passcheck'. It should fail or pass accordingly.

我需要检查用户是否发布了与数据库中的密码相同的密码。旧密码字段是“oldpass”。我创建的自定义验证器称为“passcheck”。它应该相应地失败或通过。

My UsersController code below doesnt work. What could have I have done wrong?

下面我的 UsersController 代码不起作用。我可能做错了什么?

    $rules = array(
        'oldpass'   =>  'passcheck',
    );

    $messages = array(
        'passcheck' => 'Your old password was incorrect',
    );


    Validator::extend('passcheck', function($attribute, $value, $parameters)
    {
        if(!DB::table('users')->where('password', Hash::make(Input::get('oldpass')))->first()){
            return false;
        }
        else{
            return true;
        };
    });

    $validator = Validator::make($inputs, $rules, $messages);

回答by The Alpha

You should use something like this,

你应该使用这样的东西,

$user = DB::table('users')->where('username', 'someusername')->first();
if (Hash::check(Input::get('oldpass'), $user->password)) {
    // The passwords match...
    return true;
}
else {
    return false;
}

So, you have to get the record using usernameor any other fieldand then check the password.

因此,您必须使用username或任何其他方式获取记录field,然后检查密码。

@lucasmichotoffered even shorter solution:

@lucasmichot提供了更短的解决方案:

Validator::extend('passcheck', function ($attribute, $value, $parameters) 
{
    return Hash::check($value, Auth::user()->getAuthPassword());
});

回答by Vladislav Rastrusny

I would make it like this:

我会让它像这样:

/**
 * Rule is to be defined like this:
 *
 * 'passcheck:users,password,id,1' - Means password is taken from users table, user is searched by field id equal to 1
 */
Validator::extend('passcheck', function ($attribute, $value, $parameters) {
    $user = DB::table($parameters[0])->where($parameters[2], $parameters[3])->first([$parameters[1]]);
    if (Hash::check($value, $user->{$parameters[1]})) {
        return true;
    } else {
        return false;
    }
});

This validator rule will make database query to check current user's password

此验证器规则将使数据库查询以检查当前用户的密码

You can make it even shorter and save query:

你可以让它更短并保存查询:

Validator::extend('passcheck', function ($attribute, $value, $parameters) {
    return Hash::check($value, Auth::user()->getAuthPassword());
});

回答by igaster

Please dont tie your rule to an Html element. Use the parameters Laravel provides to create your custom rules. This would be (asuming that you have a user authenticated):

请不要将您的规则绑定到 Html 元素。使用 Laravel 提供的参数来创建自定义规则。这将是(假设您的用户已通过身份验证):

Validator::extend('passcheck', function($attribute, $value, $parameters) {
    return Hash::check($value, Auth::user()->password); // Works for any form!
});

$messages = array(
    'passcheck' => 'Your old password was incorrect',
);

$validator = Validator::make(Input::all(), [
    'oldpass'  => 'passcheck',
    // more rules ...
], $messages);