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

  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\EntityWriteResult;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  7. use Shopware\Core\Framework\Log\Package;
  8. use Shopware\Core\PlatformRequest;
  9. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextPersister;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. /**
  14.  * @internal
  15.  */
  16. #[Package('customer-order')]
  17. class CustomerTokenSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(
  23.         private readonly SalesChannelContextPersister $contextPersister,
  24.         private readonly RequestStack $requestStack
  25.     ) {
  26.     }
  27.     /**
  28.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             CustomerEvents::CUSTOMER_WRITTEN_EVENT => 'onCustomerWritten',
  34.             CustomerEvents::CUSTOMER_DELETED_EVENT => 'onCustomerDeleted',
  35.         ];
  36.     }
  37.     public function onCustomerWritten(EntityWrittenEvent $event): void
  38.     {
  39.         foreach ($event->getWriteResults() as $writeResult) {
  40.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_UPDATE) {
  41.                 continue;
  42.             }
  43.             $payload $writeResult->getPayload();
  44.             if (!$this->customerCredentialsChanged($payload)) {
  45.                 continue;
  46.             }
  47.             $customerId $payload['id'];
  48.             $newToken $this->invalidateUsingSession($customerId);
  49.             if ($newToken) {
  50.                 $this->contextPersister->revokeAllCustomerTokens($customerId$newToken);
  51.             } else {
  52.                 $this->contextPersister->revokeAllCustomerTokens($customerId);
  53.             }
  54.         }
  55.     }
  56.     public function onCustomerDeleted(EntityDeletedEvent $event): void
  57.     {
  58.         foreach ($event->getIds() as $customerId) {
  59.             $this->contextPersister->revokeAllCustomerTokens($customerId);
  60.         }
  61.     }
  62.     /**
  63.      * @param array<string, mixed> $payload
  64.      */
  65.     private function customerCredentialsChanged(array $payload): bool
  66.     {
  67.         return isset($payload['password']);
  68.     }
  69.     private function invalidateUsingSession(string $customerId): ?string
  70.     {
  71.         $master $this->requestStack->getMainRequest();
  72.         if (!$master) {
  73.             return null;
  74.         }
  75.         // Is not a storefront request
  76.         if (!$master->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  77.             return null;
  78.         }
  79.         /** @var SalesChannelContext $context */
  80.         $context $master->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  81.         // Not loggedin skip
  82.         if ($context->getCustomer() === null) {
  83.             return null;
  84.         }
  85.         // The written customer is not the same as logged-in. We don't modify the user session
  86.         if ($context->getCustomer()->getId() !== $customerId) {
  87.             return null;
  88.         }
  89.         $token $context->getToken();
  90.         $newToken $this->contextPersister->replace($token$context);
  91.         $context->assign([
  92.             'token' => $newToken,
  93.         ]);
  94.         if (!$master->hasSession()) {
  95.             return null;
  96.         }
  97.         $session $master->getSession();
  98.         $session->migrate();
  99.         $session->set('sessionId'$session->getId());
  100.         $session->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  101.         $master->headers->set(PlatformRequest::HEADER_CONTEXT_TOKEN$newToken);
  102.         return $newToken;
  103.     }
  104. }