Skip to content

[FrameworkBundle][Workflow] fix guard expressions #28018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $

foreach ($config['workflows'] as $name => $workflow) {
$type = $workflow['type'];
$workflowId = sprintf('%s.%s', $type, $name);

// Process Metadata (workflow + places (transition is done in the "create transition" block))
$metadataStoreDefinition = new Definition(Workflow\Metadata\InMemoryMetadataStore::class, array(array(), array(), null));
Expand All @@ -510,28 +511,53 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $

// Create transitions
$transitions = array();
$guardsConfiguration = array();
$transitionsMetadataDefinition = new Definition(\SplObjectStorage::class);
foreach ($workflow['transitions'] as $transition) {
// Global transition counter per workflow
$transitionCounter = 0;
foreach (array_values($workflow['transitions']) as $number => $transition) {
if ('workflow' === $type) {
$transitionDefinition = new Definition(Workflow\Transition::class, array($transition['name'], $transition['from'], $transition['to']));
$transitions[] = $transitionDefinition;
$transitionId = sprintf('%s.transition.%s', $workflowId, $transitionCounter++);
$container->setDefinition($transitionId, $transitionDefinition);
$transitions[] = new Reference($transitionId);

if ($transition['metadata']) {
$transitionsMetadataDefinition->addMethodCall('attach', array(
$transitionDefinition,
$transition['metadata'],
));
}

if (isset($transition['guard'])) {
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
$configuration->addArgument(new Reference($transitionId));
$configuration->addArgument($transition['guard']);
$eventName = sprintf('workflow.%s.guard.%s', $name, $transition['name']);
$guardsConfiguration[$eventName][] = $configuration;
}
} elseif ('state_machine' === $type) {
foreach ($transition['from'] as $from) {
foreach ($transition['to'] as $to) {
$transitionDefinition = new Definition(Workflow\Transition::class, array($transition['name'], $from, $to));
$transitions[] = $transitionDefinition;
$transitionId = sprintf('%s.transition.%s', $workflowId, $transitionCounter++);
$container->setDefinition($transitionId, $transitionDefinition);
$transitions[] = new Reference($transitionId);

if ($transition['metadata']) {
$transitionsMetadataDefinition->addMethodCall('attach', array(
$transitionDefinition,
$transition['metadata'],
));
}

if (isset($transition['guard'])) {
$configuration = new Definition(Workflow\EventListener\GuardExpression::class);
$configuration->addArgument(new Reference($transitionId));
$configuration->addArgument($transition['guard']);
$eventName = sprintf('workflow.%s.guard.%s', $name, $transition['name']);
$guardsConfiguration[$eventName][] = $configuration;
}
}
}
}
Expand All @@ -542,7 +568,6 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
$places = array_map(function (array $place) {
return $place['name'];
}, $workflow['places']);

// Create a Definition
$definitionDefinition = new Definition(Workflow\Definition::class);
$definitionDefinition->setPublic(false);
Expand All @@ -567,7 +592,7 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
}

// Create Workflow
$workflowId = sprintf('%s.%s', $type, $name);

$workflowDefinition = new ChildDefinition(sprintf('%s.abstract', $type));
$workflowDefinition->replaceArgument(0, new Reference(sprintf('%s.definition', $workflowId)));
if (isset($markingStoreDefinition)) {
Expand Down Expand Up @@ -605,12 +630,8 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
// Add Guard Listener
$guard = new Definition(Workflow\EventListener\GuardListener::class);
$guard->setPrivate(true);
$configuration = array();
foreach ($workflow['transitions'] as $transitionName => $config) {
if (!isset($config['guard'])) {
continue;
}

if ($guardsConfiguration) {
if (!class_exists(ExpressionLanguage::class)) {
throw new LogicException('Cannot guard workflows as the ExpressionLanguage component is not installed.');
}
Expand All @@ -619,13 +640,8 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
throw new LogicException('Cannot guard workflows as the Security component is not installed.');
}

$eventName = sprintf('workflow.%s.guard.%s', $name, $transitionName);
$guard->addTag('kernel.event_listener', array('event' => $eventName, 'method' => 'onTransition'));
$configuration[$eventName] = $config['guard'];
}
if ($configuration) {
$guard->setArguments(array(
$configuration,
$guardsConfiguration,
new Reference('workflow.security.expression_language'),
new Reference('security.token_storage'),
new Reference('security.authorization_checker'),
Expand All @@ -634,6 +650,10 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
new Reference('validator', ContainerInterface::NULL_ON_INVALID_REFERENCE),
));

foreach ($guardsConfiguration as $eventName => $config) {
$guard->addTag('kernel.event_listener', array('event' => $eventName, 'method' => 'onTransition'));
}

$container->setDefinition(sprintf('%s.listener.guard', $workflowId), $guard);
$container->setParameter('workflow.has_guard_listeners', true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
<xsd:sequence>
<xsd:element name="from" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="to" type="xsd:string" minOccurs="1" maxOccurs="unbounded" />
<xsd:element name="guard" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="metadata" type="metadata" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest;

$container->loadFromExtension('framework', array(
'workflows' => array(
'article' => array(
'type' => 'workflow',
'marking_store' => array(
'type' => 'multiple_state',
),
'supports' => array(
FrameworkExtensionTest::class,
),
'initial_place' => 'draft',
'places' => array(
'draft',
'wait_for_journalist',
'approved_by_journalist',
'wait_for_spellchecker',
'approved_by_spellchecker',
'published',
),
'transitions' => array(
'request_review' => array(
'from' => 'draft',
'to' => array('wait_for_journalist', 'wait_for_spellchecker'),
),
'journalist_approval' => array(
'from' => 'wait_for_journalist',
'to' => 'approved_by_journalist',
),
'spellchecker_approval' => array(
'from' => 'wait_for_spellchecker',
'to' => 'approved_by_spellchecker',
),
'publish' => array(
'from' => array('approved_by_journalist', 'approved_by_spellchecker'),
'to' => 'published',
'guard' => '!!true',
),
'publish_editor_in_chief' => array(
'name' => 'publish',
'from' => 'draft',
'to' => 'published',
'guard' => '!!false',
),
),
),
),
));
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:workflow name="article" type="workflow" initial-place="draft">
<framework:marking-store type="multiple_state">
<framework:argument>a</framework:argument>
<framework:argument>a</framework:argument>
</framework:marking-store>
<framework:support>Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest</framework:support>
<framework:place name="draft" />
<framework:place name="wait_for_journalist" />
<framework:place name="approved_by_journalist" />
<framework:place name="wait_for_spellchecker" />
<framework:place name="approved_by_spellchecker" />
<framework:place name="published" />
<framework:transition name="request_review">
<framework:from>draft</framework:from>
<framework:to>wait_for_journalist</framework:to>
<framework:to>wait_for_spellchecker</framework:to>
</framework:transition>
<framework:transition name="journalist_approval">
<framework:from>wait_for_journalist</framework:from>
<framework:to>approved_by_journalist</framework:to>
</framework:transition>
<framework:transition name="spellchecker_approval">
<framework:from>wait_for_spellchecker</framework:from>
<framework:to>approved_by_spellchecker</framework:to>
</framework:transition>
<framework:transition name="publish">
<framework:from>approved_by_journalist</framework:from>
<framework:from>approved_by_spellchecker</framework:from>
<framework:to>published</framework:to>
<framework:guard>!!true</framework:guard>
</framework:transition>
<framework:transition name="publish">
<framework:from>draft</framework:from>
<framework:to>published</framework:to>
<framework:guard>!!false</framework:guard>
</framework:transition>
</framework:workflow>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
framework:
workflows:
article:
type: workflow
marking_store:
type: multiple_state
supports:
- Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\FrameworkExtensionTest
initial_place: draft
places:
- draft
- wait_for_journalist
- approved_by_journalist
- wait_for_spellchecker
- approved_by_spellchecker
- published
transitions:
request_review:
from: [draft]
to: [wait_for_journalist, wait_for_spellchecker]
journalist_approval:
from: [wait_for_journalist]
to: [approved_by_journalist]
spellchecker_approval:
from: [wait_for_spellchecker]
to: [approved_by_spellchecker]
publish:
from: [approved_by_journalist, approved_by_spellchecker]
to: [published]
guard: "!!true"
publish_editor_in_chief:
name: publish
from: [draft]
to: [published]
guard: "!!false"
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,84 @@ public function testWorkflowMultipleTransitionsWithSameName()

$this->assertCount(5, $transitions);

$this->assertSame('request_review', $transitions[0]->getArgument(0));
$this->assertSame('journalist_approval', $transitions[1]->getArgument(0));
$this->assertSame('spellchecker_approval', $transitions[2]->getArgument(0));
$this->assertSame('publish', $transitions[3]->getArgument(0));
$this->assertSame('publish', $transitions[4]->getArgument(0));

$this->assertSame(array('approved_by_journalist', 'approved_by_spellchecker'), $transitions[3]->getArgument(1));
$this->assertSame(array('draft'), $transitions[4]->getArgument(1));
$this->assertSame('workflow.article.transition.0', (string) $transitions[0]);
$this->assertSame(array(
'request_review',
array(
'draft',
),
array(
'wait_for_journalist', 'wait_for_spellchecker',
),
), $container->getDefinition($transitions[0])->getArguments());

$this->assertSame('workflow.article.transition.1', (string) $transitions[1]);
$this->assertSame(array(
'journalist_approval',
array(
'wait_for_journalist',
),
array(
'approved_by_journalist',
),
), $container->getDefinition($transitions[1])->getArguments());

$this->assertSame('workflow.article.transition.2', (string) $transitions[2]);
$this->assertSame(array(
'spellchecker_approval',
array(
'wait_for_spellchecker',
),
array(
'approved_by_spellchecker',
),
), $container->getDefinition($transitions[2])->getArguments());

$this->assertSame('workflow.article.transition.3', (string) $transitions[3]);
$this->assertSame(array(
'publish',
array(
'approved_by_journalist',
'approved_by_spellchecker',
),
array(
'published',
),
), $container->getDefinition($transitions[3])->getArguments());

$this->assertSame('workflow.article.transition.4', (string) $transitions[4]);
$this->assertSame(array(
'publish',
array(
'draft',
),
array(
'published',
),
), $container->getDefinition($transitions[4])->getArguments());
}

public function testGuardExpressions()
{
$container = $this->createContainerFromFile('workflow_with_guard_expression');

$this->assertTrue($container->hasDefinition('workflow.article.listener.guard'), 'Workflow guard listener is registered as a service');
$this->assertTrue($container->hasParameter('workflow.has_guard_listeners'), 'Workflow guard listeners parameter exists');
$this->assertTrue(true === $container->getParameter('workflow.has_guard_listeners'), 'Workflow guard listeners parameter is enabled');
$guardDefinition = $container->getDefinition('workflow.article.listener.guard');
$this->assertSame(array(
array(
'event' => 'workflow.article.guard.publish',
'method' => 'onTransition',
),
), $guardDefinition->getTag('kernel.event_listener'));
$guardsConfiguration = $guardDefinition->getArgument(0);
$this->assertTrue(1 === \count($guardsConfiguration), 'Workflow guard configuration contains one element per transition name');
$transitionGuardExpressions = $guardsConfiguration['workflow.article.guard.publish'];
$this->assertSame('workflow.article.transition.3', (string) $transitionGuardExpressions[0]->getArgument(0));
$this->assertSame('!!true', $transitionGuardExpressions[0]->getArgument(1));
$this->assertSame('workflow.article.transition.4', (string) $transitionGuardExpressions[1]->getArgument(0));
$this->assertSame('!!false', $transitionGuardExpressions[1]->getArgument(1));
}

public function testWorkflowServicesCanBeEnabled()
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/Workflow/EventListener/GuardExpression.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Workflow\EventListener;

use Symfony\Component\Workflow\Transition;

class GuardExpression
{
private $transition;

private $expression;

public function getTransition(): Transition
{
return $this->transition;
}

public function getExpression(): string
{
return $this->expression;
}

public function __construct(Transition $transition, string $expression)
{
$this->transition = $transition;
$this->expression = $expression;
}
}
Loading