vendor/shopware/storefront/Controller/AuthController.php line 127

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  4. use Shopware\Core\Checkout\Customer\Exception\BadCredentialsException;
  5. use Shopware\Core\Checkout\Customer\Exception\CustomerAuthThrottledException;
  6. use Shopware\Core\Checkout\Customer\Exception\CustomerNotFoundByHashException;
  7. use Shopware\Core\Checkout\Customer\Exception\CustomerNotFoundException;
  8. use Shopware\Core\Checkout\Customer\Exception\CustomerRecoveryHashExpiredException;
  9. use Shopware\Core\Checkout\Customer\Exception\InactiveCustomerException;
  10. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLoginRoute;
  11. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLogoutRoute;
  12. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractResetPasswordRoute;
  13. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractSendPasswordRecoveryMailRoute;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  15. use Shopware\Core\Framework\Feature;
  16. use Shopware\Core\Framework\Log\Package;
  17. use Shopware\Core\Framework\RateLimiter\Exception\RateLimitExceededException;
  18. use Shopware\Core\Framework\Routing\Annotation\Since;
  19. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  20. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  21. use Shopware\Core\PlatformRequest;
  22. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  23. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Shopware\Storefront\Checkout\Cart\SalesChannel\StorefrontCartFacade;
  26. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  27. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  28. use Shopware\Storefront\Page\Account\Login\AccountGuestLoginPageLoadedHook;
  29. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedHook;
  30. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoader;
  31. use Shopware\Storefront\Page\Account\RecoverPassword\AccountRecoverPasswordPage;
  32. use Shopware\Storefront\Page\Account\RecoverPassword\AccountRecoverPasswordPageLoadedHook;
  33. use Shopware\Storefront\Page\Account\RecoverPassword\AccountRecoverPasswordPageLoader;
  34. use Symfony\Component\HttpFoundation\Request;
  35. use Symfony\Component\HttpFoundation\Response;
  36. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  37. use Symfony\Component\Routing\Annotation\Route;
  38. /**
  39.  * @Route(defaults={"_routeScope"={"storefront"}})
  40.  *
  41.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  42.  */
  43. #[Package('storefront')]
  44. class AuthController extends StorefrontController
  45. {
  46.     private AccountLoginPageLoader $loginPageLoader;
  47.     private AbstractSendPasswordRecoveryMailRoute $sendPasswordRecoveryMailRoute;
  48.     private AbstractResetPasswordRoute $resetPasswordRoute;
  49.     private AbstractLoginRoute $loginRoute;
  50.     private AbstractLogoutRoute $logoutRoute;
  51.     private StorefrontCartFacade $cartFacade;
  52.     private AccountRecoverPasswordPageLoader $recoverPasswordPageLoader;
  53.     private SalesChannelContextServiceInterface $salesChannelContext;
  54.     private CartService $cartService;
  55.     /**
  56.      * @internal
  57.      */
  58.     public function __construct(
  59.         AccountLoginPageLoader $loginPageLoader,
  60.         AbstractSendPasswordRecoveryMailRoute $sendPasswordRecoveryMailRoute,
  61.         AbstractResetPasswordRoute $resetPasswordRoute,
  62.         AbstractLoginRoute $loginRoute,
  63.         AbstractLogoutRoute $logoutRoute,
  64.         StorefrontCartFacade $cartFacade,
  65.         AccountRecoverPasswordPageLoader $recoverPasswordPageLoader,
  66.         SalesChannelContextServiceInterface $salesChannelContextService,
  67.     CartService $cartService
  68.     ) {
  69.         $this->loginPageLoader $loginPageLoader;
  70.         $this->sendPasswordRecoveryMailRoute $sendPasswordRecoveryMailRoute;
  71.         $this->resetPasswordRoute $resetPasswordRoute;
  72.         $this->loginRoute $loginRoute;
  73.         $this->logoutRoute $logoutRoute;
  74.         $this->cartFacade $cartFacade;
  75.         $this->recoverPasswordPageLoader $recoverPasswordPageLoader;
  76.         $this->salesChannelContext $salesChannelContextService;
  77.     $this->cartService $cartService;
  78.     }
  79.     /**
  80.      * @Since("6.0.0.0")
  81.      * @Route("/account/login", name="frontend.account.login.page", methods={"GET"})
  82.      * @NoStore
  83.      */
  84.     public function loginPage(Request $requestRequestDataBag $dataSalesChannelContext $context): Response
  85.     {
  86.         /** @var string $redirect */
  87.         // $redirect = $request->get('redirectTo', 'frontend.account.home.page');
  88.     if ($this->cartService->getCart($context->getToken(), $context)->getLineItems()->count() > 0) {
  89.             $redirect $request->get('redirectTo''frontend.checkout.confirm.page');
  90.         } else {
  91.             $redirect $request->get('redirectTo''frontend.account.home.page');
  92.         }
  93.         $customer $context->getCustomer();
  94.         if ($customer !== null && $customer->getGuest() === false) {
  95.             $request->request->set('redirectTo'$redirect);
  96.             return $this->createActionResponse($request);
  97.         }
  98.         $page $this->loginPageLoader->load($request$context);
  99.         $this->hook(new AccountLoginPageLoadedHook($page$context));
  100.         return $this->renderStorefront('@Storefront/storefront/page/account/register/index.html.twig', [
  101.             'redirectTo' => $redirect,
  102.             'redirectParameters' => $request->get('redirectParameters'json_encode([])),
  103.             'page' => $page,
  104.             'loginError' => (bool) $request->get('loginError'),
  105.             'waitTime' => $request->get('waitTime'),
  106.             'errorSnippet' => $request->get('errorSnippet'),
  107.             'data' => $data,
  108.         ]);
  109.     }
  110.     /**
  111.      * @Since("6.3.4.1")
  112.      * @Route("/account/guest/login", name="frontend.account.guest.login.page", methods={"GET"})
  113.      * @NoStore
  114.      */
  115.     public function guestLoginPage(Request $requestSalesChannelContext $context): Response
  116.     {
  117.         /** @var string $redirect */
  118.         $redirect $request->get('redirectTo''frontend.account.home.page');
  119.         $customer $context->getCustomer();
  120.         if ($customer !== null) {
  121.             $request->request->set('redirectTo'$redirect);
  122.             return $this->createActionResponse($request);
  123.         }
  124.         $waitTime = (int) $request->get('waitTime');
  125.         if ($waitTime) {
  126.             $this->addFlash(self::INFO$this->trans('account.loginThrottled', ['%seconds%' => $waitTime]));
  127.         }
  128.         if ((bool) $request->get('loginError')) {
  129.             $this->addFlash(self::DANGER$this->trans('account.orderGuestLoginWrongCredentials'));
  130.         }
  131.         $page $this->loginPageLoader->load($request$context);
  132.         $this->hook(new AccountGuestLoginPageLoadedHook($page$context));
  133.         return $this->renderStorefront('@Storefront/storefront/page/account/guest-auth.html.twig', [
  134.             'redirectTo' => $redirect,
  135.             'redirectParameters' => $request->get('redirectParameters'json_encode([])),
  136.             'page' => $page,
  137.         ]);
  138.     }
  139.     /**
  140.      * @Since("6.0.0.0")
  141.      * @Route("/account/logout", name="frontend.account.logout.page", methods={"GET"})
  142.      */
  143.     public function logout(Request $requestSalesChannelContext $contextRequestDataBag $dataBag): Response
  144.     {
  145.         if ($context->getCustomer() === null) {
  146.             return $this->redirectToRoute('frontend.account.login.page');
  147.         }
  148.         try {
  149.             $this->logoutRoute->logout($context$dataBag);
  150.             $this->addFlash(self::SUCCESS$this->trans('account.logoutSucceeded'));
  151.             $parameters = [];
  152.         } catch (ConstraintViolationException $formViolations) {
  153.             $parameters = ['formViolations' => $formViolations];
  154.         }
  155.         return $this->redirectToRoute('frontend.account.login.page'$parameters);
  156.     }
  157.     /**
  158.      * @Since("6.0.0.0")
  159.      * @Route("/account/login", name="frontend.account.login", methods={"POST"}, defaults={"XmlHttpRequest"=true})
  160.      */
  161.     public function login(Request $requestRequestDataBag $dataSalesChannelContext $context): Response
  162.     {
  163.         $customer $context->getCustomer();
  164.         if ($customer !== null && $customer->getGuest() === false) {
  165.             return $this->createActionResponse($request);
  166.         }
  167.         try {
  168.             $token $this->loginRoute->login($data$context)->getToken();
  169.             $cartBeforeNewContext $this->cartFacade->get($token$context);
  170.             $newContext $this->salesChannelContext->get(
  171.                 new SalesChannelContextServiceParameters(
  172.                     $context->getSalesChannelId(),
  173.                     $token,
  174.                     $context->getLanguageIdChain()[0],
  175.                     $context->getCurrencyId(),
  176.                     $context->getDomainId(),
  177.                     $context->getContext()
  178.                 )
  179.             );
  180.             // Update the sales channel context for CacheResponseSubscriber
  181.             $request->attributes->set(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT$newContext);
  182.             if (!empty($token)) {
  183.                 $this->addCartErrors($cartBeforeNewContext);
  184.                 return $this->createActionResponse($request);
  185.             }
  186.         } catch (BadCredentialsException UnauthorizedHttpException InactiveCustomerException CustomerAuthThrottledException $e) {
  187.             if ($e instanceof InactiveCustomerException) {
  188.                 $errorSnippet $e->getSnippetKey();
  189.             }
  190.             if ($e instanceof CustomerAuthThrottledException) {
  191.                 $waitTime $e->getWaitTime();
  192.             }
  193.         }
  194.         $data->set('password'null);
  195.         return $this->forwardToRoute(
  196.             'frontend.account.login.page',
  197.             [
  198.                 'loginError' => true,
  199.                 'errorSnippet' => $errorSnippet ?? null,
  200.                 'waitTime' => $waitTime ?? null,
  201.             ]
  202.         );
  203.     }
  204.     /**
  205.      * @Since("6.1.0.0")
  206.      * @Route("/account/recover", name="frontend.account.recover.page", methods={"GET"})
  207.      */
  208.     public function recoverAccountForm(Request $requestSalesChannelContext $context): Response
  209.     {
  210.         $page $this->loginPageLoader->load($request$context);
  211.         return $this->renderStorefront('@Storefront/storefront/page/account/profile/recover-password.html.twig', [
  212.             'page' => $page,
  213.         ]);
  214.     }
  215.     /**
  216.      * @Since("6.1.0.0")
  217.      * @Route("/account/recover", name="frontend.account.recover.request", methods={"POST"})
  218.      */
  219.     public function generateAccountRecovery(Request $requestRequestDataBag $dataSalesChannelContext $context): Response
  220.     {
  221.         try {
  222.             $data->get('email')
  223.                 ->set('storefrontUrl'$request->attributes->get(RequestTransformer::STOREFRONT_URL));
  224.             $this->sendPasswordRecoveryMailRoute->sendRecoveryMail(
  225.                 $data->get('email')->toRequestDataBag(),
  226.                 $context,
  227.                 false
  228.             );
  229.             $this->addFlash(self::SUCCESS$this->trans('account.recoveryMailSend'));
  230.         } catch (CustomerNotFoundException $e) {
  231.             $this->addFlash(self::SUCCESS$this->trans('account.recoveryMailSend'));
  232.         } catch (InconsistentCriteriaIdsException $e) {
  233.             $this->addFlash(self::DANGER$this->trans('error.message-default'));
  234.         } catch (RateLimitExceededException $e) {
  235.             $this->addFlash(self::INFO$this->trans('error.rateLimitExceeded', ['%seconds%' => $e->getWaitTime()]));
  236.         }
  237.         return $this->redirectToRoute('frontend.account.recover.page');
  238.     }
  239.     /**
  240.      * @Since("6.1.0.0")
  241.      * @Route("/account/recover/password", name="frontend.account.recover.password.page", methods={"GET"})
  242.      */
  243.     public function resetPasswordForm(Request $requestSalesChannelContext $context): Response
  244.     {
  245.         /** @deprecated tag:v6.5.0 - call to loginPageLoader and $loginPage will be removed */
  246.         $loginPage null;
  247.         if (!Feature::isActive('v6.5.0.0')) {
  248.             $loginPage $this->loginPageLoader->load($request$context);
  249.         }
  250.         /** @var ?string $hash */
  251.         $hash $request->get('hash');
  252.         if (!$hash || !\is_string($hash)) {
  253.             $this->addFlash(self::DANGER$this->trans('account.passwordHashNotFound'));
  254.             return $this->redirectToRoute('frontend.account.recover.request');
  255.         }
  256.         try {
  257.             $page $this->recoverPasswordPageLoader->load($request$context$hash);
  258.         } catch (ConstraintViolationException $e) {
  259.             $this->addFlash(self::DANGER$this->trans('account.passwordHashNotFound'));
  260.             return $this->redirectToRoute('frontend.account.recover.request');
  261.         }
  262.         $this->hook(new AccountRecoverPasswordPageLoadedHook($page$context));
  263.         if ($page->getHash() === null || $page->isHashExpired()) {
  264.             $this->addFlash(self::DANGER$this->trans('account.passwordHashNotFound'));
  265.             return $this->redirectToRoute('frontend.account.recover.request');
  266.         }
  267.         if (Feature::isActive('v6.5.0.0')) {
  268.             return $this->renderStorefront('@Storefront/storefront/page/account/profile/reset-password.html.twig', [
  269.                 'page' => $page,
  270.                 'formViolations' => $request->get('formViolations'),
  271.             ]);
  272.         }
  273.         /** @deprecated tag:v6.5.0 - page will be instance of AccountRecoverPasswordPage and $hash will be moved to $page.getHash() */
  274.         return $this->renderStorefront('@Storefront/storefront/page/account/profile/reset-password.html.twig', [
  275.             'page' => $loginPage,
  276.             'hash' => $hash,
  277.             'formViolations' => $request->get('formViolations'),
  278.         ]);
  279.     }
  280.     /**
  281.      * @Since("6.1.0.0")
  282.      * @Route("/account/recover/password", name="frontend.account.recover.password.reset", methods={"POST"})
  283.      */
  284.     public function resetPassword(RequestDataBag $dataSalesChannelContext $context): Response
  285.     {
  286.         $hash $data->get('password')->get('hash');
  287.         try {
  288.             $pw $data->get('password');
  289.             $this->resetPasswordRoute->resetPassword($pw->toRequestDataBag(), $context);
  290.             $this->addFlash(self::SUCCESS$this->trans('account.passwordChangeSuccess'));
  291.         } catch (ConstraintViolationException $formViolations) {
  292.             $this->addFlash(self::DANGER$this->trans('account.passwordChangeNoSuccess'));
  293.             return $this->forwardToRoute(
  294.                 'frontend.account.recover.password.page',
  295.                 ['hash' => $hash'formViolations' => $formViolations'passwordFormViolation' => true]
  296.             );
  297.         } catch (CustomerNotFoundByHashException $e) {
  298.             $this->addFlash(self::DANGER$this->trans('account.passwordChangeNoSuccess'));
  299.             return $this->forwardToRoute('frontend.account.recover.request');
  300.         } catch (CustomerRecoveryHashExpiredException $e) {
  301.             $this->addFlash(self::DANGER$this->trans('account.passwordHashExpired'));
  302.             return $this->forwardToRoute('frontend.account.recover.request');
  303.         }
  304.         return $this->redirectToRoute('frontend.account.profile.page');
  305.     }
  306. }