2021-07-29 18:26:14 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-07-29 18:26:14 +01:00
|
|
|
namespace App\Controller;
|
|
|
|
|
|
|
|
use App\Core\Controller;
|
2021-09-06 19:49:03 +01:00
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Form;
|
|
|
|
use function App\Core\I18n\_m;
|
2021-07-29 18:26:14 +01:00
|
|
|
use App\Entity\LocalUser;
|
2021-09-06 19:49:03 +01:00
|
|
|
use App\Security\EmailVerifier;
|
|
|
|
use App\Util\Exception\ClientException;
|
2021-07-29 18:26:14 +01:00
|
|
|
use App\Util\Exception\RedirectException;
|
2021-09-06 19:49:03 +01:00
|
|
|
use App\Util\Form\FormFields;
|
2021-07-29 18:26:14 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
2021-09-06 19:49:03 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
2021-07-29 18:26:14 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
|
|
|
use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
|
|
|
|
use SymfonyCasts\Bundle\ResetPassword\Exception\ResetPasswordExceptionInterface;
|
2021-09-06 23:47:28 +01:00
|
|
|
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordToken;
|
2021-07-29 18:26:14 +01:00
|
|
|
|
2021-08-03 11:14:11 +01:00
|
|
|
/**
|
|
|
|
* Send password reset emails to users
|
|
|
|
* TODO: As we don't have email services setup yet, this won't be tested right now
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2021-07-29 18:26:14 +01:00
|
|
|
class ResetPassword extends Controller
|
|
|
|
{
|
|
|
|
use ResetPasswordControllerTrait;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display & process form to request a password reset.
|
|
|
|
*/
|
|
|
|
public function requestPasswordReset(Request $request)
|
|
|
|
{
|
2021-09-06 19:49:03 +01:00
|
|
|
$form = Form::create([
|
2021-10-10 09:26:18 +01:00
|
|
|
['email', EmailType::class, ['label' => _m('Email'), 'constraints' => [new NotBlank(['message' => _m('Please enter an email')])]]],
|
2021-07-29 18:26:14 +01:00
|
|
|
['password_reset_request', SubmitType::class, ['label' => _m('Submit request')]],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
return EmailVerifier::processSendingPasswordResetEmail($form->get('email')->getData(), $this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'_template' => 'reset_password/request.html.twig',
|
2021-09-06 19:49:03 +01:00
|
|
|
'password_reset_form' => $form->createView(),
|
2021-07-29 18:26:14 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Confirmation page after a user has requested a password reset.
|
|
|
|
*/
|
|
|
|
public function checkEmail()
|
|
|
|
{
|
|
|
|
// We prevent users from directly accessing this page
|
|
|
|
if (null === ($resetToken = $this->getTokenObjectFromSession())) {
|
|
|
|
throw new RedirectException('request_reset_password');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'_template' => 'reset_password/check_email.html.twig',
|
|
|
|
'resetToken' => $resetToken,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates and process the reset URL that the user clicked in their email.
|
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public function reset(Request $request, ?string $token = null)
|
2021-07-29 18:26:14 +01:00
|
|
|
{
|
|
|
|
if ($token) {
|
|
|
|
// We store the token in session and remove it from the URL, to avoid the URL being
|
|
|
|
// loaded in a browser and potentially leaking the token to 3rd party JavaScript.
|
|
|
|
$this->storeTokenInSession($token);
|
|
|
|
throw new RedirectException('reset_password');
|
|
|
|
}
|
|
|
|
|
|
|
|
$token = $this->getTokenFromSession();
|
|
|
|
if (null === $token) {
|
|
|
|
throw new ClientException(_m('No reset password token found in the URL or in the session'));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$user = EmailVerifier::validateTokenAndFetchUser($token);
|
|
|
|
} catch (ResetPasswordExceptionInterface $e) {
|
|
|
|
$this->addFlash('reset_password_error', _m('There was a problem validating your reset request - {reason}', ['reason' => $e->getReason()]));
|
|
|
|
throw new RedirectException('request_reset_password');
|
|
|
|
}
|
|
|
|
|
|
|
|
// The token is valid; allow the user to change their password.
|
2021-09-06 19:49:03 +01:00
|
|
|
$form = Form::create([
|
2021-08-03 11:14:11 +01:00
|
|
|
FormFields::repeated_password(),
|
2021-07-29 18:26:14 +01:00
|
|
|
['password_reset', SubmitType::class, ['label' => _m('Change password')]],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
// A password reset token should be used only once, remove it.
|
|
|
|
EmailVerifier::removeResetRequest($token);
|
|
|
|
|
|
|
|
$user->setPassword(LocalUser::hashPassword($form->get('password')->getData()));
|
|
|
|
DB::flush();
|
|
|
|
|
|
|
|
// The session is cleaned up after the password has been changed.
|
|
|
|
$this->cleanSessionAfterReset();
|
|
|
|
|
|
|
|
throw new RedirectException('main_all');
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'_template' => 'reset_password/reset.html.twig',
|
|
|
|
'resetForm' => $form->createView(),
|
|
|
|
];
|
|
|
|
}
|
2021-09-06 23:47:28 +01:00
|
|
|
|
|
|
|
public function setInSession(ResetPasswordToken $reset_token)
|
|
|
|
{
|
|
|
|
$this->setTokenObjectInSession($reset_token);
|
|
|
|
}
|
2021-07-29 18:26:14 +01:00
|
|
|
}
|