From b044ffb4a23d109d62724e7c62855f4d03d72a32 Mon Sep 17 00:00:00 2001 From: GDIBass Date: Wed, 16 Aug 2017 23:21:24 +0000 Subject: [PATCH] Added support for guards when advancing workflow from a command --- .../Workflow/EventListener/GuardListener.php | 6 ++++++ .../Tests/EventListener/GuardListenerTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/Workflow/EventListener/GuardListener.php b/src/Symfony/Component/Workflow/EventListener/GuardListener.php index 20ba04c007..d7d9d2edc4 100644 --- a/src/Symfony/Component/Workflow/EventListener/GuardListener.php +++ b/src/Symfony/Component/Workflow/EventListener/GuardListener.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Workflow\EventListener; use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; +use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; @@ -55,6 +56,11 @@ class GuardListener { $token = $this->tokenStorage->getToken(); + if($token == null) { + $token = new AnonymousToken('secret','anon',[]); + $this->tokenStorage->setToken($token); + } + if (null !== $this->roleHierarchy) { $roles = $this->roleHierarchy->getReachableRoles($token->getRoles()); } else { diff --git a/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php b/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php index b46ee9092c..50481654e5 100644 --- a/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php +++ b/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php @@ -69,6 +69,20 @@ class GuardListenerTest extends TestCase $this->assertTrue($event->isBlocked()); } + public function testWithNoTokenStorage() + { + $event = $this->createEvent(); + $this->tokenStorage = null; + + $this->listener->onTransition($event, 'event_name_a'); + + $this->assertFalse($event->isBlocked()); + + $this->listener->onTransition($event, 'event_name_b'); + + $this->assertTrue($event->isBlocked()); + } + private function createEvent() { $subject = new \stdClass();