added recovery process
This commit is contained in:
@ -2,12 +2,14 @@
|
|||||||
namespace Da\User\Controller;
|
namespace Da\User\Controller;
|
||||||
|
|
||||||
use Da\User\Event\FormEvent;
|
use Da\User\Event\FormEvent;
|
||||||
|
use Da\User\Event\ResetPasswordEvent;
|
||||||
use Da\User\Factory\MailFactory;
|
use Da\User\Factory\MailFactory;
|
||||||
use Da\User\Form\RecoveryForm;
|
use Da\User\Form\RecoveryForm;
|
||||||
use Da\User\Model\Token;
|
use Da\User\Model\Token;
|
||||||
use Da\User\Query\TokenQuery;
|
use Da\User\Query\TokenQuery;
|
||||||
use Da\User\Query\UserQuery;
|
use Da\User\Query\UserQuery;
|
||||||
use Da\User\Service\PasswordRecoveryService;
|
use Da\User\Service\PasswordRecoveryService;
|
||||||
|
use Da\User\Service\ResetPasswordService;
|
||||||
use Da\User\Traits\ContainerTrait;
|
use Da\User\Traits\ContainerTrait;
|
||||||
use Da\User\Traits\ModuleTrait;
|
use Da\User\Traits\ModuleTrait;
|
||||||
use Da\User\Validator\AjaxRequestModelValidator;
|
use Da\User\Validator\AjaxRequestModelValidator;
|
||||||
@ -60,6 +62,12 @@ class RecoveryController extends Controller
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays / handles user password recovery request.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*/
|
||||||
public function actionRequest()
|
public function actionRequest()
|
||||||
{
|
{
|
||||||
if (!$this->getModule()->allowPasswordRecovery) {
|
if (!$this->getModule()->allowPasswordRecovery) {
|
||||||
@ -93,4 +101,63 @@ class RecoveryController extends Controller
|
|||||||
|
|
||||||
return $this->render('request', ['model' => $form,]);
|
return $this->render('request', ['model' => $form,]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays / handles user password reset.
|
||||||
|
*
|
||||||
|
* @param $id
|
||||||
|
* @param $code
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
* @throws NotFoundHttpException
|
||||||
|
*/
|
||||||
|
public function actionReset($id, $code)
|
||||||
|
{
|
||||||
|
if (!$this->getModule()->allowPasswordRecovery) {
|
||||||
|
throw new NotFoundHttpException();
|
||||||
|
}
|
||||||
|
/** @var Token $token */
|
||||||
|
$token = $this->tokenQuery->whereIsRecoveryType($id, $code)->one();
|
||||||
|
/** @var ResetPasswordEvent $event */
|
||||||
|
$event = $this->make(ResetPasswordEvent::class, [$token]);
|
||||||
|
|
||||||
|
$this->trigger(ResetPasswordEvent::EVENT_BEFORE_TOKEN_VALIDATE, $event);
|
||||||
|
|
||||||
|
if ($token === null || $token->getIsExpired() || $token->user === null) {
|
||||||
|
Yii::$app->session->setFlash(
|
||||||
|
'danger',
|
||||||
|
Yii::t('user', 'Recovery link is invalid or expired. Please try requesting a new one.')
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->render(
|
||||||
|
'message',
|
||||||
|
[
|
||||||
|
'title' => Yii::t('user', 'Invalid or expired link'),
|
||||||
|
'module' => $this->getModule(),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var RecoveryForm $form */
|
||||||
|
$form = $this->make(RecoveryForm::class, ['scenario' => RecoveryForm::SCENARIO_RESET]);
|
||||||
|
$event = $event->updateForm($form);
|
||||||
|
|
||||||
|
$this->make(AjaxRequestModelValidator::class, [$form])->validate();
|
||||||
|
|
||||||
|
if ($form->load(Yii::$app->getRequest()->post())) {
|
||||||
|
if ($this->make(ResetPasswordService::class, [$form->password, $token->user])->run()) {
|
||||||
|
$this->trigger(ResetPasswordEvent::EVENT_AFTER_RESET, $event);
|
||||||
|
|
||||||
|
return $this->render(
|
||||||
|
'message',
|
||||||
|
[
|
||||||
|
'title' => Yii::t('user', 'Password has been changed'),
|
||||||
|
'module' => $this->getModule(),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->render('reset', ['model' => $form,]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,7 +16,7 @@ class ResetPasswordEvent extends Event
|
|||||||
protected $form;
|
protected $form;
|
||||||
protected $token;
|
protected $token;
|
||||||
|
|
||||||
public function __construct(RecoveryForm $form, Token $token, array $config = [])
|
public function __construct(Token $token, RecoveryForm $form, array $config = [])
|
||||||
{
|
{
|
||||||
$this->form = $form;
|
$this->form = $form;
|
||||||
$this->token = $token;
|
$this->token = $token;
|
||||||
@ -33,4 +33,9 @@ class ResetPasswordEvent extends Event
|
|||||||
{
|
{
|
||||||
return $this->token;
|
return $this->token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function updateForm(RecoveryForm $form)
|
||||||
|
{
|
||||||
|
return new static($this->getToken(), $form);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Da\User\Query;
|
namespace Da\User\Query;
|
||||||
|
|
||||||
|
use Da\User\Model\Token;
|
||||||
use yii\db\ActiveQuery;
|
use yii\db\ActiveQuery;
|
||||||
|
|
||||||
class TokenQuery extends ActiveQuery
|
class TokenQuery extends ActiveQuery
|
||||||
{
|
{
|
||||||
|
public function whereIsRecoveryType($userId, $code)
|
||||||
|
{
|
||||||
|
return $this->andWhere(['user_id' => $userId, 'code' => $code, 'type' => Token::TYPE_RECOVERY]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,8 @@ class ResetPasswordService implements ServiceInterface
|
|||||||
|
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
return (bool)$this->model->updateAttributes(
|
|
||||||
|
return $this->model && (bool)$this->model->updateAttributes(
|
||||||
[
|
[
|
||||||
'password_hash' => $this->securityHelper->generatePasswordHash($this->password)
|
'password_hash' => $this->securityHelper->generatePasswordHash($this->password)
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user