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