Laravel 更新密码通过验证但不更新记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28399899/
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 10:52:23  来源:igfitidea点击:

Laravel update password passes validation but doesn't update record

phpauthenticationlaravelpasswordseloquent

提问by 001221

Hi I am using Laravel and I am using a couple of packages for user auth and roles which are Zizaco Confideand I want to update the users password firstly they should input there current password a new password and confirm the password and they should match. Now the validation works but the users password isn't updated in the database. My code is below:

嗨,我正在使用 Laravel,我正在使用几个用于用户身份验证和角色的包,它们是Zizaco Confide,我想首先更新用户密码,他们应该在那里输入当前密码和新密码并确认密码,他们应该匹配。现在验证有效,但用户密码未在数据库中更新。我的代码如下:

public function updatePassword($id) {

     $rules = array(
        'now_password'          => 'required',
        'password'              => 'min:5|confirmed|different:now_password',
        'password_confirmation' => 'required_with:password|min:5'
        );
        //password update.
        $now_password       = Input::get('now_password');
        $password           = Input::get('password');
        $passwordconf       = Input::get('password_confirmation');

        $validator = Validator::make(Input::only('now_password', 'password', 'password_confirmation'), $rules);                  

      if ($validator->fails()) {    
                return Redirect::back()->withErrors($validator);
            }
        elseif (Hash::check($now_password, Auth::user()->password)) {

                    $user = User::find($id);
                    $user->password = Hash::make($password);
                    $user->save();
          return Redirect::back()->with('success', true)->with('message','User updated.');

            } else  {
                return Redirect::back()->withErrors('Password incorrect');
            }


}

Any ideas why the users password is not isn't updated using this block of code

没有使用此代码块更新用户密码的任何想法

$user = User::find($id);
$user->password = Hash::make($password);
$user->save();

User Model

用户模型

<?php

use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\ConfideUserInterface;
use Zizaco\Entrust\HasRole;

class User extends Eloquent implements ConfideUserInterface
{
    use SoftDeletingTrait;
    use ConfideUser;
    use HasRole;

    protected $softDelete = true;

    public function favourites()
    {
        return $this->hasMany('Favourite');
    }


}

回答by Xyrus

To check inputted password:

检查输入的密码:

1.

1.

 $now_password  = Input::get('now_password');
 $user = DB::table('users')->where('name', Auth::user()->name)->first();
        if(Hash::check($now_password, $user->password)){
    //Your update here
}

2.

2.

$now_password   = Input::get('now_password');
if(Hash::check($now_password, Auth::user()->password)){
    //Your update here
}

To check if they match and if the new password is different than old.

检查它们是否匹配以及新密码是否与旧密码不同。

$rules = array(
        'now_password'          => 'required|min:8',
        'password'              => 'required|min:8|confirmed|different:now_password',
        'password_confirmation' => 'required|min:8',
    );

And edit your form to (or enter your names):

并将您的表单编辑为(或输入您的姓名):

{{ Form::label('now_password', 'Old Password') }}
{{ Form::password('now_password')}}
{{ Form::label('password', 'New Password') }}
{{ Form::password('password')}}
{{ Form::label('password_confirmation', 'Confrim New Password') }}
{{ Form::password('password_confirmation')}}

Update

更新

Ok, so you don't want to edit only passwords.

好的,所以您不想只编辑密码。

Edit your rules:

编辑规则:

$rules = array(
'now_password'          => 'required|min:5',
'password'              => 'min:5|confirmed|different:now_password',
'password_confirmation' => 'required_with:password|min:5'
);

I think that current password should be required in every type of change. Other inputs imo shouldn't be required, because you don't know which data user want to edit.

我认为在每种类型的更改中都应该需要当前密码。不需要其他输入 imo,因为您不知道用户想要编辑哪个数据。

You should also add to your rules something like:

您还应该在规则中添加以下内容:

'username'  => alpha_num|unique:users,username

etc.. (for more see http://laravel.com/docs/4.2/validation#available-validation-rules)

等..(有关更多信息,请参阅http://laravel.com/docs/4.2/validation#available-validation-rules

If Validator pass, you should check which data user want to change (which inputs are not empty). Something like:

如果验证器通过,您应该检查用户想要更改哪些数据(哪些输入不为空)。就像是:

if(!empty(Input::get('firstname'))){
    $user->firstname    = Input::get('firstname');
}

etc...with every input.

等等...每个输入。

回答by Hossein Jabbari

Try it like this:

像这样尝试:

public function updatePassword($id)
{
    $user = User::findOrFail($id);

    User::$rules['now_password'] = 'required';
    // other rules here

    $validator = Validator::make($data = Input::all(), User::rules('update', $id));

    if ($validator->fails())
    {
        return Redirect::back()->withErrors($validator)->withInput();
    }

    array_forget($data, 'password_confirmation');
    array_forget($data, 'now_password');

    $data['password'] = Hash::make($data['password']);

    $user->update($data);

    return Redirect::back()->with('success', true)->with('message','User updated.');
}

回答by lahiru dilshan

remember your password confirmation field name must be "password_confirmation" if you used first way... if you used another name for password confirm field you can use second way.

请记住,如果您使用第一种方式,您的密码确认字段名称必须是“password_confirmation”……如果您使用其他名称作为密码确认字段,您可以使用第二种方式。

$this->validate($request, [
            'email'    => 'required|unique:user|email|max:255',
            'username' => 'required|max:20',
            'password' => 'required|min:3|confirmed',
            'password_confirmation' => 'required',
            'gender' => 'required|in:m,f',
        ]);

$this->validate($request, [
            'email'    => 'required|unique:user|email|max:255',
            'username' => 'required|max:20',
            'password' => 'required|min:3',
            'confirm_password' => 'same:password',
            'gender' => 'required|in:m,f',
        ]);

回答by Mahesh Jadhav

public function change_password_post($id)
    {
        $rules = array(
                    'current'       =>  'required|string|min:8',
                    'new'       =>  'required|string|min:8',
                    'confirm'       =>  'required|same:new'
                    );

        $validator = Validator::make(Input::only('current', 'new', 'confirm'), $rules);

        if($validator->fails())
        {
            return Redirect::back()
                ->withErrors($validator);
        }
else
            {
                $users = User::where('id', '=', $id)->first();

                if (Hash::check(Input::get('current'), $users->password))
                {
                    if(Input::get('new') == Input::get('confirm'))
                    {
                        $users->password =Hash::make(Input::get('new'));
                        $users->save();

                        $msg = array('msg' => 'Password changed Successfully');

                        return Redirect::back()
                            ->withErrors($msg);
                    }
                    else
                    {
                        $msg = array('msg' => 'New password and Confirm password did not match');

                        return Redirect::back()
                                ->withErrors($msg);
                    }
                }
                else
                {
                    $msg = array('msg' => 'Current password is incorrect');

                    return Redirect::back()
                        ->withErrors($msg);
                }
        }
    }