|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller\Auth; |
|
6
|
|
|
|
|
7
|
|
|
use App\Controller\BaseController; |
|
8
|
|
|
use App\Entity\Profile; |
|
9
|
|
|
use App\Entity\User; |
|
10
|
|
|
use App\Form\Type\RegistrationFormType; |
|
11
|
|
|
use App\Message\SendEmailConfirmationLink; |
|
12
|
|
|
use App\Repository\SettingsRepository; |
|
13
|
|
|
use App\Security\RegistrationFormAuthenticator; |
|
14
|
|
|
use App\Service\Admin\UserService; |
|
15
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
19
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
20
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
21
|
|
|
use Symfony\Component\Security\Core\Security; |
|
22
|
|
|
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface; |
|
23
|
|
|
|
|
24
|
|
|
final class RegisterController extends BaseController |
|
25
|
|
|
{ |
|
26
|
|
|
private MessageBusInterface $messageBus; |
|
27
|
|
|
private RegistrationFormAuthenticator $authenticator; |
|
28
|
|
|
private Security $security; |
|
29
|
|
|
private UserAuthenticatorInterface $userAuthenticator; |
|
30
|
|
|
private UserService $service; |
|
31
|
|
|
private array $settings; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( |
|
34
|
|
|
ManagerRegistry $doctrine, |
|
35
|
|
|
MessageBusInterface $messageBus, |
|
36
|
|
|
RegistrationFormAuthenticator $authenticator, |
|
37
|
|
|
RequestStack $requestStack, |
|
38
|
|
|
Security $security, |
|
39
|
|
|
SettingsRepository $settingsRepository, |
|
40
|
|
|
UserAuthenticatorInterface $userAuthenticator, |
|
41
|
|
|
UserService $service |
|
42
|
|
|
) { |
|
43
|
|
|
parent::__construct($settingsRepository, $doctrine); |
|
44
|
|
|
$this->authenticator = $authenticator; |
|
45
|
|
|
$this->messageBus = $messageBus; |
|
46
|
|
|
$this->security = $security; |
|
47
|
|
|
$this->service = $service; |
|
48
|
|
|
$this->settings = $this->site($requestStack->getCurrentRequest()); |
|
|
|
|
|
|
49
|
|
|
$this->userAuthenticator = $userAuthenticator; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
#[Route('/register', name: 'register')] |
|
53
|
|
|
public function register(Request $request): ?Response |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->security->isGranted('ROLE_USER')) { |
|
56
|
|
|
return $this->redirectToRoute('user_property'); |
|
57
|
|
|
} elseif ('1' !== $this->settings['anyone_can_register']) { |
|
58
|
|
|
$this->addFlash('danger', 'message.registration_suspended'); |
|
59
|
|
|
|
|
60
|
|
|
return $this->redirectToRoute('property'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$user = new User(); |
|
64
|
|
|
$form = $this->createForm(RegistrationFormType::class, $user); |
|
65
|
|
|
$form->handleRequest($request); |
|
66
|
|
|
|
|
67
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
68
|
|
|
$user->setProfile(new Profile()); |
|
69
|
|
|
$this->service->create($user); |
|
70
|
|
|
$this->messageBus->dispatch(new SendEmailConfirmationLink($user)); |
|
71
|
|
|
|
|
72
|
|
|
return $this->authenticate($user, $request); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->render('auth/register.html.twig', [ |
|
76
|
|
|
'registrationForm' => $form->createView(), |
|
77
|
|
|
'site' => $this->settings, |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
private function authenticate(User $user, Request $request): ?Response |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->userAuthenticator->authenticateUser( |
|
84
|
|
|
$user, |
|
85
|
|
|
$this->authenticator, |
|
86
|
|
|
$request |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|