Skip to content

Commit

Permalink
RateLimitSubscriber: check if user is logged in (#395)
Browse files Browse the repository at this point in the history
Bug: T286048
  • Loading branch information
MusikAnimal committed Feb 8, 2022
1 parent 37dab4d commit 02d0616
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/EventSubscriber/RateLimitSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Krinkle\Intuition\Intuition;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
Expand All @@ -31,6 +32,9 @@ class RateLimitSubscriber implements EventSubscriberInterface {
/** @var HttpClientInterface */
protected $client;

/** @var SessionInterface */
protected $session;

/** @var int */
protected $rateLimit;

Expand All @@ -47,12 +51,14 @@ public function __construct(
Intuition $intuition,
CacheItemPoolInterface $cache,
HttpClientInterface $client,
SessionInterface $session,
int $rateLimit,
int $rateDuration
) {
$this->intuition = $intuition;
$this->cache = $cache;
$this->client = $client;
$this->session = $session;
$this->rateLimit = $rateLimit;
$this->rateDuration = $rateDuration;
}
Expand All @@ -75,6 +81,7 @@ public function onKernelController( ControllerEvent $event ): void {
$controller = $event->getController();
$action = null;
$request = $event->getRequest();
$loggedIn = (bool)$this->session->get( 'logged_in_user' );

// when a controller class defines multiple action methods, the controller
// is returned as [$controllerInstance, 'methodName']
Expand All @@ -83,7 +90,9 @@ public function onKernelController( ControllerEvent $event ): void {
}

// Abort if rate limitations are disabled or we're not exporting a book.
if ( $this->rateLimit + $this->rateDuration === 0 || $action !== 'home' || !$request->get( 'page' ) ) {
if ( $loggedIn || $this->rateLimit + $this->rateDuration === 0 ||
$action !== 'home' || !$request->get( 'page' )
) {
return;
}

Expand Down

0 comments on commit 02d0616

Please sign in to comment.