reset password

parent d4587571
...@@ -7,6 +7,7 @@ use yii\web\Controller; ...@@ -7,6 +7,7 @@ use yii\web\Controller;
use common\models\LoginForm; use common\models\LoginForm;
use common\models\RecoveryForm; use common\models\RecoveryForm;
use common\models\ResetPasswordForm;
use common\modules\users\models\User; use common\modules\users\models\User;
/** /**
...@@ -110,6 +111,27 @@ class SiteController extends Controller ...@@ -110,6 +111,27 @@ class SiteController extends Controller
]); ]);
} }
public function actionResetPassword($token)
{
$success = false;
try {
$model = new ResetPasswordForm($token);
} catch (InvalidParamException $e) {
throw new BadRequestHttpException($e->getMessage());
}
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword())
{
$success = true;
}
return $this->render('reset-password', [
'model' => $model,
'success' => $success,
]);
}
public function actionLogout() public function actionLogout()
{ {
Yii::$app->user->logout(); Yii::$app->user->logout();
......
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<!-- begin login -->
<div class="login login-v2" data-pageload-addclass="animated flipInX">
<!-- begin brand -->
<div class="login-header">
<div class="brand">
<img src="/img/logo.png">
<small>Востановление пароля</small>
</div>
<div class="icon">
<i class="fa fa-sign-in"></i>
</div>
</div>
<!-- end brand -->
<div class="login-content">
<?php if($success) : ?>
<center>
Новый пароль успешно сохранен. <br>
<?=Html::a('Вернуться к авторизации', ['login'])?>
</center>
<?php else : ?>
<?php $form = ActiveForm::begin([
'enableClientValidation' => true,
'id' => 'login-form',
'options' => [
'class' => 'margin-bottom-0'
],
'fieldConfig' => [
'template' => '{input}{error}',
],
]); ?>
<div class="form-group m-b-20">
<?= $form->field(
$model,
'password',
[
'inputOptions' => [
'class' => 'form-control input-lg',
'placeholder' => 'Введите новый пароль',
]
]
)->passwordInput()->label(false) ?>
</div>
<div class="login-buttons">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success btn-block btn-lg', 'name' => 'login-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php endif; ?>
</div>
</div>
<!-- end login -->
<?php
$this->registerJsFile('/js/login-v2.demo.min.js', ['position' => \yii\web\View::POS_END ]);
$this->registerJs('App.init();LoginV2.init();', \yii\web\View::POS_READY);
?>
\ No newline at end of file
<?php
namespace common\models;
use Yii;
use yii\base\InvalidParamException;
use yii\base\Model;
use common\modules\users\models\User;
/**
* Password reset form
*/
class ResetPasswordForm extends Model
{
public $password;
/**
* @var \common\models\User
*/
private $_user;
/**
* Creates a form model given a token.
*
* @param string $token
* @param array $config name-value pairs that will be used to initialize the object properties
* @throws \yii\base\InvalidParamException if token is empty or not valid
*/
public function __construct($token, $config = [])
{
if (empty($token) || !is_string($token)) {
throw new InvalidParamException('Password reset token cannot be blank.');
}
$this->_user = User::findByPasswordResetToken($token);
if (!$this->_user) {
throw new InvalidParamException('Wrong password reset token.');
}
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['password', 'required'],
['password', 'string', 'min' => 7],
];
}
/**
* Resets password.
*
* @return boolean if password was reset.
*/
public function resetPassword()
{
$user = $this->_user;
$user->setPassword($this->password);
$user->removePasswordResetToken();
return $user->save(false);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment