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

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.     /** @var AuthenticationUtils */
  25.     private $authenticationUtils;
  26.     /** @var FormFactoryInterface */
  27.     private $formFactory;
  28.     /** @var EngineInterface|Environment */
  29.     private $templatingEngine;
  30.     /** @var AuthorizationCheckerInterface */
  31.     private $authorizationChecker;
  32.     /** @var RouterInterface */
  33.     private $router;
  34.     /**
  35.      * @param EngineInterface|Environment $templatingEngine
  36.      */
  37.     public function __construct(
  38.         AuthenticationUtils $authenticationUtils,
  39.         FormFactoryInterface $formFactory,
  40.         object $templatingEngine,
  41.         AuthorizationCheckerInterface $authorizationChecker,
  42.         RouterInterface $router
  43.     ) {
  44.         $this->authenticationUtils $authenticationUtils;
  45.         $this->formFactory $formFactory;
  46.         $this->templatingEngine $templatingEngine;
  47.         $this->authorizationChecker $authorizationChecker;
  48.         $this->router $router;
  49.     }
  50.     public function loginAction(Request $request): Response
  51.     {
  52.         $alreadyLoggedInRedirectRoute $request->attributes->get('_sylius')['logged_in_route'] ?? null;
  53.         if ($alreadyLoggedInRedirectRoute && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  54.             return new RedirectResponse($this->router->generate($alreadyLoggedInRedirectRoute));
  55.         }
  56.         $lastError $this->authenticationUtils->getLastAuthenticationError();
  57.         $lastUsername $this->authenticationUtils->getLastUsername();
  58.         $options $request->attributes->get('_sylius');
  59.         $template $options['template'] ?? '@SyliusUi/Security/login.html.twig';
  60.         $formType $options['form'] ?? SecurityLoginType::class;
  61.         $form $this->formFactory->createNamed(''$formType);
  62.         return new Response($this->templatingEngine->render($template, [
  63.             'form' => $form->createView(),
  64.             'last_username' => $lastUsername,
  65.             'last_error' => $lastError,
  66.         ]));
  67.     }
  68.     public function checkAction(Request $request): void
  69.     {
  70.         throw new \RuntimeException('You must configure the check path to be handled by the firewall.');
  71.     }
  72.     public function logoutAction(Request $request): void
  73.     {
  74.         throw new \RuntimeException('You must configure the logout path to be handled by the firewall.');
  75.     }
  76. }