src/Controller/Front/SecurityController.php line 64

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front;
  4. use App\Entity\Activite;
  5. use App\Entity\Client;
  6. use App\Entity\Utilisateur;
  7. use App\Event\ForgottenPasswordEvent;
  8. use App\Form\ForgottenPasswordType;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Exception;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Bundle\SecurityBundle\Security;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. class SecurityController extends AbstractController
  19. {
  20.     #[Route('/login'name'login'priority10)]
  21.     public function login(AuthenticationUtils $authenticationUtils): Response
  22.     {
  23.         $error $authenticationUtils->getLastAuthenticationError();
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         return $this->render('login/index.html.twig', [
  26.             'last_username' => $lastUsername,
  27.             'error' => $error,
  28.             'target_path' => '/admin',
  29.         ]);
  30.     }
  31.     #[Route('/login/redirect'name'login_redirect'priority200)]
  32.     public function loginRedirect(EntityManagerInterface $emSecurity $security): Response
  33.     {
  34.         $route 'client_activites';
  35.         /** @var Utilisateur $utilisateur */
  36.         $utilisateur $this->getUser();
  37.         $client $utilisateur->getClient();
  38.         if (null !== $client) {
  39.             $activiteCount $em->getRepository(Activite::class)->count(['client' => $client]);
  40.             if (=== $activiteCount) {
  41.                 /** @var Activite $activite */
  42.                 $activite $em->getRepository(Activite::class)->findOneBy(['client' => $client]);
  43.                 return $this->redirectToRoute('client_activite_choix_annee', ['activiteSlug' => $activite->getSlug()]);
  44.             }
  45.         }
  46.         if ($security->isGranted('ROLE_ADMIN')) {
  47.             $route 'admin';
  48.         }
  49.         return $this->redirectToRoute($route);
  50.     }
  51.     #[Route('/mot-de-passe-oublie'name'forgotten_password'priority10)]
  52.     public function forgottenPassword(Request $requestEntityManagerInterface $emEventDispatcherInterface $eventDispatcher): Response
  53.     {
  54.         $form $this->createForm(ForgottenPasswordType::class);
  55.         $form->handleRequest($request);
  56.         if ($form->isSubmitted() && $form->isValid()) {
  57.             /** @var string $email */
  58.             $email $form->get('email')->getData();
  59.             $client $em->getRepository(Client::class)->findOneByEmail($email);
  60.             if (null === $client) {
  61.                 $this->addFlash('danger''Aucun client avec cet email.');
  62.                 return $this->redirectToRoute('forgotten_password');
  63.             }
  64.             if (null === $client->getPasswordToken()) {
  65.                 $client->generatePasswordToken();
  66.             }
  67.             $this->addFlash('success''Un email contenant un lien pour modifier votre mot de passe vous a été envoyé.');
  68.             $event = new ForgottenPasswordEvent($client);
  69.             $eventDispatcher->dispatch($eventForgottenPasswordEvent::NAME);
  70.         }
  71.         return $this->render('login/forgotten_password.html.twig', [
  72.             'form' => $form,
  73.         ]);
  74.     }
  75.     #[Route('/logout'name'logout'priority10)]
  76.     public function logout(): never
  77.     {
  78.         // controller can be blank: it will never be called!
  79.         throw new Exception('Don\'t forget to activate logout in security.yaml');
  80.     }
  81. }