custom/plugins/NrbnPrivateshop/src/Storefront/Controller/FrontendController.php line 114

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NrbnPrivateshop\Storefront\Controller;
  3. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Core\Content\Mail\Service\MailService;
  6. use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
  7. use Shopware\Core\Content\MailTemplate\MailTemplateTypes;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  11. use Shopware\Core\System\SalesChannel\ContextTokenResponse;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\RedirectResponse;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use \Symfony\Component\HttpKernel\KernelEvents;
  20. use Shopware\Storefront\Framework\Twig\TemplateConfigAccessor;
  21. class FrontendController implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var ContainerInterface;
  25.      */
  26.     protected $container;
  27.     /**
  28.      * @var TemplateConfigAccessor;
  29.      */
  30.     protected $config;
  31.     /**
  32.      * @var SystemConfigService
  33.      */
  34.     protected $systemConfig;
  35.     /**
  36.      * @var MailService
  37.      */
  38.     protected $mailService;
  39.     public function __construct(
  40.         ContainerInterface $container,
  41.         TemplateConfigAccessor $config,
  42.         SystemConfigService $systemConfig,
  43.         MailService $mailService
  44.     ){
  45.         $this->container $container;
  46.         $this->config $config;
  47.         $this->systemConfig $systemConfig;
  48.         $this->mailService $mailService;
  49.     }
  50.     public static function getSubscribedEvents(): array
  51.     {
  52.         return [
  53.             KernelEvents::RESPONSE => 'onRequest',
  54.             CustomerRegisterEvent::class => 'onRequestRegister',
  55.             AccountOverviewPageLoadedEvent::class => 'onAccountLoginRequest'
  56.         ];
  57.     }
  58.     public function onAccountLoginRequest(AccountOverviewPageLoadedEvent $event){
  59.         $customer $event->getSalesChannelContext()->getCustomer();
  60.         $reqAttr $event->getRequest()->attributes->all();
  61.         if($customer->getActive() == false){
  62.             $sessionService $this->container->get("session");
  63.             $sessionService->set('deactivateUser'1);
  64.             $logOutEvent = new CustomerLogoutEvent($event->getSalesChannelContext(), $customer);
  65.             $eventDispatcher $this->container->get("event_dispatcher");
  66.             $eventDispatcher->dispatch($logOutEvent);
  67.             new ContextTokenResponse($event->getSalesChannelContext()->getToken());
  68.             header('Location: /account/login');
  69.             die();
  70.         }
  71.         return true;
  72.     }
  73.     public function onRequestRegister(CustomerRegisterEvent $event){
  74.         $customerId $event->getCustomer()->getId();
  75.         $criteria = new Criteria();
  76.         $criteria->addFilter(new EqualsFilter("id"$customerId));
  77.         $customerService $this->container->get("customer.repository");
  78.         $customerService->update([
  79.             [
  80.                 "id" => $customerId,
  81.                 "active" => false
  82.             ]
  83.         ], $event->getContext());
  84.         #$this->sendMail($event->getSalesChannelContext(), $event->getCustomer());
  85.     }
  86.     public function onRequest(ResponseEvent $event)
  87.     {
  88.         $context $event->getRequest()->attributes->get("sw-sales-channel-context");
  89.         $user false;
  90.         
  91.         if($context){
  92.             $user $context->getCustomer();
  93.         }
  94.         
  95.         $sessionService $this->container->get("session");
  96.         $sessionService->remove("deactivateUser");
  97.         $reqAttr $event->getRequest()->attributes->all();
  98.         $isRegisterAllowed $this->systemConfig->get("NrbnPrivateshop.config.userRegistration");
  99.         $event->getRequest()->attributes->set("isRegisterAllowed"$isRegisterAllowed);
  100.         if(
  101.             !key_exists("resolved-uri"$reqAttr) ||
  102.             !key_exists("sw-original-request-uri"$reqAttr) ||
  103.             $user
  104.         )
  105.         {
  106.             return true;
  107.         };
  108.         $allowedUrls = [
  109.             "frontend.account.login.page",
  110.             "frontend.account.home.page",
  111.             "frontend.account.register.page",
  112.             "frontend.account.recover.page",
  113.             "frontend.checkout.info",
  114.             "frontend.cart.offcanvas",
  115.             "frontend.cookie.offcanvas",
  116.             "frontend.cookie.permission",
  117.             "frontend.search.suggest",
  118.             "/account/recover",
  119.             "/account/recover/password",
  120.             "/form/contact",
  121.             "/contact/",
  122.             "/AGB/",
  123.             "/AGB/",
  124.             "/Impressum/",
  125.             "/Datenschutzerklaerung/",
  126.             "/Kontaktformular/",
  127.         ];
  128.         foreach ($allowedUrls as $allowedUrl){
  129.             try{
  130.                 $url $this->container->get('router')->generate($allowedUrl);
  131.             } catch (\Exception $e){
  132.                 $url $allowedUrl;
  133.             }
  134.             if(
  135.                 $reqAttr["resolved-uri"] == $url ||
  136.                 $reqAttr["sw-original-request-uri"] == $url
  137.             ){
  138.                 return true;
  139.             }
  140.         }
  141.         $salesChannelId $event->getRequest()->attributes->get("sw-sales-channel-id");
  142.         $allowedCMSPages = [
  143.             "core.basicInformation.privacyPage",
  144.             "core.basicInformation.imprintPage",
  145.             "core.basicInformation.contactPage",
  146.             "core.basicInformation.shippingPaymentInfoPage",
  147.             "NrbnSenertec.config.SignUp"
  148.         ];
  149.         foreach ($allowedCMSPages as $allowedCMSPage){
  150.             $pageId $this->config->config($allowedCMSPage$salesChannelId);
  151.             $pageUrl $this->container->get('router')->generate('frontend.cms.page', ['id'=>$pageId]);
  152.             if(
  153.                 $reqAttr["resolved-uri"] == $pageUrl
  154.             )
  155.             {
  156.                 return true;
  157.             }
  158.         }
  159.         $loginUrl $this->container->get('router')->generate('frontend.account.login.page');
  160.         if($reqAttr["resolved-uri"] != $loginUrl){
  161.             $event->setResponse(new RedirectResponse($loginUrl));
  162.         }
  163.         return true;
  164.     }
  165.     private function sendMail(SalesChannelContext $salesChannelContext$customer): void
  166.     {
  167.         $data = new DataBag();
  168.         $data->set(
  169.             'recipients',
  170.             [
  171.                 "ghanei@netzreform.de" => $salesChannelContext->getSalesChannel()->getName(),
  172.                 $customer->getEmail() => $customer->getFirstName() . " " $customer->getLastName()
  173.             ]
  174.         );
  175.         $mailTemplate $this->getMailTemplate($salesChannelContext);
  176.         $template "<div style='font-family:arial; font-size:12px;'>
  177.             <p>" .
  178.             $customer->getFirstName() . " " $customer->getLastName() .",<br/>
  179.                 <br/>
  180.                 vielen Dank für Ihre Anmeldung in unserem Shop.<br/>
  181.                 Sie erhalten Zugriff über Ihre E-Mail-Adresse <strong> " $customer->getEmail() . "</strong> und dem von Ihnen gewählten Kennwort.<br/>
  182.                 Sie können Ihr Kennwort jederzeit nachträglich ändern.
  183.             </p>
  184.         </div>";
  185.         $data->set('senderName'$salesChannelContext->getSalesChannel()->getName());
  186.         //$data->set('templateId', $mailTemplate->getId());
  187.         $data->set('salesChannelId'$salesChannelContext->getSalesChannel()->getId());
  188.         $data->set('contentHtml'$template);
  189.         $data->set('contentPlain'"test");
  190.         $data->set('subject'$mailTemplate->getSubject());
  191.         $data->set('salesChannelId'$salesChannelContext->getSalesChannelId());
  192.         $this->mailService->send(
  193.             $data->all(),
  194.             $salesChannelContext->getContext()
  195.         );
  196.     }
  197.     private function getMailTemplate(SalesChannelContext $salesChannelContext): ?MailTemplateEntity
  198.     {
  199.         $criteria = new Criteria();
  200.         $criteria->addFilter(new EqualsFilter('mailTemplateType.technicalName'MailTemplateTypes::MAILTYPE_CUSTOMER_REGISTER));
  201.         $criteria->setLimit(1);
  202.         $mailTemplateRepository $this->container->get("mail_template.repository");
  203.         return $mailTemplateRepository->search($criteria$salesChannelContext->getContext())->first();
  204.     }
  205. }