【问题标题】:Laravel - Confide - Save Change PasswordLaravel - Confide - 保存更改密码
【发布时间】:2014-10-22 21:42:15
【问题描述】:

我设法保存了新密码或更改了已登录用户的密码。

    public function saveNewPassword() {
    $rules = array(
        'old_password'            => 'required',
        'password'       => 'required|confirmed|different:old_password',
        'password_confirmation' => 'required|different:old_password|same:password_confirmation'
    );
    $user = User::findOrFail(Auth::user()->id);
    // Validate the inputs
    $validator = Validator::make(Input::all(), $rules);


    if ($validator->fails()) {
        return Redirect::back()
            ->withErrors($validator)
            ->withInput();
    } else {
        $password = Input::get( 'password' );
        $passwordConfirmation = Input::get( 'password_confirmation' );

        if(!empty($password)) {
            if($password === $passwordConfirmation) {
                $user->password = $password;
                $user->password_confirmation = $passwordConfirmation;
            } 
        } else {
            unset($user->password);
            unset($user->password_confirmation);
        }

        // Save if valid. Password field will be hashed before save
        $user->save();
    }

    // Get validation errors (see Ardent package)
    $error = $user->errors()->all();

    if(empty($error)) {
        Session::flash('message', 'Successfully saved!');
        return Redirect::back();

    } else {
        Session::flash('error', $error);
        return Redirect::back();
    }
}  

我的问题是,如何检查旧密码,即等于当前密码?有任何想法吗? Confide 有自己的密码修改方法吗?

【问题讨论】:

  • 你不能用Hash::check('old_password', $user->password)吗?

标签: php laravel laravel-4 change-password


【解决方案1】:

我使用这个解决方案来更改密码。在您的规则中,您有一个错误:password_confirmation 应该与 password 而不是 password_confirmation 相同。

这是完整且经过测试的功能:

public function changePassword($id){
    $rules = array(
        'old_password'                  => 'required',
        'new_password'                  => 'required|confirmed|different:old_password',
        'new_password_confirmation'     => 'required|different:old_password|same:new_password'
    );

    $user = User::find(Auth::user()->id);
    $validator = Validator::make(Input::all(), $rules);

    //Is the input valid? new_password confirmed and meets requirements
    if ($validator->fails()) {
        Session::flash('validationErrors', $validator->messages());
        return Redirect::back()->withInput();
    }

    //Is the old password correct?
    if(!Hash::check(Input::get('old_password'), $user->password)){
        return Redirect::back()->withInput()->withError('Password is not correct.');
    }

    //Set new password to user
    $user->password = Input::get('new_password');
    $user->password_confirmation = Input::get('new_password_confirmation');

    $user->touch();
    $save = $user->save();

    return Redirect::to('logout')->withMessage('Password has been changed.');

}

如果您不使用 Confide,这也适用。

【讨论】:

    【解决方案2】:

    来自confide的github:

    与 Laravel Auth and Reminders 组件/配置集成。

    所以我猜想使用Auth::validate() 方法可以解决问题。

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 2017-03-26
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      相关资源
      最近更新 更多