Skip to content

Commit

Permalink
Merge pull request #1 from michalbundyra/failed-routing-is-granted
Browse files Browse the repository at this point in the history
Hotfix: isGranted for failed routing
  • Loading branch information
michalbundyra committed Jan 24, 2020
2 parents 0b8c353 + cf25e0f commit 8ff1655
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/LaminasRbac.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function isGranted(string $role, ServerRequestInterface $request) : bool
));
}

// No matching route. Everyone can access.
if ($routeResult->isFailure()) {
return true;
}

$routeName = $routeResult->getMatchedRouteName();
if (null !== $this->assertion) {
$this->assertion->setRequest($request);
Expand Down
51 changes: 35 additions & 16 deletions test/LaminasRbacTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Mezzio\Authorization\Exception;
use Mezzio\Authorization\Rbac\LaminasRbac;
use Mezzio\Authorization\Rbac\LaminasRbacAssertionInterface;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
Expand Down Expand Up @@ -61,12 +62,10 @@ public function testIsGrantedWithoutAssertion()
$this->rbac->isGranted('foo', 'home', null)->willReturn(true);
$laminasRbac = new LaminasRbac($this->rbac->reveal());

$routeResult = $this->prophesize(RouteResult::class);
$routeResult->getMatchedRouteName()->willReturn('home');
$routeResult = $this->getSuccessRouteResult('home');

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(RouteResult::class, false)
->willReturn($routeResult->reveal());
$request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

$result = $laminasRbac->isGranted('foo', $request->reveal());
$this->assertTrue($result);
Expand All @@ -77,25 +76,21 @@ public function testIsNotGrantedWithoutAssertion()
$this->rbac->isGranted('foo', 'home', null)->willReturn(false);
$laminasRbac = new LaminasRbac($this->rbac->reveal());

$routeResult = $this->prophesize(RouteResult::class);
$routeResult->getMatchedRouteName()->willReturn('home');
$routeResult = $this->getSuccessRouteResult('home');

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(RouteResult::class, false)
->willReturn($routeResult->reveal());
$request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

$result = $laminasRbac->isGranted('foo', $request->reveal());
$this->assertFalse($result);
}

public function testIsGrantedWitAssertion()
{
$routeResult = $this->prophesize(RouteResult::class);
$routeResult->getMatchedRouteName()->willReturn('home');
$routeResult = $this->getSuccessRouteResult('home');

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(RouteResult::class, false)
->willReturn($routeResult->reveal());
$request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

$this->rbac->isGranted('foo', 'home', $this->assertion->reveal())->willReturn(true);

Expand All @@ -108,12 +103,10 @@ public function testIsGrantedWitAssertion()

public function testIsNotGrantedWitAssertion()
{
$routeResult = $this->prophesize(RouteResult::class);
$routeResult->getMatchedRouteName()->willReturn('home');
$routeResult = $this->getSuccessRouteResult('home');

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(RouteResult::class, false)
->willReturn($routeResult->reveal());
$request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

$this->rbac->isGranted('foo', 'home', $this->assertion->reveal())->willReturn(false);

Expand All @@ -123,4 +116,30 @@ public function testIsNotGrantedWitAssertion()
$this->assertFalse($result);
$this->assertion->setRequest($request->reveal())->shouldBeCalled();
}

public function testIsGrantedWithFailedRouting()
{
$routeResult = $this->getFailureRouteResult(Route::HTTP_METHOD_ANY);

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute(RouteResult::class, false)->willReturn($routeResult);

$laminasRbac = new LaminasRbac($this->rbac->reveal());

$result = $laminasRbac->isGranted('foo', $request->reveal());
$this->assertTrue($result);
}

private function getSuccessRouteResult(string $routeName): RouteResult
{
$route = $this->prophesize(Route::class);
$route->getName()->willReturn($routeName);

return RouteResult::fromRoute($route->reveal());
}

private function getFailureRouteResult(?array $methods): RouteResult
{
return RouteResult::fromRouteFailure($methods);
}
}

0 comments on commit 8ff1655

Please sign in to comment.