app/Customize/EventListener/ControllerAugumentsListener.php line 142

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  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 Customize\EventListener;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Customize\Entity\LoggedinHistory;
  18. use Eccube\Entity\Customer;
  19. use Exception;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. use Symfony\Component\Security\Core\Security;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. /**
  26.  * コントローラを呼び出す直前のイベントリスナー。
  27.  * コントローラに渡すパラメータなどを変更できる。
  28.  * @see https://symfony.com/doc/4.4/components/http_kernel.html#component-http-kernel-event-table
  29.  */
  30. class ControllerAugumentsListener implements EventSubscriberInterface
  31. {
  32.     /**
  33.      * ログインチェックから除外するページの一覧(name)を定義する
  34.      */
  35.     const EXCLUDE_MULTIPLE_LOGIN_PREVENTION_PAGES = array(
  36.         'mypage_login'
  37.     );
  38.     const BLOG_DETAIL = array(
  39.         'cm_blog_pro_page_list',
  40.         'cm_blog_pro_page_detail'
  41.     );
  42.     /**
  43.      * @var EntityManagerInterface
  44.      */
  45.     protected $entityManager;
  46.     /**
  47.      * @var SessionInterface
  48.      */
  49.     private $session;
  50.     /**
  51.      * @var Security $security
  52.      */
  53.     private $security;
  54.     public function __construct(EntityManagerInterface $entityManagerSessionInterface $sessionSecurity $security)
  55.     {
  56.         $this->entityManager $entityManager;
  57.         $this->session $session;
  58.         $this->security $security;
  59.     }
  60.     /**
  61.      * 対応するメソッドと優先度を登録する。
  62.      * 数値が高いほど優先度高。
  63.      *
  64.      * @see https://symfony.com/doc/4.4/event_dispatcher.html
  65.      *
  66.      * @return array
  67.      */
  68.     public static function getSubscribedEvents()
  69.     {
  70.         return [
  71.             KernelEvents::CONTROLLER_ARGUMENTS => [
  72.                 ['multipleLoginPrevention'1],
  73.                 ['blogDetailBlocker'0]
  74.             ]
  75.         ];
  76.     }
  77.     /**
  78.      * セッションにログイントークン、データベースにユーザ情報とログイントークンを保存する。
  79.      * 一般ユーザログインとしてログイン済みで、他の端末(トークン)からのログインがあれば強制ログアウトする。
  80.      *
  81.      * @param ControllerArgumentsEvent $event
  82.      * @return RedirectResponse|void
  83.      */
  84.     public function multipleLoginPrevention(ControllerArgumentsEvent $event)
  85.     {
  86.         global $kernel;
  87.         $container $kernel->getContainer();
  88.         $request $event->getRequest();
  89.         $route $request->get('_route');
  90.         /** @var Customer|\Eccube\Entity\Member $user */
  91.         $user $this->security->getUser();
  92.         if (!$user instanceof Customer) {
  93.             return;
  94.         }
  95.         if (in_array($routeself::EXCLUDE_MULTIPLE_LOGIN_PREVENTION_PAGES)) {
  96.             return;
  97.         }
  98.         $sessionLoginToken $this->session->get('login_token''');
  99.         /** @var LoggedinHistory|null $loggedinHistory */
  100.         $loggedinHistory $this->entityManager->getRepository('\Customize\Entity\LoggedinHistory')
  101.             ->findOneBy(['Customer' => $user], ['login_date' => 'DESC']);
  102.         if (empty($sessionLoginToken) || empty($loggedinHistory)) {
  103.             return;
  104.         }
  105.         $dbLoginToken $loggedinHistory->getLoginToken();
  106.         if ($dbLoginToken != $sessionLoginToken) {
  107.             $container->get('security.token_storage')->setToken(null);
  108.             $redirectUrl $container->get('router')->generate('mypage_login', [], UrlGeneratorInterface::ABSOLUTE_PATH);
  109.             if (empty($redirectUrl)) {
  110.                 $redirectUrl '/';
  111.             }
  112.             $this->session->getFlashBag()->add('customize.info.auto_logout''他の画面からのログインを検知したためログアウトされました。');
  113.             $event->setController(function () use ($redirectUrl) {
  114.                 return new RedirectResponse($redirectUrl);
  115.             });
  116.         }
  117.     }
  118.     /**
  119.      * 会員専用ブログにアクセスできないようデフォルトのブログ画面へのアクセスはリダイレクトさせる
  120.      *
  121.      * @param ControllerArgumentsEvent $event
  122.      * @return void
  123.      * @throws NotFoundHttpException
  124.      */
  125.     public function blogDetailBlocker(ControllerArgumentsEvent $event)
  126.     {
  127.         $request $event->getRequest();
  128.         $route $request->get('_route');
  129.         if (in_array($routeself::BLOG_DETAIL)) {
  130.             throw new NotFoundHttpException();
  131.         }
  132.     }
  133. }