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 Gbere\SimpleAuth\Repository\UserRepository; |
10
|
|
|
use Gbere\SimpleAuth\Security\LoginFormAuthenticator; |
11
|
|
|
use Gbere\SimpleAuth\Service\Mailer; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; |
18
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
19
|
|
|
|
20
|
|
|
final class ConfirmRegistrationController extends AbstractController |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @Route("/register/confirmation/{token}", name="simple_auth_confirm_registration") |
24
|
|
|
* |
25
|
|
|
* @throws ORMException |
26
|
|
|
* @throws OptimisticLockException |
27
|
|
|
* @throws TransportExceptionInterface |
28
|
|
|
*/ |
29
|
|
|
public function __invoke( |
30
|
|
|
string $token, |
31
|
|
|
Request $request, |
32
|
|
|
GuardAuthenticatorHandler $guardHandler, |
33
|
|
|
LoginFormAuthenticator $authenticator, |
34
|
|
|
Mailer $mailer, |
35
|
|
|
UserRepository $userRepository, |
36
|
|
|
TranslatorInterface $translator |
37
|
|
|
): Response { |
38
|
|
|
$user = $userRepository->findOneBy(['confirmationToken' => $token]); |
39
|
|
|
if (null === $user) { |
40
|
|
|
$this->addFlash('danger', $translator->trans('flash.invalid_token', [], 'SimpleAuthBundle')); |
41
|
|
|
|
42
|
|
|
return $this->redirectToRoute('simple_auth_login'); |
43
|
|
|
} |
44
|
|
|
$user->hasEnabled(true); |
45
|
|
|
$user->setConfirmationToken(null); |
46
|
|
|
$userRepository->persistAndFlush($user); |
47
|
|
|
$this->addFlash('info', $translator->trans('flash.user_enabled', [], 'SimpleAuthBundle')); |
48
|
|
|
$mailer->sendWelcomeMessage($user); |
49
|
|
|
|
50
|
|
|
// TODO: Auto login after validate? |
51
|
|
|
// return $guardHandler->authenticateUserAndHandleSuccess($user, $request, $authenticator, 'gbere_main_firewall'); |
52
|
|
|
|
53
|
|
|
return $this->redirectToRoute('simple_auth_login'); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|