Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 validator for password field in edit account

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.

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

    $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);
like image 614
Tim Truston Avatar asked Jul 27 '13 16:07

Tim Truston


3 Answers

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 username or any other field and then check the password.

@lucasmichot offered even shorter solution:

Validator::extend('passcheck', function ($attribute, $value, $parameters) 
{
    return Hash::check($value, Auth::user()->getAuthPassword());
});
like image 85
The Alpha Avatar answered Oct 23 '22 18:10

The Alpha


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());
});
like image 32
Vladislav Rastrusny Avatar answered Oct 23 '22 18:10

Vladislav Rastrusny


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):

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);
like image 44
igaster Avatar answered Oct 23 '22 18:10

igaster