-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle][Workflow] better errors when security deps are missing #23638
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/WorkflowGuardListenerPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
|
||
/** | ||
* @author Christian Flothmann <christian.flothmann@sensiolabs.de> | ||
*/ | ||
class WorkflowGuardListenerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasParameter('workflow.has_guard_listeners')) { | ||
return; | ||
} | ||
|
||
$container->getParameterBag()->remove('workflow.has_guard_listeners'); | ||
|
||
if (!$container->has('security.token_storage')) { | ||
throw new LogicException('The "security.token_storage" service is needed to be able to use the workflow guard listener.'); | ||
} | ||
|
||
if (!$container->has('security.authorization_checker')) { | ||
throw new LogicException('The "security.authorization_checker" service is needed to be able to use the workflow guard listener.'); | ||
} | ||
|
||
if (!$container->has('security.authentication.trust_resolver')) { | ||
throw new LogicException('The "security.authentication.trust_resolver" service is needed to be able to use the workflow guard listener.'); | ||
} | ||
|
||
if (!$container->has('security.role_hierarchy')) { | ||
throw new LogicException('The "security.role_hierarchy" service is needed to be able to use the workflow guard listener.'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...ndle/FrameworkBundle/Tests/DependencyInjection/Compiler/WorkflowGuardListenerPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?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\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; | ||
use Symfony\Component\Security\Core\Role\RoleHierarchy; | ||
use Symfony\Component\Workflow\EventListener\GuardListener; | ||
|
||
class WorkflowGuardListenerPassTest extends TestCase | ||
{ | ||
private $container; | ||
private $compilerPass; | ||
|
||
protected function setUp() | ||
{ | ||
$this->container = new ContainerBuilder(); | ||
$this->container->register('foo.listener.guard', GuardListener::class); | ||
$this->container->register('bar.listener.guard', GuardListener::class); | ||
$this->compilerPass = new WorkflowGuardListenerPass(); | ||
} | ||
|
||
public function testListenersAreNotRemovedIfParameterIsNotSet() | ||
{ | ||
$this->compilerPass->process($this->container); | ||
|
||
$this->assertTrue($this->container->hasDefinition('foo.listener.guard')); | ||
$this->assertTrue($this->container->hasDefinition('bar.listener.guard')); | ||
} | ||
|
||
public function testParameterIsRemovedWhenThePassIsProcessed() | ||
{ | ||
$this->container->setParameter('workflow.has_guard_listeners', array('foo.listener.guard', 'bar.listener.guard')); | ||
|
||
try { | ||
$this->compilerPass->process($this->container); | ||
} catch (LogicException $e) { | ||
// Here, we are not interested in the exception handling. This is tested further down. | ||
} | ||
|
||
$this->assertFalse($this->container->hasParameter('workflow.has_guard_listeners')); | ||
} | ||
|
||
public function testListenersAreNotRemovedIfAllDependenciesArePresent() | ||
{ | ||
$this->container->setParameter('workflow.has_guard_listeners', array('foo.listener.guard', 'bar.listener.guard')); | ||
$this->container->register('security.token_storage', TokenStorageInterface::class); | ||
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class); | ||
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class); | ||
$this->container->register('security.role_hierarchy', RoleHierarchy::class); | ||
|
||
$this->compilerPass->process($this->container); | ||
|
||
$this->assertTrue($this->container->hasDefinition('foo.listener.guard')); | ||
$this->assertTrue($this->container->hasDefinition('bar.listener.guard')); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException | ||
* @expectedExceptionMessage The "security.token_storage" service is needed to be able to use the workflow guard listener. | ||
*/ | ||
public function testListenersAreRemovedIfTheTokenStorageServiceIsNotPresent() | ||
{ | ||
$this->container->setParameter('workflow.has_guard_listeners', array('foo.listener.guard', 'bar.listener.guard')); | ||
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class); | ||
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class); | ||
$this->container->register('security.role_hierarchy', RoleHierarchy::class); | ||
|
||
$this->compilerPass->process($this->container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException | ||
* @expectedExceptionMessage The "security.authorization_checker" service is needed to be able to use the workflow guard listener. | ||
*/ | ||
public function testListenersAreRemovedIfTheAuthorizationCheckerServiceIsNotPresent() | ||
{ | ||
$this->container->setParameter('workflow.has_guard_listeners', array('foo.listener.guard', 'bar.listener.guard')); | ||
$this->container->register('security.token_storage', TokenStorageInterface::class); | ||
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class); | ||
$this->container->register('security.role_hierarchy', RoleHierarchy::class); | ||
|
||
$this->compilerPass->process($this->container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException | ||
* @expectedExceptionMessage The "security.authentication.trust_resolver" service is needed to be able to use the workflow guard listener. | ||
*/ | ||
public function testListenersAreRemovedIfTheAuthenticationTrustResolverServiceIsNotPresent() | ||
{ | ||
$this->container->setParameter('workflow.has_guard_listeners', array('foo.listener.guard', 'bar.listener.guard')); | ||
$this->container->register('security.token_storage', TokenStorageInterface::class); | ||
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class); | ||
$this->container->register('security.role_hierarchy', RoleHierarchy::class); | ||
|
||
$this->compilerPass->process($this->container); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException | ||
* @expectedExceptionMessage The "security.role_hierarchy" service is needed to be able to use the workflow guard listener. | ||
*/ | ||
public function testListenersAreRemovedIfTheRoleHierarchyServiceIsNotPresent() | ||
{ | ||
$this->container->setParameter('workflow.has_guard_listeners', array('foo.listener.guard', 'bar.listener.guard')); | ||
$this->container->register('security.token_storage', TokenStorageInterface::class); | ||
$this->container->register('security.authorization_checker', AuthorizationCheckerInterface::class); | ||
$this->container->register('security.authentication.trust_resolver', AuthenticationTrustResolverInterface::class); | ||
|
||
$this->compilerPass->process($this->container); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure it's need but now we add all Compiler pass in the associated component.
It's a forgetfulness or it's done on purpose? (I dont this it's necessary here but ...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is completely specifc to the FrameworkBundle. If you want to register the listener in the container in your own application, you will probably have better ways to check if all needed dependencies are present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍