app/Plugin/CustomerGroup42/Security/EventSubscriber/LoginSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of CustomerGroup
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\CustomerGroup42\Security\EventSubscriber;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Entity\Category;
  15. use Eccube\Entity\Customer;
  16. use Eccube\Entity\Product;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  20. use Symfony\Component\Security\Http\SecurityEvents;
  21. class LoginSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var UrlGeneratorInterface
  25.      */
  26.     private UrlGeneratorInterface $urlGenerator;
  27.     /**
  28.      * @var EntityManagerInterface
  29.      */
  30.     private EntityManagerInterface $entityManager;
  31.     public function __construct(
  32.         UrlGeneratorInterface $urlGenerator,
  33.         EntityManagerInterface $entityManager
  34.     ) {
  35.         $this->urlGenerator $urlGenerator;
  36.         $this->entityManager $entityManager;
  37.     }
  38.     /**
  39.      * @return string[]
  40.      */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  45.         ];
  46.     }
  47.     /**
  48.      * @param InteractiveLoginEvent $event
  49.      *
  50.      * @return void
  51.      */
  52.     public function onInteractiveLogin(InteractiveLoginEvent $event): void
  53.     {
  54.         $user $event->getAuthenticationToken()->getUser();
  55.         if (!$user instanceof Customer) {
  56.             return;
  57.         }
  58.         $event->getAuthenticationToken()->setAttribute('canViewProducts'array_map(function (Product $product) {
  59.             return $product->getId();
  60.         }, $user->getGroupProducts()->toArray()));
  61.         $event->getAuthenticationToken()->setAttribute('canViewCategories'array_map(function (Category $category) {
  62.             return $category->getId();
  63.         }, $user->getGroupCategories()->toArray()));
  64.     }
  65. }