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

Skip to content

Commit b89df24

Browse files
committed
[Workflow] add guard is_valid() method support
1 parent de5c60d commit b89df24

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde
632632
new Reference('security.token_storage'),
633633
new Reference('security.authorization_checker'),
634634
new Reference('security.authentication.trust_resolver'),
635+
new Reference('validator'),
635636
new Reference('security.role_hierarchy'),
636637
));
637638

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,12 @@ protected function registerFunctions()
2929
}, function (array $variables, $attributes, $object = null) {
3030
return $variables['auth_checker']->isGranted($attributes, $object);
3131
});
32+
33+
$this->register('is_valid', function ($object = 'null', $groups = 'null') {
34+
return sprintf('$validator->validate(%s, null, %s)', $object, $groups);
35+
}, function (array $variables, $object = null, $groups = null) {
36+
$errors = $variables['validator']->validate($object, null, $groups);
37+
return count($errors) === 0;
38+
});
3239
}
3340
}

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

Lines changed: 14 additions & 2 deletions
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,14 +29,23 @@ 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)
33-
{
34+
public function __construct(
35+
$configuration,
36+
ExpressionLanguage $expressionLanguage,
37+
TokenStorageInterface $tokenStorage,
38+
AuthorizationCheckerInterface $authenticationChecker,
39+
AuthenticationTrustResolverInterface $trustResolver,
40+
ValidatorInterface $validator,
41+
RoleHierarchyInterface $roleHierarchy = null
42+
) {
3443
$this->configuration = $configuration;
3544
$this->expressionLanguage = $expressionLanguage;
3645
$this->tokenStorage = $tokenStorage;
3746
$this->authenticationChecker = $authenticationChecker;
3847
$this->trustResolver = $trustResolver;
48+
$this->validator = $validator;
3949
$this->roleHierarchy = $roleHierarchy;
4050
}
4151

@@ -72,6 +82,8 @@ private function getVariables(GuardEvent $event)
7282
'auth_checker' => $this->authenticationChecker,
7383
// needed for the is_* expression function
7484
'trust_resolver' => $this->trustResolver,
85+
// needed for the is_valid expression function
86+
'validator' => $this->validator
7587
);
7688

7789
return $variables;

src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
99
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
1010
use Symfony\Component\Security\Core\Role\Role;
11+
use Symfony\Component\Validator\Validator\ValidatorInterface;
1112
use Symfony\Component\Workflow\EventListener\ExpressionLanguage;
1213
use Symfony\Component\Workflow\EventListener\GuardListener;
1314
use Symfony\Component\Workflow\Event\GuardEvent;
@@ -30,8 +31,9 @@ protected function setUp()
3031
$this->tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();
3132
$authenticationChecker = $this->getMockBuilder(AuthorizationCheckerInterface::class)->getMock();
3233
$trustResolver = $this->getMockBuilder(AuthenticationTrustResolverInterface::class)->getMock();
34+
$validator = $this->getMockBuilder(ValidatorInterface::class)->getMock();
3335

34-
$this->listener = new GuardListener($configuration, $expressionLanguage, $this->tokenStorage, $authenticationChecker, $trustResolver);
36+
$this->listener = new GuardListener($configuration, $expressionLanguage, $this->tokenStorage, $authenticationChecker, $trustResolver, $validator);
3537
}
3638

3739
protected function tearDown()

0 commit comments

Comments
 (0)