Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e8c701f

Browse files
halundraNlyrixx
authored andcommitted
[Workflow] Added guard 'is_valid()' method support
1 parent 50fe6a3 commit e8c701f

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde
657657
new Reference('security.authorization_checker'),
658658
new Reference('security.authentication.trust_resolver'),
659659
new Reference('security.role_hierarchy'),
660+
new Reference('validator', ContainerInterface::NULL_ON_INVALID_REFERENCE),
660661
));
661662

662663
$container->setDefinition(sprintf('%s.listener.guard', $workflowId), $guard);

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* Add guard `is_valid()` method support
78
* Added support for `Event::getWorkflowName()` for "announce" events.
89
* Added `workflow.completed` events which are fired after a transition is completed.
910

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

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

1414
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage as BaseExpressionLanguage;
15+
use Symfony\Component\Validator\Validator\ValidatorInterface;
16+
use Symfony\Component\Workflow\Exception\RuntimeException;
1517

1618
/**
1719
* Adds some function to the default Symfony Security ExpressionLanguage.
@@ -29,5 +31,17 @@ protected function registerFunctions()
2931
}, function (array $variables, $attributes, $object = null) {
3032
return $variables['auth_checker']->isGranted($attributes, $object);
3133
});
34+
35+
$this->register('is_valid', function ($object = 'null', $groups = 'null') {
36+
return sprintf('0 === count($validator->validate(%s, null, %s))', $object, $groups);
37+
}, function (array $variables, $object = null, $groups = null) {
38+
if (!$variables['validator'] instanceof ValidatorInterface) {
39+
throw new RuntimeException('Validator not defined, did you install the component?');
40+
}
41+
42+
$errors = $variables['validator']->validate($object, null, $groups);
43+
44+
return 0 === count($errors);
45+
});
3246
}
3347
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1616
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1717
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
18+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1819
use Symfony\Component\Workflow\Event\GuardEvent;
1920

2021
/**
@@ -28,15 +29,17 @@ class GuardListener
2829
private $authenticationChecker;
2930
private $trustResolver;
3031
private $roleHierarchy;
32+
private $validator;
3133

32-
public function __construct($configuration, ExpressionLanguage $expressionLanguage, TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authenticationChecker, AuthenticationTrustResolverInterface $trustResolver, RoleHierarchyInterface $roleHierarchy = null)
34+
public function __construct($configuration, ExpressionLanguage $expressionLanguage, TokenStorageInterface $tokenStorage, AuthorizationCheckerInterface $authenticationChecker, AuthenticationTrustResolverInterface $trustResolver, RoleHierarchyInterface $roleHierarchy = null, ValidatorInterface $validator = null)
3335
{
3436
$this->configuration = $configuration;
3537
$this->expressionLanguage = $expressionLanguage;
3638
$this->tokenStorage = $tokenStorage;
3739
$this->authenticationChecker = $authenticationChecker;
3840
$this->trustResolver = $trustResolver;
3941
$this->roleHierarchy = $roleHierarchy;
42+
$this->validator = $validator;
4043
}
4144

4245
public function onTransition(GuardEvent $event, $eventName)
@@ -72,6 +75,8 @@ private function getVariables(GuardEvent $event)
7275
'auth_checker' => $this->authenticationChecker,
7376
// needed for the is_* expression function
7477
'trust_resolver' => $this->trustResolver,
78+
// needed for the is_valid expression function
79+
'validator' => $this->validator,
7580
);
7681

7782
return $variables;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\Exception;
13+
14+
/**
15+
* Base RuntimeException for the Workflow component.
16+
*
17+
* @author Alain Flaus <[email protected]>
18+
*/
19+
class RuntimeException extends \RuntimeException implements ExceptionInterface
20+
{
21+
}

0 commit comments

Comments
 (0)