src/Controller/SecurityController.php line 28

  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use App\Repository\UserRepository;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. class SecurityController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/login", name="app_login")
  14.      */
  15.     public function login(AuthenticationUtils $authenticationUtilsUserRepository $userRepositoryRequestStack $requestStackEntityManagerInterface $em): Response
  16.     {
  17.         $error $authenticationUtils->getLastAuthenticationError();
  18.         $userEmail $authenticationUtils->getLastUsername();
  19.         $session $requestStack->getSession();
  20.         if ($userEmail) {
  21.             $user $userRepository->findOneBy(['email' => $userEmail]);
  22.             if ($user) {
  23.                 try {
  24.                     $defaultLanguage $user->getDefaultLanguage();
  25.                     if ($defaultLanguage && $defaultLanguage->getAbbreviation()) {
  26.                         // Store the abbreviation (locale code) in lowercase for ICU compatibility
  27.                         $session->set('selected_language'strtolower($defaultLanguage->getAbbreviation()));
  28.                     }
  29.                 } catch (\Exception $e) {
  30.                     // Language was deleted, clear the reference using DQL to avoid loading the entity
  31.                     $em->createQuery('
  32.                         UPDATE App\Entity\User u
  33.                         SET u.defaultLanguage = NULL
  34.                         WHERE u.id = :userId
  35.                     ')
  36.                         ->setParameter('userId'$user->getId())
  37.                         ->execute();
  38.                 }
  39.             }
  40.         }
  41.         return $this->render('security/login.html.twig', [
  42.             'last_username' => $userEmail,
  43.             'error' => $error,
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/logout", name="app_logout")
  48.      */
  49.     public function logout()
  50.     {
  51.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  52.     }
  53. }