Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 48df720

Browse files
authored
Merge pull request #358 from svycka/hotfix/issue-357
stop propagation only after MvcEvent::EVENT_DISPATCH_ERROR
2 parents 7344374 + a3afdac commit 48df720

File tree

7 files changed

+119
-50
lines changed

7 files changed

+119
-50
lines changed

src/ZfcRbac/Guard/AbstractGuard.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,13 @@ public function onResult(MvcEvent $event)
6868
403
6969
));
7070

71-
$event->stopPropagation(true);
72-
7371
$application = $event->getApplication();
7472
$eventManager = $application->getEventManager();
7573

76-
if (method_exists($eventManager, 'triggerEvent')) {
77-
// ZF3 EventManager
78-
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
79-
$eventManager->triggerEvent($event);
80-
} else {
81-
// ZF2 EventManager
82-
$eventManager->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);
83-
}
74+
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
75+
$eventManager->triggerEvent($event);
76+
77+
// just in case
78+
$event->stopPropagation(true);
8479
}
8580
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license.
17+
*/
18+
19+
namespace ZfcRbacTest\Asset;
20+
21+
use Zend\Mvc\MvcEvent;
22+
use ZfcRbac\Guard\AbstractGuard;
23+
24+
class DummyGuard extends AbstractGuard
25+
{
26+
/**
27+
* @param MvcEvent $event
28+
*
29+
* @return bool
30+
*/
31+
public function isGranted(MvcEvent $event)
32+
{
33+
return false;
34+
}
35+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/*
3+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14+
*
15+
* This software consists of voluntary contributions made by many individuals
16+
* and is licensed under the MIT license.
17+
*/
18+
19+
namespace ZfcRbacTest\Guard;
20+
21+
use Zend\EventManager\EventManager;
22+
use Zend\Mvc\Application;
23+
use Zend\Mvc\MvcEvent;
24+
use ZfcRbacTest\Asset\DummyGuard;
25+
26+
/**
27+
* @covers \ZfcRbac\Guard\AbstractGuard
28+
* @covers \ZfcRbac\Guard\ControllerGuard
29+
*/
30+
class AbstractGuardTest extends \PHPUnit_Framework_TestCase
31+
{
32+
public function testDoesNotLimitDispatchErrorEventToOnlyOneListener()
33+
{
34+
$eventManager = new EventManager();
35+
$application = $this->prophesize(Application::class);
36+
$application->getEventManager()->willReturn($eventManager);
37+
38+
$event = new MvcEvent();
39+
$event->setApplication($application->reveal());
40+
41+
$guard = new DummyGuard();
42+
$guard->attach($eventManager);
43+
44+
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function (MvcEvent $event) {
45+
$event->setParam('first-listener', true);
46+
});
47+
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function (MvcEvent $event) {
48+
$event->setParam('second-listener', true);
49+
});
50+
51+
// attach listener with lower priority than DummyGuard
52+
$eventManager->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $event) {
53+
$this->fail('should not be called, because guard should stop propagation');
54+
}, DummyGuard::EVENT_PRIORITY - 1);
55+
56+
$event->setName(MvcEvent::EVENT_ROUTE);
57+
$eventManager->triggerEvent($event);
58+
59+
$this->assertTrue($event->getParam('first-listener'));
60+
$this->assertTrue($event->getParam('second-listener'));
61+
$this->assertTrue($event->propagationIsStopped());
62+
}
63+
}

tests/ZfcRbacTest/Guard/ControllerGuardTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization()
488488
]);
489489

490490
$application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
491-
$eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
491+
$eventManager = $this->getMock('Zend\EventManager\EventManager');
492492

493493
$application->expects($this->once())
494494
->method('getEventManager')
495495
->will($this->returnValue($eventManager));
496496

497-
if (method_exists($eventManager, 'triggerEvent')) {
498-
$eventManager->expects($this->once())
499-
->method('triggerEvent')
500-
->with($event);
501-
} else {
502-
$eventManager->expects($this->once())
503-
->method('trigger')
504-
->with(MvcEvent::EVENT_DISPATCH_ERROR);
505-
}
497+
$eventManager->expects($this->once())
498+
->method('triggerEvent')
499+
->with($event);
506500

507501
$routeMatch->setParam('controller', 'MyController');
508502
$routeMatch->setParam('action', 'delete');

tests/ZfcRbacTest/Guard/ControllerPermissionsGuardTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,21 +475,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization()
475475
]);
476476

477477
$application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
478-
$eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
478+
$eventManager = $this->getMock('Zend\EventManager\EventManager');
479479

480480
$application->expects($this->once())
481481
->method('getEventManager')
482482
->will($this->returnValue($eventManager));
483483

484-
if (method_exists($eventManager, 'triggerEvent')) {
485-
$eventManager->expects($this->once())
486-
->method('triggerEvent')
487-
->with($event);
488-
} else {
489-
$eventManager->expects($this->once())
490-
->method('trigger')
491-
->with(MvcEvent::EVENT_DISPATCH_ERROR);
492-
}
484+
$eventManager->expects($this->once())
485+
->method('triggerEvent')
486+
->with($event);
493487

494488
$event->setRouteMatch($routeMatch);
495489
$event->setApplication($application);

tests/ZfcRbacTest/Guard/RouteGuardTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,15 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization()
433433
$routeMatch = $this->createRouteMatch();
434434

435435
$application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
436-
$eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
436+
$eventManager = $this->getMock('Zend\EventManager\EventManager');
437437

438438
$application->expects($this->once())
439439
->method('getEventManager')
440440
->will($this->returnValue($eventManager));
441441

442-
if (method_exists($eventManager, 'triggerEvent')) {
443-
$eventManager->expects($this->once())
444-
->method('triggerEvent')
445-
->with($event);
446-
} else {
447-
$eventManager->expects($this->once())
448-
->method('trigger')
449-
->with(MvcEvent::EVENT_DISPATCH_ERROR);
450-
}
442+
$eventManager->expects($this->once())
443+
->method('triggerEvent')
444+
->with($event);
451445

452446
$routeMatch->setMatchedRouteName('adminRoute');
453447
$event->setRouteMatch($routeMatch);

tests/ZfcRbacTest/Guard/RoutePermissionsGuardTest.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public function testProperlyFillEventOnAuthorization()
410410

411411
public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization()
412412
{
413-
$eventManager = $this->getMock('Zend\EventManager\EventManagerInterface');
413+
$eventManager = $this->getMock('Zend\EventManager\EventManager');
414414

415415
$application = $this->getMock('Zend\Mvc\Application', [], [], '', false);
416416
$application->expects($this->once())
@@ -424,15 +424,9 @@ public function testProperlySetUnauthorizedAndTriggerEventOnUnauthorization()
424424
$event->setRouteMatch($routeMatch);
425425
$event->setApplication($application);
426426

427-
if (method_exists($eventManager, 'triggerEvent')) {
428-
$eventManager->expects($this->once())
429-
->method('triggerEvent')
430-
->with($event);
431-
} else {
432-
$eventManager->expects($this->once())
433-
->method('trigger')
434-
->with(MvcEvent::EVENT_DISPATCH_ERROR);
435-
}
427+
$eventManager->expects($this->once())
428+
->method('triggerEvent')
429+
->with($event);
436430

437431
$authorizationService = $this->getMock('ZfcRbac\Service\AuthorizationServiceInterface', [], [], '', false);
438432
$authorizationService->expects($this->once())

0 commit comments

Comments
 (0)