vendor/shopware/core/Checkout/Customer/Subscriber/CustomerTokenSubscriber.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  6. use Shopware\Core\PlatformRequest;
  7. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class CustomerTokenSubscriber implements EventSubscriberInterface
  12. {
  13.     private SalesChannelContextPersister $contextPersister;
  14.     private RequestStack $requestStack;
  15.     public function __construct(
  16.         SalesChannelContextPersister $contextPersister,
  17.         RequestStack $requestStack
  18.     ) {
  19.         $this->contextPersister $contextPersister;
  20.         $this->requestStack $requestStack;
  21.     }
  22.     /**
  23.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  24.      */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  29.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onCustomerDeleted',
  30.         ];
  31.     }
  32.     public function onCustomerWritten(EntityWrittenEvent $event): void
  33.     {
  34.         $master $this->requestStack->getMainRequest();
  35.         if (!$master) {
  36.             return;
  37.         }
  38.         if (!$master->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  39.             return;
  40.         }
  41.         /** @var SalesChannelContext $context */
  42.         $context $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  43.         $token $context->getToken();
  44.         $payloads $event->getPayloads();
  45.         foreach ($payloads as $payload) {
  46.             if ($this->customerCredentialsChanged($payload)) {
  47.                 $newToken $this->contextPersister->replace($token$context);
  48.                 $context->assign([
  49.                     'token' => $newToken,
  50.                 ]);
  51.                 if (!$master->hasSession()) {
  52.                     return;
  53.                 }
  54.                 $session $master->getSession();
  55.                 $session->migrate();
  56.                 $session->set('sessionId'$session->getId());
  57.                 $session->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  58.                 $master->headers->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  59.                 return;
  60.             }
  61.         }
  62.     }
  63.     public function onCustomerDeleted(EntityDeletedEvent $event): void
  64.     {
  65.         $master $this->requestStack->getMainRequest();
  66.         if (!$master) {
  67.             return;
  68.         }
  69.         $customerIds $event->getIds();
  70.         foreach ($customerIds as $customerId) {
  71.             $this->contextPersister->revokeAllCustomerTokens($customerId);
  72.         }
  73.     }
  74.     private function customerCredentialsChanged(array $payload): bool
  75.     {
  76.         return isset($payload['password']);
  77.     }
  78. }