custom/plugins/NetiNextAccessManager/src/Subscriber/RegisterSubscriber.php line 113

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Constants\BusinessEventsConstants;
  5. use NetInventors\NetiNextAccessManager\Events\BusinessEvent\CustomerRegisteredEvent;
  6. use NetInventors\NetiNextAccessManager\Events\BusinessEvent\CustomerRegisteredOperatorEvent;
  7. use NetInventors\NetiNextAccessManager\Extension\Content\Customer\Aggregate\CustomerAttribute\CustomerAttributeEntity;
  8. use NetInventors\NetiNextAccessManager\Service\CustomerService;
  9. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  10. use NetInventors\NetiNextAccessManager\Service\Repository\CustomerMediaRepository;
  11. use NetInventors\NetiNextAccessManager\Service\UploadFileService;
  12. use NetInventors\NetiNextAccessManager\Validation\AccountType\AccountType;
  13. use NetInventors\NetiNextAccessManager\Validation\CustomerFileUpload\BaseConstraint;
  14. use NetInventors\NetiNextAccessManager\Validation\CustomerFileUpload\CustomerType;
  15. use NetInventors\NetiNextAccessManager\Validation\CustomerFileUpload\FileSize;
  16. use NetInventors\NetiNextAccessManager\Validation\CustomerFileUpload\MaxAmount;
  17. use NetInventors\NetiNextAccessManager\Validation\CustomerFileUpload\MimeType;
  18. use NetInventors\NetiNextAccessManager\Validation\CustomerFileUpload\Required;
  19. use Shopware\Core\Checkout\Customer\CustomerEntity;
  20. use Shopware\Core\Checkout\Customer\Event\CustomerDoubleOptInRegistrationEvent;
  21. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  22. use Shopware\Core\Content\Media\Event\MediaFileExtensionWhitelistEvent;
  23. use Shopware\Core\Framework\Context;
  24. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  25. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  26. use Shopware\Core\Framework\Uuid\Uuid;
  27. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  28. use Shopware\Core\System\SystemConfig\SystemConfigService;
  29. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  30. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  31. use Symfony\Component\HttpFoundation\Request;
  32. use Symfony\Component\HttpFoundation\RequestStack;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. class RegisterSubscriber implements EventSubscriberInterface
  35. {
  36.     public const REGISTRATION_CUSTOMER_GROUPS 'netiAccessManagerRegistrationCustomerGroups';
  37.     private $isNotCustomerRegistration true;
  38.     /** @var RequestStack */
  39.     protected $requestStack;
  40.     /** @var PluginConfig */
  41.     protected $pluginConfig;
  42.     /** @var UploadFileService */
  43.     protected $uploadFileService;
  44.     /** @var CustomerMediaRepository */
  45.     protected $customerUploadFilesRepository;
  46.     /** @var CustomerService */
  47.     protected $customerService;
  48.     /** @var EventDispatcherInterface */
  49.     protected $eventDispatcher;
  50.     /** @var SystemConfigService */
  51.     protected                           $systemConfigService;
  52.     protected EntityRepositoryInterface $customerGroupRepository;
  53.     /**
  54.      * Register constructor.
  55.      *
  56.      * @param RequestStack              $requestStack
  57.      * @param PluginConfig              $pluginConfig
  58.      * @param UploadFileService         $uploadFileService
  59.      * @param CustomerMediaRepository   $customerUploadFilesRepository
  60.      * @param CustomerService           $customerService
  61.      * @param EventDispatcherInterface  $eventDispatcher
  62.      * @param SystemConfigService       $systemConfigService
  63.      * @param EntityRepositoryInterface $customerGroupRepository
  64.      */
  65.     public function __construct(
  66.         RequestStack $requestStack,
  67.         PluginConfig $pluginConfig,
  68.         UploadFileService $uploadFileService,
  69.         CustomerMediaRepository $customerUploadFilesRepository,
  70.         CustomerService $customerService,
  71.         EventDispatcherInterface $eventDispatcher,
  72.         SystemConfigService $systemConfigService,
  73.         EntityRepositoryInterface $customerGroupRepository
  74.     ) {
  75.         $this->requestStack                  $requestStack;
  76.         $this->pluginConfig                  $pluginConfig;
  77.         $this->uploadFileService             $uploadFileService;
  78.         $this->customerUploadFilesRepository $customerUploadFilesRepository;
  79.         $this->customerService               $customerService;
  80.         $this->eventDispatcher               $eventDispatcher;
  81.         $this->systemConfigService           $systemConfigService;
  82.         $this->customerGroupRepository       $customerGroupRepository;
  83.     }
  84.     /**
  85.      * @return array|string[]
  86.      */
  87.     public static function getSubscribedEvents(): array
  88.     {
  89.         return [
  90.             CustomerRegisterEvent::class                => 'onCustomerRegister',
  91.             CustomerDoubleOptInRegistrationEvent::class => 'onCustomerDoubleOptInRegistration',
  92.             'framework.validation.customer.create'      => 'onCreateCustomer',
  93.             MediaFileExtensionWhitelistEvent::class     => 'onWhiteListEvent',
  94.             AccountLoginPageLoadedEvent::class          => 'onAccountLoginPageLoaded',
  95.         ];
  96.     }
  97.     public function onAccountLoginPageLoaded(AccountLoginPageLoadedEvent $event):void
  98.     {
  99.         if (
  100.             !$this->pluginConfig->isActive()
  101.             || !$this->pluginConfig->isActivateCustomerGroupSelection()
  102.             || === \count($this->pluginConfig->getRegistrationCustomerGroups())
  103.         ) {
  104.             return;
  105.         }
  106.         $criteria       = new Criteria($this->pluginConfig->getRegistrationCustomerGroups());
  107.         $customerGroups $this->customerGroupRepository->search($criteria$event->getContext());
  108.         $event->getPage()->addExtension(self::REGISTRATION_CUSTOMER_GROUPS$customerGroups);
  109.     }
  110.     public function onCustomerDoubleOptInRegistration(CustomerDoubleOptInRegistrationEvent $event): void
  111.     {
  112.         if (!$this->pluginConfig->isActive()) {
  113.             return;
  114.         }
  115.         $currentRequest $this->requestStack->getCurrentRequest();
  116.         if (!$currentRequest instanceof Request) {
  117.             return;
  118.         }
  119.         $context  $event->getContext();
  120.         $customer $event->getCustomer();
  121.         $accountType $this->getAccountType($currentRequest);
  122.         $this->changeCustomerGroup($customer$accountType$context);
  123.         $this->customerService->setActiveAttribute(
  124.             $customer->getId(),
  125.             !$this->isPrivateLogin($accountType),
  126.             $context,
  127.             null,
  128.             CustomerAttributeEntity::ACTIVATED_BY_SYSTEM
  129.         );
  130.         $this->uploadFiles($accountType$currentRequest$context$customer);
  131.     }
  132.     public function onCreateCustomer(BuildValidationEvent $event): void
  133.     {
  134.         if (!$this->pluginConfig->isActive()) {
  135.             return;
  136.         }
  137.         $event->getDefinition()->add(BaseConstraint::REQUEST_KEY, new Required());
  138.         $event->getDefinition()->add(BaseConstraint::REQUEST_KEY, new MaxAmount());
  139.         $event->getDefinition()->add(BaseConstraint::REQUEST_KEY, new MimeType());
  140.         $event->getDefinition()->add(BaseConstraint::REQUEST_KEY, new FileSize());
  141.         $event->getDefinition()->add(BaseConstraint::REQUEST_KEY, new CustomerType());
  142.         $event->getDefinition()->add(AccountType::REQUEST_KEY, new AccountType());
  143.     }
  144.     protected function uploadFiles(string $accountTypeRequest $currentRequestContext $contextCustomerEntity $customer): array
  145.     {
  146.         $files = [];
  147.         if (
  148.             \in_array($accountType$this->pluginConfig->getUploadFile(), true)
  149.             || (
  150.                 $this->pluginConfig->isActivateCustomerGroupSelection()
  151.                 && \in_array($accountType$this->pluginConfig->getUploadFileCustomerGroups(), true)
  152.             )
  153.         ) {
  154.             $files $currentRequest->files->get(BaseConstraint::REQUEST_KEY);
  155.             if (!\is_array($files)) {
  156.                 return [];
  157.             }
  158.             $this->isNotCustomerRegistration false;
  159.             $files                           $this->uploadFileService->uploadFiles($files$context);
  160.             $this->isNotCustomerRegistration true;
  161.             $this->customerUploadFilesRepository->addUploadFiles(array_keys($files), $customer->getId(), $context);
  162.         }
  163.         return $files;
  164.     }
  165.     /**
  166.      * @param CustomerRegisterEvent $event
  167.      */
  168.     public function onCustomerRegister(CustomerRegisterEvent $event): void
  169.     {
  170.         $customer $event->getCustomer();
  171.         $context  $event->getContext();
  172.         if (!$this->pluginConfig->isActive()) {
  173.             $this->customerService->setActiveAttribute($customer->getId(), true$context);
  174.             return;
  175.         }
  176.         $currentRequest $this->requestStack->getCurrentRequest();
  177.         if (!$currentRequest instanceof Request || null === $currentRequest->get('accountType')) {
  178.             return;
  179.         }
  180.         /*
  181.          * Overwrite the account type with the customer group ID if the double opt-in registration is active
  182.          * and the customer group selections is active
  183.          */
  184.         $accountType $this->getAccountType($currentRequest);
  185.         if ($customer->getDoubleOptInRegistration() && $this->pluginConfig->isActivateCustomerGroupSelection()) {
  186.             $accountType $customer->getGroupId();
  187.         }
  188.         $files       $this->uploadFiles($accountType$currentRequest$context$customer);
  189.         $this->customerService->setActiveAttribute(
  190.             $customer->getId(),
  191.             !$this->isPrivateLogin($accountType),
  192.             $context,
  193.             null,
  194.             CustomerAttributeEntity::ACTIVATED_BY_SYSTEM
  195.         );
  196.         $this->changeCustomerGroup($customer$accountType$context);
  197.         if (CustomerEntity::ACCOUNT_TYPE_PRIVATE === $accountType) {
  198.             $this->customerService->setPrivateCustomerGroup($customer->getId(), $context);
  199.         } elseif (CustomerEntity::ACCOUNT_TYPE_BUSINESS === $accountType) {
  200.             $this->customerService->setBusinessCustomerGroup($customer->getId(), $context);
  201.         }
  202.         if (!$this->isPrivateLogin($accountType)) {
  203.             return;
  204.         }
  205.         $customerExtended $this->customerService->getCustomerWithSalutationAndBillingAddress($customer->getId(), $context);
  206.         if ([] === $files) {
  207.             $files $this->customerUploadFilesRepository->getUploadFilesIdsFromCustomer($customer->getId(), $context);
  208.         }
  209.         $this->eventDispatcher->dispatch(new CustomerRegisteredEvent($event->getSalesChannelContext(), $customerExtended));
  210.         $this->eventDispatcher->dispatch(
  211.             new CustomerRegisteredOperatorEvent(
  212.                 $event->getSalesChannelContext(),
  213.                 $customerExtended,
  214.                 array_keys($files),
  215.                 $this->systemConfigService,
  216.                 $this->getLink(BusinessEventsConstants::ACTIVATION_PATH$customerExtended->getId()),
  217.                 $this->getLink(BusinessEventsConstants::DEACTIVATION_PATH$customerExtended->getId())
  218.             )
  219.         );
  220.     }
  221.     protected function changeCustomerGroup(CustomerEntity $customerstring $customerGroupIdContext $context): void
  222.     {
  223.         if (
  224.             !$this->pluginConfig->isActivateCustomerGroupSelection()
  225.             || $customer->getGroupId() === $customerGroupId
  226.         ) {
  227.             return;
  228.         }
  229.         $this->customerService->changeCustomerGroup($customer->getId(), $customerGroupId$context);
  230.     }
  231.     protected function getAccountType(Request $request): string
  232.     {
  233.         $accountType $request->request->get('accountType'CustomerEntity::ACCOUNT_TYPE_PRIVATE);
  234.         if ('' === $accountType) {
  235.             $accountType CustomerEntity::ACCOUNT_TYPE_PRIVATE;
  236.         }
  237.         return $accountType;
  238.     }
  239.     protected function isPrivateLogin(string $accountType): bool
  240.     {
  241.         if ($this->pluginConfig->isActivateCustomerGroupSelection()) {
  242.             return \in_array($accountType$this->pluginConfig->getPrivateLoginCustomerGroups(), true);
  243.         }
  244.         return \in_array($accountType$this->pluginConfig->getPrivateLoginAccountTypes(), true);
  245.     }
  246.     protected function getLink(string $pathstring $customerId): string
  247.     {
  248.         $link '';
  249.         if ($this->pluginConfig->isAcceptPrivateLoginByMail()) {
  250.             $currentRequest $this->requestStack->getCurrentRequest();
  251.             if (!$currentRequest instanceof Request) {
  252.                 return '';
  253.             }
  254.             $baseUrl $currentRequest->get('sw-sales-channel-absolute-base-url');
  255.             $link    $baseUrl $path $customerId;
  256.         }
  257.         return $link;
  258.     }
  259.     public function onWhiteListEvent(MediaFileExtensionWhitelistEvent $event): void
  260.     {
  261.         if ($this->isNotCustomerRegistration || !$this->pluginConfig->isActive()) {
  262.             return;
  263.         }
  264.         $whiteList $event->getWhitelist();
  265.         foreach ($this->pluginConfig->getUploadFileFormats() as $mimeType) {
  266.             $extension explode('/'$mimeType)[1];
  267.             if (!\in_array($extension$whiteListtrue)) {
  268.                 $whiteList[] = $extension;
  269.             }
  270.         }
  271.         $event->setWhitelist($whiteList);
  272.     }
  273. }