src/Controller/Security/SecurityController.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  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 Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use DateTimeImmutable;
  12. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  13. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  14. use App\Service\SwitchUserTokenService;
  15. class SecurityController extends AbstractController
  16. {
  17.     public function __construct(private SwitchUserTokenService $switchUserTokenService)
  18.     {
  19.         $this->switchUserTokenService $switchUserTokenService;
  20.     }
  21.     
  22.     #[Route(path'/login'name'app_login')]
  23.     public function login(AuthenticationUtils $authenticationUtils): Response
  24.     {
  25.         if ($this->getUser()) {
  26.             return $this->redirectToRoute('app_home');
  27.         }
  28.         // get the login error if there is one
  29.         $error $authenticationUtils->getLastAuthenticationError();
  30.         // last username entered by the user
  31.         $lastUsername $authenticationUtils->getLastUsername();
  32.         return $this->render('authentication/sign-in.html.twig', ['last_username' => $lastUsername'error' => $error]);
  33.     }
  34.     #[Route(path'/logout'name'app_logout')]
  35.     public function logout(): void
  36.     {
  37.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  38.     }
  39.     #[Route('/switch-user'name'app_switch_user')]
  40.     public function switchUser(Request $requestEventDispatcherInterface $eventDispatcherSession $session)
  41.     {
  42.         $token $request->query->get('token');
  43.         $userId $request->query->get('user_id');
  44.         $tokenObj $this->switchUserTokenService->fetchToken($token$userId);
  45.         if ($tokenObj and $tokenObj->getExpiredAt() > new DateTimeImmutable()) {
  46.             $authToken = new UsernamePasswordToken($tokenObj->getUser(), "main"$tokenObj->getUser()->getRoles());
  47.             $this->container->get("security.token_storage")->setToken($authToken);
  48.             $event = new InteractiveLoginEvent($request$authToken);
  49.             $eventDispatcher->dispatch($event"security.interactive_login");
  50.             $session->set('_switch_token'$token);
  51.         
  52.             return $this->redirectToRoute('app_home');
  53.         }
  54.         throw new BadRequestException('Jeton invalide !');
  55.     }
  56. }