2013-12-19 70 views
3

我正在使用laravel-4 auth組件。我想創建一個可以改變用戶密碼的函數。我的觀點如下:如何使用auth更改laravel-4中的密碼

password - Text-box; new_password - 文本框; confirm_new_password - 文本框

我也檢查了手動密碼重置,但在該文檔(http://laravel.com/docs/security#password-reminders-and-reset)他們發送密碼重置郵件。 查看如下:

@extends('layouts.main') 
@section('title') Change Password 
@stop 
@section('content') 
{{ Form::open(array('url'=>'users/user-password-change', 'class'=>'block small center login')) }} 
<h3 class="">Change Password</h3> 
    <h6>Please change your password below.</h6> 
    <ul> 
     @foreach($errors->all() as $error) 
      <li>{{ $error }}</li> 
     @endforeach 
    </ul> 

    {{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Old Password')) }} 
    {{ Form::password('new_password', array('class'=>'input-block-level', 'placeholder'=>'New Password')) }} 
    {{ Form::password('confirm_new_password', array('class'=>'input-block-level', 'placeholder'=>'Confirm New Password')) }} 


    {{ Form::submit('Register', array('class'=>'k-button'))}} 
{{ Form::close() }} 
@stop 

控制器代碼如下:

public function postUserPasswordChange(){ 
    $validator = Validator::make(Input::all(), User::$change_password_rules); 
    if($validator->passes()){ 
     $user=new UserEventbot; 
     $user->password=Hash::make(Input::get('new_password')); 
     $user->save(); 
     return Redirect::to('users/change-password'); 
    }else { 
     return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput(); 
    } 
} 

請幫我在這,如何先匹配此密碼與數據庫表的用戶,然後整個過程。 謝謝。

+0

什麼在你的控制器中?什麼是內部更新功能。 – Anam

+0

我添加了控制器代碼,請檢查。 –

+0

用戶登錄? – Anam

回答

5

嘗試以下操作:

public function postUserPasswordChange(){ 
    $validator = Validator::make(Input::all(), User::$change_password_rules); 
    if($validator->passes()){ 

     $user = UserEventbot::findOrFail(Auth::user()->id); 

     $user->password = Hash::make(Input::get('new_password')); 
     $user->save(); 
     return Redirect::to('users/change-password'); 
    }else { 
     return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator)->withInput(); 
    } 
} 
+1

是的,它現在有用,謝謝。還有一件事是如何將密碼與數據庫密碼相匹配。 –

9

要在數據庫密碼匹配當前用戶的密碼,你可以做這樣的事情,

// retrieve the authenticated user 
$user = Auth::user(); 

檢索由內部用戶指定的當前密碼錶格,

$current_password = Input::get('current_password'); 

我們可以s如果當前密碼蜜蜂規定已經EE和檢查對哈希密碼如下,

if (strlen($current_password) > 0 && !Hash::check($current_password, $user->password)) { 
     return Redirect::to('/user/edit-profile')->withErrors('Please specify the good current password'); 
    } 

重要的事情,這裏要注意的是功能

哈希::檢查(CURRENT_PASSWORD_ENTERED_IN_FORM,HASHED_VERSION_OF_PASSWORD_STORED_IN_AUTH_USER)

最後,如果一切正常,您可以按照以下方式更新Auth和數據庫中的當前用戶密碼,

// authenticated user 
$user = Auth::user(); 
$user->password = Hash::make($new_password); 

// finally we save the authenticated user 
$user->save(); 
4

這是我自己的解決方案,我有3個輸入= old_password,password,password_confirmation,form = action = form。 驗證檢查的要求,哈希檢查,如果舊密碼匹配

public function changePassword() 
{ 
    $user = Auth::user(); 
    $rules = array(
     'old_password' => 'required|alphaNum|between:6,16', 
     'password' => 'required|alphaNum|between:6,16|confirmed' 
    ); 

    $validator = Validator::make(Input::all(), $rules); 

    if ($validator->fails()) 
    { 
     return Redirect::action('[email protected]',$user->id)->withErrors($validator); 
    } 
    else 
    { 
     if (!Hash::check(Input::get('old_password'), $user->password)) 
     { 
      return Redirect::action('[email protected]',$user->id)->withErrors('Your old password does not match'); 
     } 
     else 
     { 
      $user->password = Hash::make(Input::get('password')); 
      $user->save(); 
      return Redirect::action('[email protected]',$user->id)->withMessage("Password have been changed"); 
     } 
    } 
} 
0

請嘗試以下變化

public function postUserPasswordChange(){ 
    $validator = Validator::make(Input::all(), User::$change_password_rules); 
    if($validator->passes()){ 
     $user = UserEventbot::findOrFail(Auth::user()->id); 
     if(Hash::check(Input::get('password'), $user->getAuthPassword())) { 
      $user->password = Hash::make(Input::get('new_password')); 
      $user->save(); 
      return Redirect::to('users/change-password')->with('message','Your password has been changed'); 
     } 
    } 
    return Redirect::to('users/change-password')->with('message', 'The following errors occurred')->withErrors($validator); 
} 

您應該排除withInput()在password.Hope的來看,這幫助