vendor/pimcore/pimcore/bundles/AdminBundle/EventListener/BruteforceProtectionListener.php line 60

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\EventListener;
  15. use Pimcore\Bundle\AdminBundle\Controller\BruteforceProtectedControllerInterface;
  16. use Pimcore\Bundle\AdminBundle\Security\BruteforceProtectionHandler;
  17. use Pimcore\Bundle\AdminBundle\Security\Exception\BruteforceProtectionException;
  18. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  19. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  23. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. /**
  26.  * @internal
  27.  */
  28. class BruteforceProtectionListener implements EventSubscriberInterface
  29. {
  30.     use PimcoreContextAwareTrait;
  31.     /**
  32.      * @var BruteforceProtectionHandler
  33.      */
  34.     protected $handler;
  35.     /**
  36.      * @param BruteforceProtectionHandler $handler
  37.      */
  38.     public function __construct(BruteforceProtectionHandler $handler)
  39.     {
  40.         $this->handler $handler;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::CONTROLLER => 'onKernelController',
  49.             KernelEvents::EXCEPTION => 'onKernelException',
  50.         ];
  51.     }
  52.     public function onKernelController(ControllerEvent $event)
  53.     {
  54.         $request $event->getRequest();
  55.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_ADMIN)) {
  56.             return;
  57.         }
  58.         $callable $event->getController();
  59.         if (is_array($callable)) {
  60.             $controller $callable[0];
  61.             if ($controller instanceof BruteforceProtectedControllerInterface) {
  62.                 $this->handler->checkProtection($request->get('username'), $request);
  63.             }
  64.         }
  65.     }
  66.     public function onKernelException(ExceptionEvent $event)
  67.     {
  68.         // handle brute force exception and return a plaintext response
  69.         $e $event->getThrowable();
  70.         if ($e instanceof BruteforceProtectionException) {
  71.             $event->setResponse(new Response($e->getMessage()));
  72.         }
  73.     }
  74. }