Skip to content

[Workflow] Override guard listener class #29751

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
->arrayNode('audit_trail')
->canBeEnabled()
->end()
->scalarNode('guard_listener')
->cannotBeEmpty()
->end()
->enumNode('type')
->values(array('workflow', 'state_machine'))
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,11 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
throw new LogicException('Cannot guard workflows as the Security component is not installed. Try running "composer require symfony/security".');
}

$guard = new Definition(Workflow\EventListener\GuardListener::class);
$guardListener = isset($workflow['guard_listener'])
? $workflow['guard_listener']
: $container->getParameter('workflow.guard_listener');

$guard = new Definition($guardListener);
$guard->setPrivate(true);

$guard->setArguments(array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="workflow.guard_listener">Symfony\Component\Workflow\EventListener\GuardListener</parameter>
</parameters>

<services>
<defaults public="false" />

Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Added guard `is_valid()` method support.
* Added support for `Event::getWorkflowName()` for "announce" events.
* Added `workflow.completed` events which are fired after a transition is completed.
* Added `guard_listener` configuration option along with `workflow.guard_listener` parameter.
* Added inheritance to `GuardListener`.

3.3.0
-----
Expand Down
18 changes: 9 additions & 9 deletions src/Symfony/Component/Workflow/EventListener/GuardListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
*/
class GuardListener
{
private $configuration;
private $expressionLanguage;
private $tokenStorage;
private $authorizationChecker;
private $trustResolver;
private $roleHierarchy;
private $validator;
protected $configuration;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a no-go: we strongly favor private properties because it helps maintenance a lot.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. So as I understand it, it is preferable to create my own new class completely override the listener.guard services declaration.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - you can decorate the core listener if it serves your purpose, but only via composition, ie using its public API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is a no-go on changing the GuardListener properties visibility and there is other ways to serve my purpose, it seems this pull request can be closed

protected $expressionLanguage;
protected $tokenStorage;
protected $authorizationChecker;
protected $trustResolver;
protected $roleHierarchy;
protected $validator;

public function __construct(array $configuration, ExpressionLanguage $expressionLanguage, TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authorizationChecker, AuthenticationTrustResolverInterface $trustResolver, RoleHierarchyInterface $roleHierarchy = null, ValidatorInterface $validator = null)
{
Expand Down Expand Up @@ -62,15 +62,15 @@ public function onTransition(GuardEvent $event, $eventName)
}
}

private function validateGuardExpression(GuardEvent $event, $expression)
protected function validateGuardExpression(GuardEvent $event, $expression)
{
if (!$this->expressionLanguage->evaluate($expression, $this->getVariables($event))) {
$event->setBlocked(true);
}
}

// code should be sync with Symfony\Component\Security\Core\Authorization\Voter\ExpressionVoter
private function getVariables(GuardEvent $event)
protected function getVariables(GuardEvent $event)
{
$token = $this->tokenStorage->getToken();

Expand Down