You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3 KiB

7 years ago
<?php
namespace frontend\controllers\auth;
use core\services\auth\PasswordResetService;
3 years ago
use DomainException;
7 years ago
use Yii;
3 years ago
use yii\base\Exception;
7 years ago
use yii\web\BadRequestHttpException;
use core\forms\auth\PasswordResetRequestForm;
use core\forms\auth\ResetPasswordForm;
use yii\web\Controller;
3 years ago
use yii\web\Response;
7 years ago
class ResetController extends Controller
7 years ago
{
public $layout = 'auth';
3 years ago
private PasswordResetService $service;
7 years ago
public function __construct($id, $module, PasswordResetService $service, $config = [])
{
parent::__construct($id, $module, $config);
3 years ago
$this->service = $service;
7 years ago
}
/**
3 years ago
* @return string|Response
* @throws Exception
7 years ago
*/
3 years ago
public function actionRequest(): string|Response
7 years ago
{
$form = new PasswordResetRequestForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->request($form);
7 years ago
Yii::$app->session->setFlash('success', Yii::t('auth', 'Check your email for further instructions.'));
return $this->goHome();
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
}
return $this->render('request', [
'model' => $form,
]);
}
/**
* @param $token
3 years ago
* @return string|Response
* @throws BadRequestHttpException|Exception
7 years ago
*/
3 years ago
public function actionConfirm($token): string|Response
7 years ago
{
try {
3 years ago
$this->service->validateToken($token);
} catch (DomainException $e) {
7 years ago
throw new BadRequestHttpException($e->getMessage());
}
$form = new ResetPasswordForm();
if ($form->load(Yii::$app->request->post()) && $form->validate()) {
try {
3 years ago
$this->service->reset($token, $form);
7 years ago
Yii::$app->session->setFlash('success', Yii::t('auth', 'New password saved.'));
3 years ago
} catch (DomainException $e) {
7 years ago
Yii::$app->errorHandler->logException($e);
Yii::$app->session->setFlash('error', $e->getMessage());
}
return $this->goHome();
}
return $this->render('confirm', [
'model' => $form,
]);
}
}