-
-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request listener to prevent access to profiler routes
- Loading branch information
1 parent
661c57f
commit aa21787
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Bolt\EventListener; | ||
|
||
use Bolt\AccessControl\Token\Token; | ||
use Bolt\Request\ProfilerAwareTrait; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* Symfony Profiler listener. | ||
* | ||
* @author Gawain Lynch <gawain.lynch@gmail.com> | ||
*/ | ||
class ProfilerListener implements EventSubscriberInterface | ||
{ | ||
use ProfilerAwareTrait; | ||
|
||
/** @var SessionInterface */ | ||
private $session; | ||
/** @var bool */ | ||
private $debug; | ||
/** @var bool */ | ||
private $debugLoggedOff; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param SessionInterface $session | ||
* @param bool $debug | ||
* @param bool $debugLoggedOff | ||
*/ | ||
public function __construct(SessionInterface $session, $debug, $debugLoggedOff) | ||
{ | ||
$this->session = $session; | ||
$this->debug = $debug; | ||
$this->debugLoggedOff = $debugLoggedOff; | ||
} | ||
|
||
/** | ||
* Request listener to prevent access to profiler routes when debugging is | ||
* not enabled, or the user is logged off & debugging is not configured to | ||
* show when logged off. | ||
* | ||
* @param GetResponseEvent $event | ||
*/ | ||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
$request = $event->getRequest(); | ||
if (!$this->isProfilerRequest($request)) { | ||
return; | ||
} | ||
|
||
$token = $this->session->isStarted() ? $this->session->get('authentication') : null; | ||
if ($this->debug && ($token instanceof Token || $this->debugLoggedOff)) { | ||
return; | ||
} | ||
|
||
throw new NotFoundHttpException(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
KernelEvents::REQUEST => ['onKernelRequest'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters