src/EventListener/AuthenticationLoginListener.php line 62

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\EventListener;
  15. use App\Controller\CartController;
  16. use App\Model\CustomerManagementFramework\Activity\LoginActivity;
  17. use CustomerManagementFrameworkBundle\ActivityManager\ActivityManagerInterface;
  18. use CustomerManagementFrameworkBundle\Model\CustomerInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\CartManager\CartInterface;
  20. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  21. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  22. use Psr\Log\LoggerAwareInterface;
  23. use Psr\Log\LoggerAwareTrait;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
  26. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  27. use Symfony\Component\Security\Http\Event\LoginSuccessEvent;
  28. /**
  29.  * Authentication listener to set correct user to e-commerce framework environment after login and track login activity
  30.  */
  31. class AuthenticationLoginListener implements EventSubscriberInterfaceLoggerAwareInterface
  32. {
  33.     use LoggerAwareTrait;
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             LoginSuccessEvent::class => 'onLoginSuccess',
  38.         ];
  39.     }
  40.     public function __construct(
  41.         protected EnvironmentInterface $environment,
  42.         protected ActivityManagerInterface $activityManager,
  43.         protected Factory $factory)
  44.     {
  45.     }
  46.     /**
  47.      * This is called when an interactive authentication attempt succeeds. This
  48.      * is called by authentication listeners inheriting from
  49.      * AbstractAuthenticationListener.
  50.      *
  51.      * @param LoginSuccessEvent $event
  52.      *
  53.      * @return void
  54.      */
  55.     public function onLoginSuccess(LoginSuccessEvent $event): void
  56.     {
  57.         $passport $event->getPassport();
  58.         if (!$passport instanceof Passport || !$passport->hasBadge(PasswordUpgradeBadge::class)) {
  59.             return;
  60.         }
  61.         $user $passport->getUser();
  62.         if (null === $user->getPassword()) {
  63.             return;
  64.         }
  65.         // save current user to e-commerce framework environment
  66.         $this->doEcommerceFrameworkLogin($user);
  67.     }
  68.     public function doEcommerceFrameworkLogin(CustomerInterface $customer)
  69.     {
  70.         if ($customer) {
  71.             //migrate current cart entries to cart of to-log-in users cart
  72.             $cartManager $this->factory->getCartManager();
  73.             $oldCart $cartManager->getCartByName(CartController::DEFAULT_CART_NAME);
  74.             $this->environment->setCurrentUserId($customer->getId());
  75.             $cartManager->reset();
  76.             if ($oldCart instanceof CartInterface && count($oldCart->getItems()) > 0) {
  77.                 $userCart $this->factory->getCartManager()->getOrCreateCartByName(CartController::DEFAULT_CART_NAME);
  78.                 foreach ($oldCart->getItems() as $item) {
  79.                     $userCart->addItem($item->getProduct(), $item->getCount());
  80.                 }
  81.                 $userCart->save();
  82.             }
  83.         } else {
  84.             $this->environment->setCurrentUserId(null);
  85.         }
  86.         // track login activity
  87.         $this->activityManager->trackActivity(new LoginActivity($customer));
  88.         $this->environment->save();
  89.     }
  90. }