Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)
This commit is contained in:
@@ -28,29 +28,21 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* @Route(path="/register")
|
||||
*/
|
||||
class SelfRegistrationController extends AbstractController
|
||||
#[Route(path: '/register')]
|
||||
final class SelfRegistrationController extends AbstractController
|
||||
{
|
||||
private $eventDispatcher;
|
||||
private $userService;
|
||||
private $tokenStorage;
|
||||
private $configuration;
|
||||
|
||||
public function __construct(EventDispatcherInterface $eventDispatcher, UserService $userService, TokenStorageInterface $tokenStorage, SystemConfiguration $configuration)
|
||||
{
|
||||
$this->eventDispatcher = $eventDispatcher;
|
||||
$this->userService = $userService;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->configuration = $configuration;
|
||||
public function __construct(
|
||||
private EventDispatcherInterface $eventDispatcher,
|
||||
private UserService $userService,
|
||||
private TokenStorageInterface $tokenStorage,
|
||||
private SystemConfiguration $configuration
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route(path="/", name="fos_user_registration_register", methods={"GET", "POST"})
|
||||
*/
|
||||
public function registerAction(Request $request): Response
|
||||
#[Route(path: '/', name: 'registration_register', methods: ['GET', 'POST'])]
|
||||
public function registerAction(Request $request, TranslatorInterface $translator): Response
|
||||
{
|
||||
if (!$this->configuration->isSelfRegistrationActive()) {
|
||||
throw $this->createNotFoundException();
|
||||
@@ -68,18 +60,18 @@ class SelfRegistrationController extends AbstractController
|
||||
$user->setEnabled(false);
|
||||
$user->setConfirmationToken($this->userService->generateSecurityToken());
|
||||
|
||||
$mail = $this->generateConfirmationEmail($user);
|
||||
$mail = $this->generateConfirmationEmail($user, $translator);
|
||||
$event = new EmailSelfRegistrationEvent($user, $mail);
|
||||
$this->eventDispatcher->dispatch($event);
|
||||
|
||||
// this will finally send the email
|
||||
$this->eventDispatcher->dispatch(new EmailEvent($event->getEmail()));
|
||||
|
||||
$request->getSession()->set('fos_user_send_confirmation_email/email', $user->getEmail());
|
||||
$request->getSession()->set('confirmation_email_address', $user->getEmail());
|
||||
|
||||
$this->userService->saveNewUser($user);
|
||||
|
||||
return $this->redirectToRoute('fos_user_registration_check_email');
|
||||
return $this->redirectToRoute('user_registration_check_email');
|
||||
}
|
||||
|
||||
return $this->render('security/self-registration/register.html.twig', [
|
||||
@@ -89,26 +81,25 @@ class SelfRegistrationController extends AbstractController
|
||||
|
||||
/**
|
||||
* Tell the user to check their email provider.
|
||||
*
|
||||
* @Route(path="/check-email", name="fos_user_registration_check_email", methods={"GET"})
|
||||
*/
|
||||
#[Route(path: '/check-email', name: 'user_registration_check_email', methods: ['GET'])]
|
||||
public function checkEmailAction(Request $request): Response
|
||||
{
|
||||
if (!$this->configuration->isSelfRegistrationActive()) {
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
|
||||
$email = $request->getSession()->get('fos_user_send_confirmation_email/email');
|
||||
$email = $request->getSession()->get('confirmation_email_address');
|
||||
|
||||
if (empty($email)) {
|
||||
return $this->redirectToRoute('fos_user_registration_register');
|
||||
return $this->redirectToRoute('registration_register');
|
||||
}
|
||||
|
||||
$request->getSession()->remove('fos_user_send_confirmation_email/email');
|
||||
$request->getSession()->remove('confirmation_email_address');
|
||||
$user = $this->userService->findUserByEmail($email);
|
||||
|
||||
if (null === $user) {
|
||||
return $this->redirectToRoute('fos_user_security_login');
|
||||
return $this->redirectToRoute('login');
|
||||
}
|
||||
|
||||
return $this->render('security/self-registration/check_email.html.twig', [
|
||||
@@ -118,9 +109,8 @@ class SelfRegistrationController extends AbstractController
|
||||
|
||||
/**
|
||||
* Receive the confirmation token from user email provider, login the user.
|
||||
*
|
||||
* @Route(path="/confirm/{token}", name="fos_user_registration_confirm", methods={"GET"})
|
||||
*/
|
||||
#[Route(path: '/confirm/{token}', name: 'registration_confirm', methods: ['GET'])]
|
||||
public function confirmAction(LoginManager $loginManager, ?string $token): Response
|
||||
{
|
||||
if (!$this->configuration->isSelfRegistrationActive()) {
|
||||
@@ -130,7 +120,7 @@ class SelfRegistrationController extends AbstractController
|
||||
$user = $this->userService->findUserByConfirmationToken($token);
|
||||
|
||||
if (null === $user) {
|
||||
return $this->redirectToRoute('fos_user_security_login');
|
||||
return $this->redirectToRoute('login');
|
||||
}
|
||||
|
||||
$user->setConfirmationToken(null);
|
||||
@@ -138,7 +128,7 @@ class SelfRegistrationController extends AbstractController
|
||||
|
||||
$this->userService->updateUser($user);
|
||||
|
||||
$response = $this->redirectToRoute('fos_user_registration_confirmed');
|
||||
$response = $this->redirectToRoute('registration_confirmed');
|
||||
$loginManager->logInUser($user, $response);
|
||||
|
||||
return $response;
|
||||
@@ -146,22 +136,16 @@ class SelfRegistrationController extends AbstractController
|
||||
|
||||
/**
|
||||
* Tell the user his account is now confirmed.
|
||||
*
|
||||
* @Route(path="/confirmed", name="fos_user_registration_confirmed", methods={"GET"})
|
||||
*/
|
||||
#[Route(path: '/confirmed', name: 'registration_confirmed', methods: ['GET'])]
|
||||
public function confirmedAction(Request $request): Response
|
||||
{
|
||||
if (!$this->configuration->isSelfRegistrationActive()) {
|
||||
throw $this->createNotFoundException();
|
||||
}
|
||||
|
||||
$user = $this->getUser();
|
||||
if ($user === null) {
|
||||
throw $this->createAccessDeniedException('This user does not have access to this section.');
|
||||
}
|
||||
|
||||
return $this->render('security/self-registration/confirmed.html.twig', [
|
||||
'user' => $user,
|
||||
'user' => $this->getUser(),
|
||||
'targetUrl' => $this->getTargetUrlFromSession($request->getSession()),
|
||||
]);
|
||||
}
|
||||
@@ -170,13 +154,13 @@ class SelfRegistrationController extends AbstractController
|
||||
{
|
||||
$options = ['validation_groups' => ['Registration', 'Default']];
|
||||
|
||||
return $this->createFormBuilder()->create('fos_user_registration_form', SelfRegistrationForm::class, $options)->getForm();
|
||||
return $this->createFormBuilder()->create('user_registration_form', SelfRegistrationForm::class, $options)->getForm();
|
||||
}
|
||||
|
||||
private function getTargetUrlFromSession(SessionInterface $session): ?string
|
||||
{
|
||||
$token = $this->tokenStorage->getToken();
|
||||
if (!method_exists($token, 'getProviderKey')) {
|
||||
if ($token === null || !method_exists($token, 'getProviderKey')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -189,17 +173,17 @@ class SelfRegistrationController extends AbstractController
|
||||
return null;
|
||||
}
|
||||
|
||||
private function generateConfirmationEmail(User $user): Email
|
||||
private function generateConfirmationEmail(User $user, TranslatorInterface $translator): Email
|
||||
{
|
||||
$username = $user->getDisplayName();
|
||||
$language = $user->getLanguage();
|
||||
|
||||
$url = $this->generateUrl('fos_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
$url = $this->generateUrl('registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
|
||||
return (new TemplatedEmail())
|
||||
->to(new Address($user->getEmail()))
|
||||
->subject(
|
||||
$this->getTranslator()->trans('registration.subject', ['%username%' => $username], 'email', $language)
|
||||
$translator->trans('registration.subject', ['%username%' => $username], 'email', $language)
|
||||
)
|
||||
->htmlTemplate('emails/confirmation.html.twig')
|
||||
->context([
|
||||
|
||||
Reference in New Issue
Block a user