src/Controller/SecurityController.php line 49

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ChangePasswordFormType;
  5. use App\Form\RegistrationFormType;
  6. use App\Repository\UserRepository;
  7. use App\Security\AppEmailAuthenticator;
  8. use App\Service\BrevoService;
  9. use DateTimeImmutable;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  17. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  18. use SymfonyCasts\Bundle\ResetPassword\Controller\ResetPasswordControllerTrait;
  19. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  20. class SecurityController extends AbstractController
  21. {
  22.     private $resetPasswordHelper;
  23.     use ResetPasswordControllerTrait;
  24.     public function __construct(ResetPasswordHelperInterface $resetPasswordHelperEntityManagerInterface $entityManager)
  25.     {
  26.         $this->resetPasswordHelper $resetPasswordHelper;
  27.         $this->entityManager $entityManager;
  28.     }
  29.     #[Route('/connexion'name'app_login')]
  30.     public function login(AuthenticationUtils $authenticationUtils): Response
  31.     {
  32.         if ($this->getUser()) {
  33.             return $this->redirectToRoute('app_station_index');
  34.         }
  35.         $error $authenticationUtils->getLastAuthenticationError();
  36.         $lastUsername $authenticationUtils->getLastUsername();
  37.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  38.     }
  39.     #[Route('/inscription'name'app_register'methods: ['GET''POST'])]
  40.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppEmailAuthenticator $authenticatorEntityManagerInterface $entityManagerBrevoService $sibService): Response
  41.     {
  42.         if ($this->getUser()) {
  43.             return $this->redirectToRoute('app_station_index');
  44.         }
  45.         $user = new User();
  46.         $form $this->createForm(RegistrationFormType::class, $user);
  47.         $form->handleRequest($request);
  48.         if ($form->isSubmitted() && $form->isValid()) {
  49.             $user->setPassword(
  50.                 $userPasswordHasher->hashPassword(
  51.                     $user,
  52.                     $form->get('plainPassword')->getData()
  53.                 )
  54.             );
  55.             $entityManager->persist($user);
  56.             $entityManager->flush();
  57.             //TODO: Send Mail
  58.             if($request->request->get('newsletter')) {
  59.                 $sibService->saveContact($user->getEmail(), $user->getUsername(), true);
  60.             }
  61.             return $userAuthenticator->authenticateUser(
  62.                 $user,
  63.                 $authenticator,
  64.                 $request
  65.             );
  66.         }
  67.         return $this->render('security/register.html.twig', [
  68.             'registrationForm' => $form->createView(),
  69.         ]);
  70.     }
  71.     #[Route('/deconnexion'name'app_logout')]
  72.     public function logout(): void
  73.     {
  74.         throw new \LogicException('');
  75.     }
  76.     #[Route('/configuration'name'app_parameters')]
  77.     public function parameters(Request $requestUserPasswordHasherInterface $userPasswordHasherUserRepository $userRepository): Response
  78.     {
  79.         $form $this->createForm(ChangePasswordFormType::class);
  80.         $form->handleRequest($request);
  81.         $user $userRepository->find($this->getUser());
  82.         if ($form->isSubmitted() && $form->isValid()) {
  83.             // Encode(hash) the plain password, and set it.
  84.             $encodedPassword $userPasswordHasher->hashPassword(
  85.                 $user,
  86.                 $form->get('plainPassword')->getData()
  87.             );
  88.             $user->setPassword($encodedPassword);
  89.             $this->entityManager->flush();
  90.             // The session is cleaned up after the password has been changed.
  91.             $this->cleanSessionAfterReset();
  92.             return $this->redirectToRoute('app_parameters');
  93.         }
  94.         return $this->render('security/parameters.html.twig', [
  95.             'resetForm' => $form->createView(),
  96.         ]);
  97.     }
  98. }