app/Plugin/AdminSecurity4/Event.php line 137

Open in your IDE?
  1. <?php
  2. namespace Plugin\AdminSecurity4;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Common\Constant;
  5. use Eccube\Common\EccubeConfig;
  6. use Eccube\Entity\Member;
  7. use Eccube\Repository\MemberRepository;
  8. use Eccube\Request\Context;
  9. use Eccube\Util\StringUtil;
  10. use Plugin\AdminSecurity4\Entity\Config;
  11. use Plugin\AdminSecurity4\Entity\LoginRecord;
  12. use Plugin\AdminSecurity4\Repository\ConfigRepository;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  16. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  17. use Symfony\Component\Security\Core\AuthenticationEvents;
  18. use Symfony\Component\Security\Core\Event\AuthenticationFailureEvent;
  19. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  20. use Symfony\Component\Security\Http\SecurityEvents;
  21. class Event implements EventSubscriberInterface
  22. {
  23.     private $em;
  24.     /**
  25.      * @var RequestStack
  26.      */
  27.     private $requestStack;
  28.     /**
  29.      * @var MemberRepository
  30.      */
  31.     private $memberRepository;
  32.     /**
  33.      * @var EccubeConfig
  34.      */
  35.     private $eccubeConfig;
  36.     /**
  37.      * @var Context
  38.      */
  39.     private $requestContext;
  40.     /**
  41.      * @var ConfigRepository
  42.      */
  43.     private $configRepository;
  44.     public function __construct(
  45.         EntityManagerInterface $em,
  46.         MemberRepository $memberRepository,
  47.         RequestStack $requestStack,
  48.         Context $requestContext,
  49.         Config $eccubeConfig,
  50.         ConfigRepository $configRepository
  51.     ) {
  52.         $this->em $em;
  53.         $this->requestStack $requestStack;
  54.         $this->memberRepository $memberRepository;
  55.         $this->eccubeConfig $eccubeConfig;
  56.         $this->requestContext $requestContext;
  57.         $this->configRepository $configRepository;
  58.     }
  59.     /**
  60.      * @return array
  61.      */
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  66.             AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
  67.             'kernel.request' => ['onKernelRequest'512],
  68.         ];
  69.     }
  70.     /**
  71.      * @param InteractiveLoginEvent $event
  72.      */
  73.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  74.     {
  75.         $request $event->getRequest();
  76.         $user $event
  77.             ->getAuthenticationToken()
  78.             ->getUser();
  79.         if ($user instanceof Member) {
  80.             $LoginRecord = new LoginRecord();
  81.             $LoginRecord
  82.                 ->setLoginUser($user)
  83.                 ->setUserName($user->getUsername())
  84.                 ->setSuccessFlg(Constant::ENABLED)
  85.                 ->setClientIp($request->getClientIp())
  86.             ;
  87.             $this->em->persist($LoginRecord);
  88.             $this->em->flush($LoginRecord);
  89.         }
  90.     }
  91.     /**
  92.      * @param AuthenticationFailureEvent $event
  93.      */
  94.     public function onAuthenticationFailure(AuthenticationFailureEvent $event)
  95.     {
  96.         if (!$this->requestContext->isAdmin()) {
  97.             return;
  98.         }
  99.         $request $this->requestStack->getCurrentRequest();
  100.         $userName $event->getAuthenticationToken()->getUsername();
  101.         $Member null;
  102.         if ($userName) {
  103.             $Member $this->memberRepository->findOneBy(['login_id' => $userName]);
  104.         }
  105.         $LoginRecord = new LoginRecord();
  106.         $LoginRecord
  107.             ->setLoginUser($Member)
  108.             ->setUserName($userName)
  109.             ->setSuccessFlg(Constant::DISABLED)
  110.             ->setClientIp($request->getClientIp())
  111.         ;
  112.         $this->em->persist($LoginRecord);
  113.         $this->em->flush($LoginRecord);
  114.     }
  115.     /**
  116.      * @param GetResponseEvent $event
  117.      */
  118.     public function onKernelRequest(GetResponseEvent $event)
  119.     {
  120.         if (!$event->isMasterRequest()) {
  121.             return;
  122.         }
  123.         $Config $this->configRepository->get();
  124.         if (!$Config) {
  125.             return;
  126.         }
  127.         /* @var $Config Config */
  128.         $denyHosts array_filter(explode("\n"StringUtil::convertLineFeed($Config->getAdminDenyHosts())), function ($var) {
  129.             return StringUtil::isNotBlank($var);
  130.         });
  131.         if (empty($denyHosts)) {
  132.             return;
  133.         }
  134.         if ($this->requestContext->isAdmin()) {
  135.             if (array_search($event->getRequest()->getClientIp(), $denyHosts) !== false) {
  136.                 throw new AccessDeniedHttpException();
  137.             }
  138.         }
  139.     }
  140. }