custom/static-plugins/SamsonCustomer/src/Controller/AccountProfileController.php line 97

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Controller;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2022
  11.  *
  12.  ***/
  13. use Psr\Log\LoggerInterface;
  14. use Samson\CustomFieldSet\Constants\CustomerCustomFieldConstants;
  15. use Samson\Entities\CustomerExtension\CustomerExtensionEntity;
  16. use Samson\Exception\CustomerNeedsPasswordChangeException;
  17. use Samson\Pages\SubAccount\SubAccountPageLoader;
  18. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  19. use Shopware\Core\Checkout\Customer\CustomerEntity;
  20. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeCustomerProfileRoute;
  21. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangeEmailRoute;
  22. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractChangePasswordRoute;
  23. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractDeleteCustomerRoute;
  24. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLogoutRoute;
  25. use Shopware\Core\Content\Category\Exception\CategoryNotFoundException;
  26. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  27. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  28. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  29. use Shopware\Core\Framework\Routing\Annotation\Since;
  30. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  31. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  32. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  33. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  34. use \Shopware\Storefront\Controller\AccountProfileController as ShopwareAccountProfileController;
  35. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  36. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoader;
  37. use Shopware\Storefront\Page\Account\Profile\AccountProfilePageLoader;
  38. use Symfony\Component\HttpFoundation\Request;
  39. use Symfony\Component\HttpFoundation\Response;
  40. use Symfony\Component\Routing\Annotation\Route;
  41. use Samson\Annotation\CustomerNeedsPasswordChange;
  42. /**
  43.  * @RouteScope(scopes={"storefront"})
  44.  */
  45. class AccountProfileController extends ShopwareAccountProfileController
  46. {
  47.     private AbstractLogoutRoute $logoutRoute;
  48.     private SubAccountPageLoader $subAccountPageLoader;
  49.     private AbstractDeleteCustomerRoute $deleteCustomerRoute;
  50.     private AbstractChangePasswordRoute $changePasswordRoute;
  51.     public function __construct(
  52.         AccountOverviewPageLoader          $overviewPageLoader,
  53.         AccountProfilePageLoader           $profilePageLoader,
  54.         AbstractChangeCustomerProfileRoute $changeCustomerProfileRoute,
  55.         AbstractChangePasswordRoute        $changePasswordRoute,
  56.         AbstractChangeEmailRoute           $changeEmailRoute,
  57.         AbstractDeleteCustomerRoute        $deleteCustomerRoute,
  58.         LoggerInterface                    $logger,
  59.         AbstractLogoutRoute                $logoutRoute,
  60.         SubAccountPageLoader               $subAccountPageLoader
  61.     )
  62.     {
  63.         parent::__construct($overviewPageLoader,
  64.             $profilePageLoader,
  65.             $changeCustomerProfileRoute,
  66.             $changePasswordRoute,
  67.             $changeEmailRoute,
  68.             $deleteCustomerRoute,
  69.             $logger
  70.         );
  71.         $this->logoutRoute $logoutRoute;
  72.         $this->subAccountPageLoader $subAccountPageLoader;
  73.         $this->deleteCustomerRoute $deleteCustomerRoute;
  74.         $this->changePasswordRoute $changePasswordRoute;
  75.     }
  76.     /**
  77.      * @Since("6.0.0.0")
  78.      * @LoginRequired()
  79.      * @CustomerNeedsPasswordChange()
  80.      * @Route("/account", name="frontend.account.home.page", methods={"GET"})
  81.      * @NoStore
  82.      *
  83.      * @throws CustomerNotLoggedInException
  84.      * @throws CategoryNotFoundException
  85.      * @throws InconsistentCriteriaIdsException
  86.      * @throws MissingRequestParameterException
  87.      * @throws CustomerNeedsPasswordChangeException
  88.      */
  89.     public function index(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  90.     {
  91.         if (array_key_exists(CustomerCustomFieldConstants::CUSTOM_FIELD_ADMIN_ACTIVATION_NEEDED$customer->getCustomFields() ?? [])) {
  92.             $this->logoutRoute->logout($context, new RequestDataBag());
  93.             return $this->redirectToRoute("frontend.account.login.page");
  94.         }
  95.         return parent::index($request$context$customer);
  96.     }
  97.     /**
  98.      * @Since("6.0.0.0")
  99.      * @LoginRequired()
  100.      * @CustomerNeedsPasswordChange()
  101.      * @Route("/account/profile", name="frontend.account.profile.page", methods={"GET"})
  102.      * @NoStore
  103.      *
  104.      * @throws CustomerNotLoggedInException
  105.      * @throws CategoryNotFoundException
  106.      * @throws InconsistentCriteriaIdsException
  107.      * @throws MissingRequestParameterException
  108.      * @throws CustomerNeedsPasswordChangeException
  109.      */
  110.     public function profileOverview(Request $requestSalesChannelContext $context): Response
  111.     {
  112.         return parent::profileOverview($request$context);
  113.     }
  114.     /**
  115.      * @Since("6.0.0.0")
  116.      * @LoginRequired()
  117.      * @CustomerNeedsPasswordChange()
  118.      * @Route("/account/profile", name="frontend.account.profile.save", methods={"POST"})
  119.      *
  120.      * @throws CustomerNotLoggedInException
  121.      * @throws CustomerNeedsPasswordChangeException
  122.      */
  123.     public function saveProfile(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  124.     {
  125.         return parent::saveProfile($data$context$customer);
  126.     }
  127.     /**
  128.      * @Since("6.0.0.0")
  129.      * @LoginRequired()
  130.      * @CustomerNeedsPasswordChange()
  131.      * @Route("/account/profile/email", name="frontend.account.profile.email.save", methods={"POST"})
  132.      *
  133.      * @throws CustomerNotLoggedInException
  134.      * @throws CustomerNeedsPasswordChangeException
  135.      */
  136.     public function saveEmail(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  137.     {
  138.         return parent::saveEmail($data$context$customer);
  139.     }
  140.     /**
  141.      * @Since("6.0.0.0")
  142.      * @LoginRequired()
  143.      * @CustomerNeedsPasswordChange()
  144.      * @Route("/account/profile/password", name="frontend.account.profile.password.save", methods={"POST"})
  145.      *
  146.      * @throws CustomerNotLoggedInException
  147.      * @throws CustomerNeedsPasswordChangeException
  148.      */
  149.     public function savePassword(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  150.     {
  151.         return parent::savePassword($data$context$customer);
  152.     }
  153.     /**
  154.      * @Since("6.3.3.0")
  155.      * @LoginRequired()
  156.      * @CustomerNeedsPasswordChange()
  157.      * @Route("/account/profile/delete", name="frontend.account.profile.delete", methods={"POST"})
  158.      *
  159.      * @throws CustomerNotLoggedInException
  160.      * @throws CustomerNeedsPasswordChangeException
  161.      */
  162.     public function deleteProfile(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  163.     {
  164.         return parent::deleteProfile($request$context$customer);
  165.     }
  166.     /**
  167.      * @Route("/account/sub-account", name="frontend.account.sub-account.page", methods={"GET"})
  168.      * @LoginRequired()
  169.      *
  170.      * @throws CustomerNotLoggedInException
  171.      */
  172.     public function subAccountOverview(Request $requestSalesChannelContext $context): Response
  173.     {
  174.         $customer $context->getCustomer();
  175.         if ($this->isSubAccount($customer)) {
  176.             return $this->redirectToRoute('frontend.account.home.page', ['customer' => $customer]);
  177.         }
  178.         $page $this->subAccountPageLoader->load($request$context);
  179.         return $this->renderStorefront('@Storefront/storefront/page/account/sub-account/index.html.twig',
  180.             [
  181.                 'page' => $page
  182.             ]);
  183.     }
  184.     /**
  185.      * @Route("/account/sub-account/{subAccountId}", name="frontend.account.sub-account.delete", methods={"GET"})
  186.      * @LoginRequired()
  187.      *
  188.      * @throws CustomerNotLoggedInException
  189.      */
  190.     public function deleteSubAccount(string $subAccountIdRequest $requestSalesChannelContext $context): Response
  191.     {
  192.         $customer $context->getCustomer();
  193.         if ($this->isSubAccount($customer)) {
  194.             return $this->redirectToRoute('frontend.account.home.page', ['customer' => $customer]);
  195.         }
  196.         $customerEntity = new CustomerEntity();
  197.         $customerEntity->setId($subAccountId);
  198.         $this->deleteCustomerRoute->delete($context$customerEntity);
  199.         return $this->redirectToRoute('frontend.account.sub-account.page');
  200.     }
  201.     /**
  202.      * @Route("/sub-account/password", name="frontend.sub-account.password.page", methods={"GET"})
  203.      * @LoginRequired()
  204.      *
  205.      * @throws CustomerNotLoggedInException
  206.      **/
  207.     public function showPasswordChange(Request $requestRequestDataBag $dataBagSalesChannelContext $context): Response
  208.     {
  209.         return $this->renderStorefront('@Storefront/storefront/page/password/index.html.twig',
  210.             [
  211.                 'redirectTo' => $request->get('redirectTo'),
  212.                 'redirectParameters' => $request->get('redirectParameters'json_encode([]))
  213.             ]);
  214.     }
  215.     /**
  216.      * @Route("/sub-account/password", name="frontend.sub-account.password.change", methods={"POST"})
  217.      * @LoginRequired()
  218.      *
  219.      * @throws CustomerNotLoggedInException
  220.      **/
  221.     public function changePassword(Request $requestRequestDataBag $dataBagSalesChannelContext $context): Response
  222.     {
  223.         $customer $context->getCustomer();
  224.         try {
  225.             $this->changePasswordRoute->change($dataBag->get('password')->toRequestDataBag(), $context$customer);
  226.             $this->addFlash(self::SUCCESS$this->trans('account.passwordChangeSuccess'));
  227.         } catch (ConstraintViolationException $formViolations) {
  228.             $this->addFlash(self::DANGER$this->trans('account.passwordChangeNoSuccess'));
  229.             return $this->forwardToRoute('frontend.account.profile.page', ['formViolations' => $formViolations'passwordFormViolation' => true]);
  230.         }
  231.         if ($dataBag->has('redirectTo') && !empty($dataBag->get('redirectTo'))) {
  232.             return $this->redirectToRoute($dataBag->get('redirectTo'), json_decode($dataBag->get('redirectParameters'), true));
  233.         }
  234.         return $this->redirectToRoute('frontend.home.page');
  235.     }
  236.     private function isSubAccount(CustomerEntity $customer): bool
  237.     {
  238.         return isset($customer->getCustomFields()[CustomerCustomFieldConstants::CUSTOM_FIELD_IS_SUB_ACCOUNT])
  239.             && $customer->getCustomFields()[CustomerCustomFieldConstants::CUSTOM_FIELD_IS_SUB_ACCOUNT];
  240.     }
  241. }