我用的是LALAVEL-4-AUTH组件。我想要做一个功能,将改变用户的密码。我的看法如下:
密码-文本框;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();
}
}请帮助我在这方面,如何首先与数据库表用户匹配这个密码,然后整个过程。谢谢你。
发布于 2013-12-19 09:44:16
尝试如下:
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();
}
}发布于 2014-04-26 19:32:13
要将当前用户密码与数据库中的密码匹配,您可以这样做,
// retrieve the authenticated user
$user = Auth::user();检索表单中用户指定的当前密码,
$current_password = Input::get('current_password');我们可以查看当前密码是否已指定,并按以下方式对照散列密码进行检查,
if (strlen($current_password) > 0 && !Hash::check($current_password, $user->password)) {
return Redirect::to('/user/edit-profile')->withErrors('Please specify the good current password');
}这里要注意的是函数
Hash::check(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();发布于 2014-05-17 21:32:50
这是我自己的解决方案,我有3个输入= old_password、密码、password_confirmation、表单和action=post。验证器检查要求,散列检查旧密码是否匹配
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('UserController@show',$user->id)->withErrors($validator);
}
else
{
if (!Hash::check(Input::get('old_password'), $user->password))
{
return Redirect::action('UserController@show',$user->id)->withErrors('Your old password does not match');
}
else
{
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::action('UserController@show',$user->id)->withMessage("Password have been changed");
}
}
}https://stackoverflow.com/questions/20674966
复制相似问题