Remove last bits of deprecated parts

This commit is contained in:
Jannis Portmann 2021-09-14 14:26:07 +02:00
parent 4ced33c694
commit d1ba54c9f9

View file

@ -4,8 +4,8 @@ namespace App\Controller;
use App\Entity\User; use App\Entity\User;
use App\Form\RegistrationFormType; use App\Form\RegistrationFormType;
use App\Security\EmailVerifier;
use App\Security\AppAuthenticator; use App\Security\AppAuthenticator;
use App\Security\EmailVerifier;
use App\Repository\UserRepository; use App\Repository\UserRepository;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -14,7 +14,6 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mime\Address; use Symfony\Component\Mime\Address;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
class RegistrationController extends AbstractController class RegistrationController extends AbstractController
@ -27,7 +26,7 @@ class RegistrationController extends AbstractController
} }
#[Route('/register', name: 'app_register')] #[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $passwordHasher, GuardAuthenticatorHandler $guardHandler, AppAuthenticator $authenticator): Response public function register(Request $request, UserPasswordHasherInterface $passwordEncoder): Response
{ {
$user = new User(); $user = new User();
$form = $this->createForm(RegistrationFormType::class, $user); $form = $this->createForm(RegistrationFormType::class, $user);
@ -36,7 +35,7 @@ class RegistrationController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password // encode the plain password
$user->setPassword( $user->setPassword(
$passwordHasher->encodePassword( $passwordEncoder->encodePassword(
$user, $user,
$form->get('plainPassword')->getData() $form->get('plainPassword')->getData()
) )
@ -54,14 +53,8 @@ class RegistrationController extends AbstractController
->subject('Please Confirm your Email') ->subject('Please Confirm your Email')
->htmlTemplate('registration/confirmation_email.html.twig') ->htmlTemplate('registration/confirmation_email.html.twig')
); );
// do anything else you need here, like send an email
return $guardHandler->authenticateUserAndHandleSuccess( return $this->redirectToRoute('security');
$user,
$request,
$authenticator,
'main' // firewall name in security.yaml
);
} }
return $this->render('registration/register.html.twig', [ return $this->render('registration/register.html.twig', [
@ -70,23 +63,13 @@ class RegistrationController extends AbstractController
} }
#[Route('/verify/email', name: 'app_verify_email')] #[Route('/verify/email', name: 'app_verify_email')]
public function verifyUserEmail(Request $request, UserRepository $userRepository): Response public function verifyUserEmail(Request $request): Response
{ {
$id = $request->get('id'); $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
if (null === $id) {
return $this->redirectToRoute('app_register');
}
$user = $userRepository->find($id);
if (null === $user) {
return $this->redirectToRoute('app_register');
}
// validate email confirmation link, sets User::isVerified=true and persists // validate email confirmation link, sets User::isVerified=true and persists
try { try {
$this->emailVerifier->handleEmailConfirmation($request, $user); $this->emailVerifier->handleEmailConfirmation($request, $this->getUser());
} catch (VerifyEmailExceptionInterface $exception) { } catch (VerifyEmailExceptionInterface $exception) {
$this->addFlash('verify_email_error', $exception->getReason()); $this->addFlash('verify_email_error', $exception->getReason());
@ -96,6 +79,6 @@ class RegistrationController extends AbstractController
// @TODO Change the redirect on success and handle or remove the flash message in your templates // @TODO Change the redirect on success and handle or remove the flash message in your templates
$this->addFlash('success', 'Your email address has been verified.'); $this->addFlash('success', 'Your email address has been verified.');
return $this->redirectToRoute('user_page'); return $this->redirectToRoute('app_register');
} }
} }