src/Controller/Shop/SecurityController.php line 90

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 App\Controller\Shop;
  12. use Sylius\Bundle\UiBundle\Form\Type\SecurityLoginType;
  13. use Sylius\Component\User\Repository\UserRepositoryInterface;
  14. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\RouterInterface;
  20. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  21. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  22. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  23. use Twig\Environment;
  24. final class SecurityController
  25. {
  26.     private AuthenticationUtils $authenticationUtils;
  27.     private FormFactoryInterface $formFactory;
  28.     /** @var EngineInterface|Environment */
  29.     private $templatingEngine;
  30.     private AuthorizationCheckerInterface $authorizationChecker;
  31.     private RouterInterface $router;
  32.     private UserRepositoryInterface $userRepository;
  33.     /**
  34.      * @param EngineInterface|Environment $templatingEngine
  35.      */
  36.     public function __construct(
  37.         AuthenticationUtils $authenticationUtils,
  38.         FormFactoryInterface $formFactory,
  39.         object $templatingEngine,
  40.         AuthorizationCheckerInterface $authorizationChecker,
  41.         RouterInterface $router,
  42.         UserRepositoryInterface $userRepository
  43.     ) {
  44.         $this->authenticationUtils $authenticationUtils;
  45.         $this->formFactory $formFactory;
  46.         $this->templatingEngine $templatingEngine;
  47.         $this->authorizationChecker $authorizationChecker;
  48.         $this->router $router;
  49.         $this->userRepository $userRepository;
  50.     }
  51.     public function loginAction(Request $request): Response
  52.     {
  53.         $errorFormerCustomer null;
  54.         $alreadyLoggedInRedirectRoute $request->attributes->get('_sylius')['logged_in_route'] ?? null;
  55.         if ($alreadyLoggedInRedirectRoute && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  56.             return new RedirectResponse($this->router->generate($alreadyLoggedInRedirectRoute));
  57.         }
  58.         $lastError $this->authenticationUtils->getLastAuthenticationError();
  59.         $lastUsername $this->authenticationUtils->getLastUsername();
  60.         $user $this->userRepository->findOneBy(['username' => $lastUsername]);
  61.         // check if user is former user (from presta). In that case, display error message to invite him to click on "forgot password"
  62.         if ($user && $user->getFormerCustomer()) {
  63.             $errorFormerCustomer = new BadCredentialsException('app.ui.error_former_customer');
  64.         }
  65.         $options $request->attributes->get('_sylius');
  66.         $template $options['template'] ?? '@SyliusUi/Security/login.html.twig';
  67.         $formType $options['form'] ?? SecurityLoginType::class;
  68.         $form $this->formFactory->createNamed(''$formType);
  69.         return new Response($this->templatingEngine->render($template, [
  70.             'form' => $form->createView(),
  71.             'last_username' => $lastUsername,
  72.             'last_error' => $lastError,
  73.             'error_former_customer' => $errorFormerCustomer
  74.         ]));
  75.     }
  76.     public function checkAction(Request $request): void
  77.     {
  78.         throw new \RuntimeException('You must configure the check path to be handled by the firewall.');
  79.     }
  80.     public function logoutAction(Request $request): void
  81.     {
  82.         throw new \RuntimeException('You must configure the logout path to be handled by the firewall.');
  83.     }
  84. }