diff --git a/src/BjyAuthorize/Guard/Controller.php b/src/BjyAuthorize/Guard/Controller.php index 61eb1d6..dd5a8fc 100644 --- a/src/BjyAuthorize/Guard/Controller.php +++ b/src/BjyAuthorize/Guard/Controller.php @@ -8,6 +8,7 @@ namespace BjyAuthorize\Guard; +use BjyAuthorize\Exception\UnAuthorizedException; use BjyAuthorize\Provider\Rule\ProviderInterface as RuleProviderInterface; use BjyAuthorize\Provider\Resource\ProviderInterface as ResourceProviderInterface; @@ -164,6 +165,9 @@ public function onDispatch(MvcEvent $event) $event->setParam('controller', $controller); $event->setParam('action', $action); + $errorMessage = sprintf("You are not authorized to access %s:%s", $controller, $action); + $event->setParam('exception', new UnAuthorizedException($errorMessage)); + /* @var $app \Zend\Mvc\ApplicationInterface */ $app = $event->getTarget(); $app->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event); diff --git a/src/BjyAuthorize/Guard/Route.php b/src/BjyAuthorize/Guard/Route.php index c49dabc..26cdc27 100644 --- a/src/BjyAuthorize/Guard/Route.php +++ b/src/BjyAuthorize/Guard/Route.php @@ -8,6 +8,7 @@ namespace BjyAuthorize\Guard; +use BjyAuthorize\Exception\UnAuthorizedException; use BjyAuthorize\Provider\Rule\ProviderInterface as RuleProviderInterface; use BjyAuthorize\Provider\Resource\ProviderInterface as ResourceProviderInterface; @@ -129,6 +130,7 @@ public function onRoute(MvcEvent $event) $event->setError(static::ERROR); $event->setParam('route', $routeName); $event->setParam('identity', $service->getIdentity()); + $event->setParam('exception', new UnAuthorizedException('You are not authorized to access ' . $routeName)); /* @var $app \Zend\Mvc\Application */ $app = $event->getTarget(); diff --git a/tests/BjyAuthorizeTest/Guard/ControllerTest.php b/tests/BjyAuthorizeTest/Guard/ControllerTest.php index d134bf9..4a9d14e 100644 --- a/tests/BjyAuthorizeTest/Guard/ControllerTest.php +++ b/tests/BjyAuthorizeTest/Guard/ControllerTest.php @@ -209,10 +209,15 @@ public function testOnDispatchWithInvalidResource() ->method('isAllowed') ->will($this->returnValue(false)); $event->expects($this->once())->method('setError')->with(Controller::ERROR); - $event->expects($this->exactly(3))->method('setParam')->with( - $this->logicalOr('identity', 'controller', 'action'), - $this->logicalOr('admin', 'test-controller', 'test-action') + + $event->expects($this->at(4))->method('setParam')->with('identity', 'admin'); + $event->expects($this->at(5))->method('setParam')->with('controller', 'test-controller'); + $event->expects($this->at(6))->method('setParam')->with('action', 'test-action'); + $event->expects($this->at(7))->method('setParam')->with( + 'exception', + $this->isInstanceOf('BjyAuthorize\Exception\UnAuthorizedException') ); + $event ->getTarget() ->getEventManager() diff --git a/tests/BjyAuthorizeTest/Guard/RouteTest.php b/tests/BjyAuthorizeTest/Guard/RouteTest.php index 36fc26d..8735782 100644 --- a/tests/BjyAuthorizeTest/Guard/RouteTest.php +++ b/tests/BjyAuthorizeTest/Guard/RouteTest.php @@ -167,10 +167,14 @@ public function testOnRouteWithInvalidResource() ->method('isAllowed') ->will($this->returnValue(false)); $event->expects($this->once())->method('setError')->with(Route::ERROR); - $event->expects($this->exactly(2))->method('setParam')->with( - $this->logicalOr('identity', 'route'), - $this->logicalOr('admin', 'test-route') + + $event->expects($this->at(3))->method('setParam')->with('route', 'test-route'); + $event->expects($this->at(4))->method('setParam')->with('identity', 'admin'); + $event->expects($this->at(5))->method('setParam')->with( + 'exception', + $this->isInstanceOf('BjyAuthorize\Exception\UnAuthorizedException') ); + $event ->getTarget() ->getEventManager()