This repository was archived by the owner on May 14, 2018. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +21
-6
lines changed
tests/BjyAuthorizeTest/Guard Expand file tree Collapse file tree 4 files changed +21
-6
lines changed Original file line number Diff line number Diff line change 88
99namespace BjyAuthorize \Guard ;
1010
11+ use BjyAuthorize \Exception \UnAuthorizedException ;
1112use BjyAuthorize \Provider \Rule \ProviderInterface as RuleProviderInterface ;
1213use 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 );
Original file line number Diff line number Diff line change 88
99namespace BjyAuthorize \Guard ;
1010
11+ use BjyAuthorize \Exception \UnAuthorizedException ;
1112use BjyAuthorize \Provider \Rule \ProviderInterface as RuleProviderInterface ;
1213use 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 ();
Original file line number Diff line number Diff 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 ()
Original file line number Diff line number Diff 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 ()
You can’t perform that action at this time.
0 commit comments