src/Security/BruteforceProtectionListener.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Security\BruteforceProtectedControllerInterface;
  4. use App\Security\BruteforceProtectionHandler;
  5. use Pimcore\Bundle\AdminBundle\Security\Exception\BruteforceProtectionException;
  6. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  7. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Pimcore\Log\ApplicationLogger;
  14. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  15. /**
  16.  * @internal
  17.  */
  18. class BruteforceProtectionListener implements EventSubscriberInterface
  19. {
  20.     use PimcoreContextAwareTrait;
  21.     /**
  22.      * @var BruteforceProtectionHandler
  23.      */
  24.     protected $handler;
  25.     /**
  26.      * @var ApplicationLogger 
  27.      */
  28.     private $logger;
  29.     private $authenticationUtils;
  30.     /**
  31.      * @param BruteforceProtectionHandler $handler
  32.      */
  33.     public function __construct(BruteforceProtectionHandler $handlerApplicationLogger $logger,AuthenticationUtils $authenticationUtils)
  34.     {
  35.         $this->handler $handler;
  36.         $this->logger $logger;
  37.         $this->authenticationUtils $authenticationUtils;
  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(ControllerEvent $event)
  50.     {
  51.         $request $event->getRequest();
  52.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  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($this->authenticationUtils->getLastUsername(), $request);
  60.             }
  61.         }
  62.     }
  63.     public function onKernelException(ExceptionEvent $event)
  64.     {
  65.         // handle brute force exception and return a plaintext response
  66.         $e $event->getThrowable();
  67.         if ($e instanceof BruteforceProtectionException) {
  68.             $this->logger->error('Error al verificar el usuario ' $e->getMessage());
  69.             $event->setResponse(new Response($e->getMessage()));
  70.         }
  71.     }
  72. }