Permalink
Browse files

EZP-29708: REST API returns the list of all SiteAccesses when invalid…

… X-Siteaccess header value is set
  • Loading branch information...
glye committed Oct 11, 2018
1 parent 36776b9 commit 1551723ec134878a4cb598bfc5d900ba6164117a
@@ -43,14 +43,20 @@ class ConsoleCommandListener implements EventSubscriberInterface, SiteAccessAwar
*/
private $siteAccess;
/**
* @var bool
*/
private $debug;
/**
* ConsoleCommandListener constructor.
*/
public function __construct($defaultSiteAccessName, array $siteAccessList, EventDispatcherInterface $eventDispatcher)
public function __construct($defaultSiteAccessName, array $siteAccessList, EventDispatcherInterface $eventDispatcher, $debug = false)
{
$this->defaultSiteAccessName = $defaultSiteAccessName;
$this->siteAccessList = $siteAccessList;
$this->eventDispatcher = $eventDispatcher;
$this->debug = $debug;
}
public static function getSubscribedEvents()
@@ -68,7 +74,7 @@ public function onConsoleCommand(ConsoleCommandEvent $event)
$this->siteAccess->matchingType = 'cli';
if (!in_array($this->siteAccess->name, $this->siteAccessList)) {
throw new InvalidSiteAccessException($this->siteAccess->name, $this->siteAccessList, $this->siteAccess->matchingType);
throw new InvalidSiteAccessException($this->siteAccess->name, $this->siteAccessList, $this->siteAccess->matchingType, $this->debug);
}
$this->eventDispatcher->dispatch(MVCEvents::CONFIG_SCOPE_CHANGE, new ScopeChangeEvent($this->siteAccess));
@@ -78,4 +84,9 @@ public function setSiteAccess(SiteAccess $siteAccess = null)
{
$this->siteAccess = $siteAccess;
}
public function setDebug($debug = false)
{
$this->debug = $debug;
}
}
@@ -83,6 +83,7 @@ services:
- "%ezpublish.siteaccess.match_config%"
- "%ezpublish.siteaccess.list%"
- "%ezpublish.siteaccess.class%"
- "%kernel.debug%"
ezpublish.siteaccess_listener:
class: "%ezpublish.siteaccess_listener.class%"
@@ -63,7 +63,11 @@ services:
ezpublish.console_event_listener:
class: "%ezpublish.console_event_listener.class%"
arguments: ["%ezpublish.siteaccess.default%", "%ezpublish.siteaccess.list%", "@event_dispatcher"]
arguments:
- "%ezpublish.siteaccess.default%"
- "%ezpublish.siteaccess.list%"
- "@event_dispatcher"
- "%kernel.debug%"
calls:
- [setSiteAccess, ["@ezpublish.siteaccess"]]
tags:
@@ -83,13 +83,29 @@ public function testGetSubscribedEvents()
/**
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foo', matched by .+\. Valid siteaccesses are/
*/
public function testInvalidSiteAccess()
public function testInvalidSiteAccessDev()
{
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'foo'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(true);
$this->listener->onConsoleCommand($event);
}
/**
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foo', matched by .+\.$/
*/
public function testInvalidSiteAccessProd()
{
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'foo'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(false);
$this->listener->onConsoleCommand($event);
}
@@ -19,9 +19,15 @@ class InvalidSiteAccessException extends RuntimeException
* @param string $siteAccess The invalid siteaccess
* @param array $siteAccessList All valid siteaccesses, as a regular array
* @param string $matchType How $siteAccess was matched
* @param bool $debug If true, Symfony environment is a debug one (like 'dev')
*/
public function __construct($siteAccess, array $siteAccessList, $matchType)
public function __construct($siteAccess, array $siteAccessList, $matchType, $debug = false)
{
parent::__construct("Invalid siteaccess '$siteAccess', matched by $matchType. Valid siteaccesses are " . implode(', ', $siteAccessList));
$message = "Invalid siteaccess '$siteAccess', matched by $matchType.";
if ($debug) {
$message .= ' Valid siteaccesses are ' . implode(', ', $siteAccessList);
}
parent::__construct($message);
}
}
@@ -84,6 +84,11 @@ class Router implements SiteAccessRouterInterface, SiteAccessAware
*/
protected $request;
/**
* @var bool
*/
protected $debug;
/**
* Constructor.
*
@@ -93,8 +98,9 @@ class Router implements SiteAccessRouterInterface, SiteAccessAware
* @param array $siteAccessesConfiguration
* @param array $siteAccessList
* @param string|null $siteAccessClass
* @param bool $debug
*/
public function __construct(MatcherBuilderInterface $matcherBuilder, LoggerInterface $logger, $defaultSiteAccess, array $siteAccessesConfiguration, array $siteAccessList, $siteAccessClass = null)
public function __construct(MatcherBuilderInterface $matcherBuilder, LoggerInterface $logger, $defaultSiteAccess, array $siteAccessesConfiguration, array $siteAccessList, $siteAccessClass = null, $debug = false)
{
$this->matcherBuilder = $matcherBuilder;
$this->logger = $logger;
@@ -103,6 +109,7 @@ public function __construct(MatcherBuilderInterface $matcherBuilder, LoggerInter
$this->siteAccessList = array_fill_keys($siteAccessList, true);
$this->siteAccessClass = $siteAccessClass ?: 'eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess';
$this->request = new SimplifiedRequest();
$this->debug = $debug;
}
/**
@@ -139,7 +146,7 @@ public function match(SimplifiedRequest $request)
$siteaccessName = $request->headers['x-siteaccess'][0];
if (!isset($this->siteAccessList[$siteaccessName])) {
unset($this->siteAccess);
throw new InvalidSiteAccessException($siteaccessName, array_keys($this->siteAccessList), 'X-Siteaccess request header');
throw new InvalidSiteAccessException($siteaccessName, array_keys($this->siteAccessList), 'X-Siteaccess request header', $this->debug);
}
$this->siteAccess->name = $siteaccessName;
@@ -153,7 +160,7 @@ public function match(SimplifiedRequest $request)
if ($siteaccessEnvName !== false) {
if (!isset($this->siteAccessList[$siteaccessEnvName])) {
unset($this->siteAccess);
throw new InvalidSiteAccessException($siteaccessEnvName, array_keys($this->siteAccessList), 'EZPUBLISH_SITEACCESS Environment variable');
throw new InvalidSiteAccessException($siteaccessEnvName, array_keys($this->siteAccessList), 'EZPUBLISH_SITEACCESS Environment variable', $this->debug);
}
$this->siteAccess->name = $siteaccessEnvName;
@@ -34,7 +34,12 @@ protected function tearDown()
parent::tearDown();
}
public function testConstruct()
public function testConstructDebug()
{
return $this->testConstruct(true);
}
public function testConstruct($debug = false)
{
return new Router(
$this->matcherBuilder,
@@ -73,7 +78,9 @@ public function testConstruct()
),
),
),
array('first_sa', 'second_sa', 'third_sa', 'fourth_sa', 'headerbased_sa', 'fr_eng', 'fr_us')
array('first_sa', 'second_sa', 'third_sa', 'fourth_sa', 'headerbased_sa', 'fr_eng', 'fr_us'),
null,
$debug
);
}
@@ -92,11 +99,24 @@ public function testMatch(SimplifiedRequest $request, $siteAccess, Router $route
$router->setSiteAccess();
}
/**
* @depends testConstructDebug
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foobar_sa', matched by .+\. Valid siteaccesses are/
*/
public function testMatchWithDevEnvFail(Router $router)
{
$saName = 'foobar_sa';
putenv("EZPUBLISH_SITEACCESS=$saName");
$router->match(new SimplifiedRequest());
}
/**
* @depends testConstruct
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foobar_sa', matched by .+\.$/
*/
public function testMatchWithEnvFail(Router $router)
public function testMatchWithProdEnvFail(Router $router)
{
$saName = 'foobar_sa';
putenv("EZPUBLISH_SITEACCESS=$saName");

0 comments on commit 1551723

Please sign in to comment.