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

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 Enterprise License (PEL)
  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 PEL
  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\FilterControllerEvent;
  23. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. class BruteforceProtectionListener implements EventSubscriberInterface
  26. {
  27.     use PimcoreContextAwareTrait;
  28.     /**
  29.      * @var BruteforceProtectionHandler
  30.      */
  31.     protected $handler;
  32.     /**
  33.      * @param BruteforceProtectionHandler $handler
  34.      */
  35.     public function __construct(BruteforceProtectionHandler $handler)
  36.     {
  37.         $this->handler $handler;
  38.     }
  39.     /**
  40.      * @inheritDoc
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             KernelEvents::CONTROLLER => 'onKernelController',
  46.             KernelEvents::EXCEPTION => 'onKernelException'
  47.         ];
  48.     }
  49.     public function onKernelController(FilterControllerEvent $event)
  50.     {
  51.         $request $event->getRequest();
  52.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_ADMIN)) {
  53.             return;
  54.         }
  55.         $callable $event->getController();
  56.         if (is_array($callable)) {
  57.             $controller $callable[0];
  58.             if ($controller instanceof BruteforceProtectedControllerInterface) {
  59.                 $this->handler->checkProtection($request->get('username'), $request);
  60.             }
  61.         }
  62.     }
  63.     public function onKernelException(GetResponseForExceptionEvent $event)
  64.     {
  65.         // handle brute force exception and return a plaintext response
  66.         $e $event->getException();
  67.         if ($e instanceof BruteforceProtectionException) {
  68.             $event->setResponse(new Response($e->getMessage()));
  69.         }
  70.     }
  71. }