app/Plugin/CustomerGroup/Security/EventSubscriber/LoginSubscriber.php line 59

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\CustomerGroup\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. /**
  22.  * Class LoginSubscriber
  23.  * @package Plugin\CustomerGroup\Security\EventSubscriber
  24.  *
  25.  *
  26.  */
  27. class LoginSubscriber implements EventSubscriberInterface
  28. {
  29.     /**
  30.      * @var UrlGeneratorInterface
  31.      */
  32.     private $urlGenerator;
  33.     /**
  34.      * @var EntityManagerInterface
  35.      */
  36.     private $entityManager;
  37.     public function __construct(
  38.         UrlGeneratorInterface $urlGenerator,
  39.         EntityManagerInterface $entityManager
  40.     )
  41.     {
  42.         $this->urlGenerator $urlGenerator;
  43.         $this->entityManager $entityManager;
  44.     }
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin'
  49.         ];
  50.     }
  51.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  52.     {
  53.         $user $event->getAuthenticationToken()->getUser();
  54.         if (!$user instanceof Customer) {
  55.             return;
  56.         }
  57.         $event->getAuthenticationToken()->setAttribute('canViewProducts'array_map(function (Product $product) {
  58.             return $product->getId();
  59.         }, $user->getGroupProducts()->toArray()));
  60.         $event->getAuthenticationToken()->setAttribute('canViewCategories'array_map(function (Category $category) {
  61.             return $category->getId();
  62.         }, $user->getGroupCategories()->toArray()));
  63.     }
  64. }