1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Controller; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Doctrine\ORM\OptimisticLockException; |
9
|
|
|
use Doctrine\ORM\ORMException; |
10
|
|
|
use Exception; |
11
|
|
|
use Gbere\SimpleAuth\Repository\UserRepository; |
12
|
|
|
use Gbere\SimpleAuth\Service\Mailer; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
14
|
|
|
use Symfony\Component\Form\Extension\Core\Type\EmailType; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
19
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
final class PasswordRequestController extends AbstractController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @Route("/password/request", name="simple_auth_password_request") |
25
|
|
|
* |
26
|
|
|
* @throws Exception |
27
|
|
|
* @throws ORMException |
28
|
|
|
* @throws OptimisticLockException |
29
|
|
|
* @throws TransportExceptionInterface |
30
|
|
|
*/ |
31
|
|
|
public function __invoke(Request $request, Mailer $mailer, UserRepository $userRepository, TranslatorInterface $translator): Response |
32
|
|
|
{ |
33
|
|
|
$builder = $this->createFormBuilder()->add('email', EmailType::class); |
34
|
|
|
$form = $builder->getForm(); |
35
|
|
|
$form->handleRequest($request); |
36
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
37
|
|
|
$email = $form->get('email')->getData(); |
38
|
|
|
$user = $userRepository->findOneBy(['email' => $email]); |
39
|
|
|
if (null !== $user) { |
40
|
|
|
$user->generateToken(); |
41
|
|
|
$user->setPasswordRequestAt(new DateTime()); |
42
|
|
|
$userRepository->persistAndFlush($user); |
43
|
|
|
$mailer->sendPasswordResetMessage($user); |
44
|
|
|
$this->addFlash('info', $translator->trans('flash.restore_password', ['email' => $email], 'SimpleAuthBundle')); |
45
|
|
|
|
46
|
|
|
return $this->redirectToRoute('simple_auth_login'); |
47
|
|
|
} |
48
|
|
|
$this->addFlash('warning', $translator->trans('flash.email_not_registered', ['email' => $email], 'SimpleAuthBundle')); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->render('@GbereSimpleAuth/frontend/password-request.html.twig', [ |
52
|
|
|
'form' => $form->createView(), |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|