platform/src/Core/Checkout/Customer/Subscriber/CustomerTokenSubscriber.php line 42

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.     /**
  14.      * @var SalesChannelContextPersister
  15.      */
  16.     private $contextPersister;
  17.     /**
  18.      * @var RequestStack
  19.      */
  20.     private $requestStack;
  21.     public function __construct(
  22.         SalesChannelContextPersister $contextPersister,
  23.         RequestStack $requestStack
  24.     ) {
  25.         $this->contextPersister $contextPersister;
  26.         $this->requestStack $requestStack;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  32.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onCustomerDeleted',
  33.         ];
  34.     }
  35.     public function onCustomerWritten(EntityWrittenEvent $event): void
  36.     {
  37.         $master $this->requestStack->getMainRequest();
  38.         if (!$master) {
  39.             return;
  40.         }
  41.         if (!$master->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  42.             return;
  43.         }
  44.         /** @var SalesChannelContext $context */
  45.         $context $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  46.         $token $context->getToken();
  47.         $payloads $event->getPayloads();
  48.         foreach ($payloads as $payload) {
  49.             if ($this->customerCredentialsChanged($payload)) {
  50.                 $newToken $this->contextPersister->replace($token$context);
  51.                 $context->assign([
  52.                     'token' => $newToken,
  53.                 ]);
  54.                 if (!$master->hasSession()) {
  55.                     return;
  56.                 }
  57.                 $session $master->getSession();
  58.                 $session->migrate();
  59.                 $session->set('sessionId'$session->getId());
  60.                 $session->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  61.                 $master->headers->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  62.                 return;
  63.             }
  64.         }
  65.     }
  66.     public function onCustomerDeleted(EntityDeletedEvent $event): void
  67.     {
  68.         $master $this->requestStack->getMainRequest();
  69.         if (!$master) {
  70.             return;
  71.         }
  72.         $customerIds $event->getIds();
  73.         foreach ($customerIds as $customerId) {
  74.             $this->contextPersister->revokeAllCustomerTokens($customerId);
  75.         }
  76.     }
  77.     private function customerCredentialsChanged(array $payload): bool
  78.     {
  79.         return isset($payload['password']);
  80.     }
  81. }