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

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\Component\Form\FormFactoryInterface;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\RouterInterface;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  20. use Symfony\Component\Templating\EngineInterface;
  21. use Twig\Environment;
  22. final class SecurityController
  23. {
  24.     public function __construct(
  25.         private AuthenticationUtils $authenticationUtils,
  26.         private FormFactoryInterface $formFactory,
  27.         private EngineInterface|Environment $templatingEngine,
  28.         private AuthorizationCheckerInterface $authorizationChecker,
  29.         private RouterInterface $router,
  30.     ) {
  31.     }
  32.     public function loginAction(Request $request): Response
  33.     {
  34.         $alreadyLoggedInRedirectRoute $request->attributes->get('_sylius')['logged_in_route'] ?? null;
  35.         if ($alreadyLoggedInRedirectRoute && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  36.             return new RedirectResponse($this->router->generate($alreadyLoggedInRedirectRoute));
  37.         }
  38.         $lastError $this->authenticationUtils->getLastAuthenticationError();
  39.         $lastUsername $this->authenticationUtils->getLastUsername();
  40.         $options $request->attributes->get('_sylius');
  41.         $template $options['template'] ?? '@SyliusUi/Security/login.html.twig';
  42.         $formType $options['form'] ?? SecurityLoginType::class;
  43.         $form $this->formFactory->createNamed(''$formType);
  44.         return new Response($this->templatingEngine->render($template, [
  45.             'form' => $form->createView(),
  46.             'last_username' => $lastUsername,
  47.             'last_error' => $lastError,
  48.         ]));
  49.     }
  50.     public function checkAction(Request $request): void
  51.     {
  52.         throw new \RuntimeException('You must configure the check path to be handled by the firewall.');
  53.     }
  54.     public function logoutAction(Request $request): void
  55.     {
  56.         throw new \RuntimeException('You must configure the logout path to be handled by the firewall.');
  57.     }
  58. }