Skip to content

Commit b044ffb

Browse files
committed
Added support for guards when advancing workflow from a command
1 parent be7751a commit b044ffb

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Symfony/Component/Workflow/EventListener/GuardListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Workflow\EventListener;
1313

1414
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
15+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1516
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1617
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1718
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
@@ -55,6 +56,11 @@ private function getVariables(GuardEvent $event)
5556
{
5657
$token = $this->tokenStorage->getToken();
5758

59+
if($token == null) {
60+
$token = new AnonymousToken('secret','anon',[]);
61+
$this->tokenStorage->setToken($token);
62+
}
63+
5864
if (null !== $this->roleHierarchy) {
5965
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
6066
} else {

src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ public function testWithSupportedEventAndAccept()
6969
$this->assertTrue($event->isBlocked());
7070
}
7171

72+
public function testWithNoTokenStorage()
73+
{
74+
$event = $this->createEvent();
75+
$this->tokenStorage = null;
76+
77+
$this->listener->onTransition($event, 'event_name_a');
78+
79+
$this->assertFalse($event->isBlocked());
80+
81+
$this->listener->onTransition($event, 'event_name_b');
82+
83+
$this->assertTrue($event->isBlocked());
84+
}
85+
7286
private function createEvent()
7387
{
7488
$subject = new \stdClass();

0 commit comments

Comments
 (0)