diff --git a/src/Symfony/Component/Workflow/EventListener/GuardListener.php b/src/Symfony/Component/Workflow/EventListener/GuardListener.php index 20ba04c007..4d1065299d 100644 --- a/src/Symfony/Component/Workflow/EventListener/GuardListener.php +++ b/src/Symfony/Component/Workflow/EventListener/GuardListener.php @@ -16,6 +16,7 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; use Symfony\Component\Workflow\Event\GuardEvent; +use Symfony\Component\Workflow\Exception\InvalidTokenConfigurationException; /** * @author Grégoire Pineau @@ -55,6 +56,10 @@ class GuardListener { $token = $this->tokenStorage->getToken(); + if (null === $token) { + throw new InvalidTokenConfigurationException(sprintf('There are no tokens available for workflow %s.', $event->getWorkflowName())); + } + if (null !== $this->roleHierarchy) { $roles = $this->roleHierarchy->getReachableRoles($token->getRoles()); } else { diff --git a/src/Symfony/Component/Workflow/Exception/InvalidTokenConfigurationException.php b/src/Symfony/Component/Workflow/Exception/InvalidTokenConfigurationException.php new file mode 100644 index 0000000000..681d7a8bba --- /dev/null +++ b/src/Symfony/Component/Workflow/Exception/InvalidTokenConfigurationException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Workflow\Exception; + +/** + * Thrown by GuardListener when there is no token set, but guards are placed on a transition. + * + * @author Matt Johnson + */ +class InvalidTokenConfigurationException extends LogicException implements ExceptionInterface +{ +} diff --git a/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php b/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php index b46ee9092c..741dcf0930 100644 --- a/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php +++ b/src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php @@ -69,6 +69,18 @@ class GuardListenerTest extends TestCase $this->assertTrue($event->isBlocked()); } + /** + * @expectedException \Symfony\Component\Workflow\Exception\InvalidTokenConfigurationException + * @expectedExceptionMessage There are no tokens available for workflow unnamed. + */ + public function testWithNoTokensInTokenStorage() + { + $event = $this->createEvent(); + $this->tokenStorage->setToken(null); + + $this->listener->onTransition($event, 'event_name_a'); + } + private function createEvent() { $subject = new \stdClass();