|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Doctrine\ORM\OptimisticLockException; |
|
9
|
|
|
use Doctrine\ORM\ORMException; |
|
10
|
|
|
use Gbere\SimpleAuth\Entity\User; |
|
11
|
|
|
use Gbere\SimpleAuth\Security\LoginFormAuthenticator; |
|
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
15
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
16
|
|
|
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; |
|
17
|
|
|
|
|
18
|
|
|
final class ConfirmRegistrationController extends AbstractController |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @Route("/register/confirmation/{token}", name="gbere_auth_confirm_registration") |
|
22
|
|
|
* |
|
23
|
|
|
* @throws ORMException |
|
24
|
|
|
* @throws OptimisticLockException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __invoke( |
|
27
|
|
|
string $token, |
|
28
|
|
|
Request $request, |
|
29
|
|
|
GuardAuthenticatorHandler $guardHandler, |
|
30
|
|
|
LoginFormAuthenticator $authenticator |
|
31
|
|
|
): Response { |
|
32
|
|
|
/** @var EntityManager $manager */ |
|
33
|
|
|
$manager = $this->getDoctrine()->getManager(); |
|
34
|
|
|
/** @var null|User $user */ |
|
35
|
|
|
$user = $manager->getRepository(User::class)->findOneBy(['confirmationToken' => $token]); |
|
36
|
|
|
if (null === $user) { |
|
37
|
|
|
$this->addFlash('danger', 'The token is invalid'); |
|
38
|
|
|
} |
|
39
|
|
|
$user->hasEnabled(true); |
|
40
|
|
|
$user->setConfirmationToken(null); |
|
41
|
|
|
$manager->persist($user); |
|
42
|
|
|
$manager->flush(); |
|
43
|
|
|
$this->addFlash('success', 'The user has been successfully activated'); |
|
44
|
|
|
|
|
45
|
|
|
// TODO: Auto login after validate? |
|
46
|
|
|
// return $guardHandler->authenticateUserAndHandleSuccess($user, $request, $authenticator, 'gbere_main_firewall'); |
|
47
|
|
|
|
|
48
|
|
|
return $this->redirectToRoute('gbere_auth_login'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|