app/Plugin/Taba2FA/Taba2FAEvent.php line 117

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (C) 2018 SPREAD WORKS Inc.
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Plugin\Taba2FA;
  9. use Eccube\Common\EccubeConfig;
  10. use Eccube\Common\Constant;
  11. use Eccube\Event\TemplateEvent;
  12. use Eccube\Request\Context;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  18. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Plugin\Taba2FA\Common\UserConfig;
  25. use Plugin\Taba2FA\Common\Constants;
  26. class Taba2FAEvent implements EventSubscriberInterface
  27. {
  28.     /**
  29.      * @var EntityManagerInterface
  30.      */
  31.     protected $entityManager;
  32.     /**
  33.      * @var EventDispatcherInterface
  34.      */
  35.     protected $eventDispatcher;
  36.     /**
  37.      * @var Context
  38.      */
  39.     protected $requestContext;
  40.     /**
  41.      * @var CsrfTokenManagerInterface
  42.      */
  43.     protected $csrfTokenManager;
  44.     /**
  45.      * @var AuthorizationCheckerInterface
  46.      */
  47.     protected $authorizationChecker;
  48.     /**
  49.      * @var array
  50.      */
  51.     private $eccubeConfig;
  52.     /**
  53.      * @var ContainerInterface
  54.      */
  55.     private $container;
  56.     /**
  57.      * @var TokenStorage
  58.      */
  59.     private $tokenStorage;
  60.     /**
  61.      * Taba2FAEvent constructor.
  62.      *
  63.      */
  64.     public function __construct(
  65.         EntityManagerInterface $entityManager,
  66.         EventDispatcherInterface $eventDispatcher,
  67.         Context $requestContext,
  68.         AuthorizationCheckerInterface $authorizationChecker,
  69.         CsrfTokenManagerInterface $csrfTokenManager,
  70.         EccubeConfig $eccubeConfig,
  71.         ContainerInterface $container,
  72.         TokenStorage $tokenStorage
  73.         ) {
  74.             $this->entityManager $entityManager;
  75.             $this->eventDispatcher $eventDispatcher;
  76.             $this->requestContext $requestContext;
  77.             $this->authorizationChecker $authorizationChecker;
  78.             $this->csrfTokenManager $csrfTokenManager;
  79.             $this->eccubeConfig $eccubeConfig;
  80.             $this->container $container;
  81.             $this->tokenStorage $tokenStorage;
  82.     }
  83.     /**
  84.      * {@inheritdoc}
  85.      *
  86.      * @return array
  87.      */
  88.     public static function getSubscribedEvents()
  89.     {
  90.         return [
  91.             KernelEvents::CONTROLLER_ARGUMENTS => [['onKernelController'100000000]],
  92.         ];
  93.     }
  94.     /**
  95.      * 2段階認証の認証状態を確認
  96.      *
  97.      * @param FilterControllerEvent $event
  98.      */
  99.     public function onKernelController(FilterControllerEvent $event)
  100.     {
  101.         // 管理画面
  102.         if (!$this->requestContext->isAdmin()) {
  103.             return;
  104.         }
  105.         // ログイン済み
  106.         if (!$this->authorizationChecker->isGranted('ROLE_ADMIN') ) {
  107.             return;
  108.         }
  109.         // 管理画面メニュー
  110.         if ($event->getRequest()->attributes->has('_template')) {
  111.             if ($event->getRequest()->attributes->has('_template')) {
  112.                 $template $event->getRequest()->attributes->get('_template');
  113.                 $this->eventDispatcher->addListener($template->getTemplate(), function (TemplateEvent $templateEvent) {
  114.                     // 管理画面のナビゲーションにtaba app のメニューを差し込みます。
  115.                     $taba $this->container->get(Constants::CONTAINER_KEY_NAME);
  116.                     if (!$taba->get(Constants::PLUGIN_CATEGORY_ID ".menu")) {
  117.                         $templateEvent->addSnippet('@Taba2FA/admin/snippet/nav_taba_secure.twig');
  118.                         $taba->set(Constants::PLUGIN_CATEGORY_ID ".menu",true);
  119.                     }
  120.                     $templateEvent->addSnippet('@Taba2FA/admin/snippet/nav.twig');
  121.                 });
  122.             }
  123.         }
  124.         
  125.         // 2段階認証 
  126.         // リクエスト取得
  127.         $request $event->getRequest();
  128.         // ルートの取得
  129.         $route $request->attributes->get('_route');
  130.         // 2段階認証画面にある画像ファイルは認証チェックしない
  131.         if ($route == Constants::ADMIN_BIND_PREFIX."logo"
  132.             || $route == Constants::ADMIN_BIND_PREFIX."poweredby") {
  133.             return;
  134.         }
  135.         // plugin_enable.ymlを利用し、無効化する機能 
  136.         if (UserConfig::getInstance()->get("plugin_enable") === true) {
  137.             // 認証画面の場合は、トップへリダイレクト
  138.             if ($route === Constants::ADMIN_BIND_PREFIX."auth")  {
  139.                 $url $this->container->get('router')->generate("admin_homepage"$parameters = array(), $referenceType UrlGeneratorInterface::ABSOLUTE_PATH);
  140.                 $event->setController(function() use ($url) {
  141.                     return new RedirectResponse($url$status=302);
  142.                 });
  143.             }
  144.             // ファイルがある場合かつ、無効の場合は、処理を終了する。
  145.             return;
  146.         } 
  147.         // メンバー情報取得
  148.         $Member $this->tokenStorage->getToken()->getUser();
  149.         // 2段階認証の設定を確認
  150.         if ($Member->getTaba2FA() && $Member->getTaba2FA()->isEnable()) {
  151.             // セッション情報をチェック
  152.             $date = new \DateTime();
  153.             $timestamp $date->format('U');
  154.             $session $request->getSession();
  155.             if (!$session->get(Constants::LAST_AUTH) || $session->get(Constants::LAST_AUTH) < $timestamp) {
  156.                 // 認証画面を除く
  157.                 if ($route != Constants::ADMIN_BIND_PREFIX."auth")  {
  158.                     // 認証画面へリダイレクト
  159.                     $url $this->container->get('router')->generate(Constants::ADMIN_BIND_PREFIX."auth"$parameters = array(), $referenceType UrlGeneratorInterface::ABSOLUTE_PATH);
  160.                     $event->setController(function() use ($url) {
  161.                         return new RedirectResponse($url$status=302);
  162.                     });
  163.                 }
  164.             }
  165.         }
  166.     }
  167. }