Skip to content
This repository was archived by the owner on May 14, 2018. It is now read-only.

Commit 02723bd

Browse files
committed
Merge pull request #163 from ftdebugger/master
Show exception details in console
2 parents cb47569 + deebca8 commit 02723bd

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/BjyAuthorize/Guard/Controller.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace BjyAuthorize\Guard;
1010

11+
use BjyAuthorize\Exception\UnAuthorizedException;
1112
use BjyAuthorize\Provider\Rule\ProviderInterface as RuleProviderInterface;
1213
use BjyAuthorize\Provider\Resource\ProviderInterface as ResourceProviderInterface;
1314

@@ -164,6 +165,9 @@ public function onDispatch(MvcEvent $event)
164165
$event->setParam('controller', $controller);
165166
$event->setParam('action', $action);
166167

168+
$errorMessage = sprintf("You are not authorized to access %s:%s", $controller, $action);
169+
$event->setParam('exception', new UnAuthorizedException($errorMessage));
170+
167171
/* @var $app \Zend\Mvc\ApplicationInterface */
168172
$app = $event->getTarget();
169173
$app->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);

src/BjyAuthorize/Guard/Route.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace BjyAuthorize\Guard;
1010

11+
use BjyAuthorize\Exception\UnAuthorizedException;
1112
use BjyAuthorize\Provider\Rule\ProviderInterface as RuleProviderInterface;
1213
use BjyAuthorize\Provider\Resource\ProviderInterface as ResourceProviderInterface;
1314

@@ -129,6 +130,7 @@ public function onRoute(MvcEvent $event)
129130
$event->setError(static::ERROR);
130131
$event->setParam('route', $routeName);
131132
$event->setParam('identity', $service->getIdentity());
133+
$event->setParam('exception', new UnAuthorizedException('You are not authorized to access ' . $routeName));
132134

133135
/* @var $app \Zend\Mvc\Application */
134136
$app = $event->getTarget();

tests/BjyAuthorizeTest/Guard/ControllerTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,15 @@ public function testOnDispatchWithInvalidResource()
209209
->method('isAllowed')
210210
->will($this->returnValue(false));
211211
$event->expects($this->once())->method('setError')->with(Controller::ERROR);
212-
$event->expects($this->exactly(3))->method('setParam')->with(
213-
$this->logicalOr('identity', 'controller', 'action'),
214-
$this->logicalOr('admin', 'test-controller', 'test-action')
212+
213+
$event->expects($this->at(4))->method('setParam')->with('identity', 'admin');
214+
$event->expects($this->at(5))->method('setParam')->with('controller', 'test-controller');
215+
$event->expects($this->at(6))->method('setParam')->with('action', 'test-action');
216+
$event->expects($this->at(7))->method('setParam')->with(
217+
'exception',
218+
$this->isInstanceOf('BjyAuthorize\Exception\UnAuthorizedException')
215219
);
220+
216221
$event
217222
->getTarget()
218223
->getEventManager()

tests/BjyAuthorizeTest/Guard/RouteTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,14 @@ public function testOnRouteWithInvalidResource()
167167
->method('isAllowed')
168168
->will($this->returnValue(false));
169169
$event->expects($this->once())->method('setError')->with(Route::ERROR);
170-
$event->expects($this->exactly(2))->method('setParam')->with(
171-
$this->logicalOr('identity', 'route'),
172-
$this->logicalOr('admin', 'test-route')
170+
171+
$event->expects($this->at(3))->method('setParam')->with('route', 'test-route');
172+
$event->expects($this->at(4))->method('setParam')->with('identity', 'admin');
173+
$event->expects($this->at(5))->method('setParam')->with(
174+
'exception',
175+
$this->isInstanceOf('BjyAuthorize\Exception\UnAuthorizedException')
173176
);
177+
174178
$event
175179
->getTarget()
176180
->getEventManager()

0 commit comments

Comments
 (0)