-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[SecurityBundle] resolve class name parameter inside AddSecurityVotersPass #23862
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d0cdefc
[SecurityBundle] added compiler pass for checking voters validity
pjarmalavicius 644261b
Fix code standard
pjarmalavicius db9df9f
Fix code standard
pjarmalavicius 280fe46
Remove CheckSecurityVotersValidityPass. Resolve class name parameter …
pjarmalavicius 82161e8
Fix coding standard
pjarmalavicius cd29371
Add empty lines for better readability
pjarmalavicius 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
Next
Next commit
[SecurityBundle] added compiler pass for checking voters validity
- Loading branch information
commit d0cdefc6c4971fc0d04b51959382c380578e583f
There are no files selected for viewing
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
47 changes: 47 additions & 0 deletions
47
...ny/Bundle/SecurityBundle/DependencyInjection/Compiler/CheckSecurityVotersValidityPass.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,47 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
|
||
/** | ||
* Checks if all configured security voters implements VoterInterface. | ||
* | ||
* @author Paulius Jarmalavičius <paulius.jarmalavicius@gmail.com> | ||
*/ | ||
class CheckSecurityVotersValidityPass implements CompilerPassInterface | ||
{ | ||
use PriorityTaggedServiceTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$voters = $this->findAndSortTaggedServices('security.voter', $container); | ||
foreach ($voters as $voter) { | ||
$class = $container->getDefinition((string) $voter)->getClass(); | ||
|
||
if (!is_a($class, VoterInterface::class, true)) { | ||
@trigger_error( | ||
sprintf( | ||
'Using a security.voter tag on a class without implementing the %1$s is deprecated as of 3.4 and will be removed in 4.0. Implement the %1$s instead.', | ||
VoterInterface::class | ||
), | ||
E_USER_DEPRECATED | ||
); | ||
} | ||
|
||
if (!method_exists($class, 'vote')) { | ||
// in case the vote method is completely missing, to prevent exceptions when voting | ||
throw new LogicException( | ||
sprintf('%s should implement the %s interface when used as voter.', $class, VoterInterface::class) | ||
); | ||
} | ||
} | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
...SecurityBundle/Tests/DependencyInjection/Compiler/CheckSecurityVotersValidityPassTest.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,78 @@ | ||
<?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\SecurityBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\AddSecurityVotersPass; | ||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\CheckSecurityVotersValidityPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; | ||
use Symfony\Component\Security\Core\Tests\Authorization\Stub\VoterWithoutInterface; | ||
|
||
class CheckSecurityVotersValidityPassTest extends TestCase | ||
{ | ||
/** | ||
* @group legacy | ||
* @expectedDeprecation Using a security.voter tag on a class without implementing the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface is deprecated as of 3.4 and will be removed in 4.0. Implement the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface instead. | ||
*/ | ||
public function testVoterMissingInterface() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container | ||
->register('security.access.decision_manager', AccessDecisionManager::class) | ||
->addArgument(array()) | ||
; | ||
$container | ||
->register('without_interface', VoterWithoutInterface::class) | ||
->addTag('security.voter') | ||
; | ||
$addCompilerPass = new AddSecurityVotersPass(); | ||
$addCompilerPass->process($container); | ||
$checkCompilerPass = new CheckSecurityVotersValidityPass(); | ||
$checkCompilerPass->process($container); | ||
|
||
$argument = $container->getDefinition('security.access.decision_manager')->getArgument(0); | ||
$refs = $argument->getValues(); | ||
$this->assertEquals(new Reference('without_interface'), $refs[0]); | ||
$this->assertCount(1, $refs); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
public function testVoterMissingInterfaceAndMethod() | ||
{ | ||
$exception = LogicException::class; | ||
$message = 'stdClass should implement the Symfony\Component\Security\Core\Authorization\Voter\VoterInterface interface when used as voter.'; | ||
|
||
if (method_exists($this, 'expectException')) { | ||
$this->expectException($exception); | ||
$this->expectExceptionMessage($message); | ||
} else { | ||
$this->setExpectedException($exception, $message); | ||
} | ||
|
||
$container = new ContainerBuilder(); | ||
$container | ||
->register('security.access.decision_manager', AccessDecisionManager::class) | ||
->addArgument(array()) | ||
; | ||
$container | ||
->register('without_method', 'stdClass') | ||
->addTag('security.voter') | ||
; | ||
$compilerPass = new CheckSecurityVotersValidityPass(); | ||
$compilerPass->process($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.
please don't remove the empty lines we have for readability.