|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gbere\SimpleAuth\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Gbere\SimpleAuth\Form\RegisterType; |
|
9
|
|
|
use Gbere\SimpleAuth\Model\UserInterface; |
|
10
|
|
|
use Gbere\SimpleAuth\Repository\UserRepository; |
|
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\Contracts\Translation\TranslatorInterface; |
|
18
|
|
|
|
|
19
|
|
|
final class RegisterController extends AbstractController |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @Route("/register", name="simple_auth_register") |
|
23
|
|
|
* |
|
24
|
|
|
* @throws TransportExceptionInterface |
|
25
|
|
|
* @throws Exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __invoke(Request $request, UserRepository $userRepository, Mailer $mailer, TranslatorInterface $translator): Response |
|
28
|
|
|
{ |
|
29
|
|
|
$user = $userRepository->createUser(); |
|
30
|
|
|
$form = $this->createForm(RegisterType::class, $user); |
|
31
|
|
|
$form->handleRequest($request); |
|
32
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
33
|
|
|
/** @var UserInterface $user */ |
|
34
|
|
|
$user = $form->getData(); |
|
35
|
|
|
$user->setPassword($userRepository->encodePassword($user->getPassword() ?? '')); |
|
36
|
|
|
if ((bool) $this->getParameter('simple_auth_confirm_registration')) { |
|
37
|
|
|
$user->generateToken(); |
|
38
|
|
|
$mailer->sendConfirmRegistrationMessage($user); |
|
39
|
|
|
$this->addFlash('info', $translator->trans('fash.confirm_registration', ['%email%' => $user->getEmail()], 'SimpleAuthBundle')); |
|
40
|
|
|
} else { |
|
41
|
|
|
$user->hasEnabled(true); |
|
42
|
|
|
} |
|
43
|
|
|
$userRepository->persistAndFlush($user); |
|
44
|
|
|
|
|
45
|
|
|
return $this->redirectToRoute('simple_auth_login'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->render('@GbereSimpleAuth/frontend/register.html.twig', [ |
|
49
|
|
|
'form' => $form->createView(), |
|
50
|
|
|
]); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|