vendor/sylius/sylius/src/Sylius/Bundle/UiBundle/Controller/SecurityController.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\UiBundle\Controller;
  12. use Sylius\Bundle\UiBundle\Form\Type\SecurityLoginType;
  13. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  14. use Symfony\Component\Form\FormFactoryInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  20. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  21. use Twig\Environment;
  22. final class SecurityController
  23. {
  24.     private AuthenticationUtils $authenticationUtils;
  25.     private FormFactoryInterface $formFactory;
  26.     /** @var EngineInterface|Environment */
  27.     private $templatingEngine;
  28.     private AuthorizationCheckerInterface $authorizationChecker;
  29.     private RouterInterface $router;
  30.     /**
  31.      * @param EngineInterface|Environment $templatingEngine
  32.      */
  33.     public function __construct(
  34.         AuthenticationUtils $authenticationUtils,
  35.         FormFactoryInterface $formFactory,
  36.         object $templatingEngine,
  37.         AuthorizationCheckerInterface $authorizationChecker,
  38.         RouterInterface $router
  39.     ) {
  40.         $this->authenticationUtils $authenticationUtils;
  41.         $this->formFactory $formFactory;
  42.         $this->templatingEngine $templatingEngine;
  43.         $this->authorizationChecker $authorizationChecker;
  44.         $this->router $router;
  45.     }
  46.     public function loginAction(Request $request): Response
  47.     {
  48.         $alreadyLoggedInRedirectRoute $request->attributes->get('_sylius')['logged_in_route'] ?? null;
  49.         if ($alreadyLoggedInRedirectRoute && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  50.             return new RedirectResponse($this->router->generate($alreadyLoggedInRedirectRoute));
  51.         }
  52.         $lastError $this->authenticationUtils->getLastAuthenticationError();
  53.         $lastUsername $this->authenticationUtils->getLastUsername();
  54.         $options $request->attributes->get('_sylius');
  55.         $template $options['template'] ?? '@SyliusUi/Security/login.html.twig';
  56.         $formType $options['form'] ?? SecurityLoginType::class;
  57.         $form $this->formFactory->createNamed(''$formType);
  58.         return new Response($this->templatingEngine->render($template, [
  59.             'form' => $form->createView(),
  60.             'last_username' => $lastUsername,
  61.             'last_error' => $lastError,
  62.         ]));
  63.     }
  64.     public function checkAction(Request $request): void
  65.     {
  66.         throw new \RuntimeException('You must configure the check path to be handled by the firewall.');
  67.     }
  68.     public function logoutAction(Request $request): void
  69.     {
  70.         throw new \RuntimeException('You must configure the logout path to be handled by the firewall.');
  71.     }
  72. }