1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Controller; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\OptimisticLockException; |
8
|
|
|
use Doctrine\ORM\ORMException; |
9
|
|
|
use Exception; |
10
|
|
|
use Gbere\SimpleAuth\Repository\UserRepository; |
11
|
|
|
use Gbere\SimpleAuth\Service\Mailer; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
17
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
18
|
|
|
|
19
|
|
|
final class ResetPasswordController extends AbstractController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/password/reset/{token}", name="gbere_auth_password_reset") |
23
|
|
|
* |
24
|
|
|
* @throws Exception |
25
|
|
|
* @throws ORMException |
26
|
|
|
* @throws OptimisticLockException |
27
|
|
|
* @throws TransportExceptionInterface |
28
|
|
|
*/ |
29
|
|
|
public function __invoke(string $token, Request $request, UserRepository $userRepository, Mailer $mailer): Response |
30
|
|
|
{ |
31
|
|
|
$user = $userRepository->findOneBy(['confirmationToken' => $token]); |
32
|
|
|
if (null === $user) { |
33
|
|
|
$this->addFlash('warning', 'The token is invalid'); |
34
|
|
|
|
35
|
|
|
return $this->redirectToRoute('gbere_auth_login'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$form = $this->createFormBuilder()->add('plainPassword', PasswordType::class, [ |
39
|
|
|
'label' => 'New password', |
40
|
|
|
])->getForm(); |
41
|
|
|
$form->handleRequest($request); |
42
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
43
|
|
|
$user->setPassword($userRepository->encodePassword($form->get('plainPassword')->getData())); |
44
|
|
|
$user->hasEnabled(true); |
45
|
|
|
$user->setConfirmationToken(null); |
46
|
|
|
$userRepository->persistAndFlush($user); |
47
|
|
|
$this->addFlash('success', 'The password was updated'); |
48
|
|
|
$mailer->sendPasswordResetNotificationMessage($user); |
49
|
|
|
|
50
|
|
|
return $this->redirectToRoute('gbere_auth_login'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->render('@GbereSimpleAuth/frontend/reset-password.html.twig', ['form' => $form->createView()]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|