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

Skip to content

Commit ab624f1

Browse files
committed
feature #21437 [Security] Use IteratorArgument for voters (jvasseur)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Security] Use IteratorArgument for voters | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | License | MIT Use an IteratorArgument for injecting voters into the AccessDecisionManager. Commits ------- 4ec80b1 Use IteratorArgument for voters
2 parents 7aee0ae + 4ec80b1 commit ab624f1

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

UPGRADE-3.3.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ Security
258258
* The `LogoutUrlGenerator::registerListener()` method will expect a 6th `$context = null` argument in 4.0.
259259
Define the argument when overriding this method.
260260

261+
* The `AccessDecisionManager::setVoters()` has been deprecated. Pass the voters to the constructor instead.
262+
261263
SecurityBundle
262264
--------------
263265

UPGRADE-4.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ Security
384384

385385
* The `LogoutUrlGenerator::registerListener()` method expects a 6th `$context = null` argument.
386386

387+
* The `AccessDecisionManager::setVoters()` has been removed. Pass the voters to the constructor instead.
388+
387389
SecurityBundle
388390
--------------
389391

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Compiler/AddSecurityVotersPass.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler;
1313

14+
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
1415
use Symfony\Component\DependencyInjection\ContainerBuilder;
1516
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1617
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
@@ -39,7 +40,7 @@ public function process(ContainerBuilder $container)
3940
throw new LogicException('No security voters found. You need to tag at least one with "security.voter"');
4041
}
4142

42-
$adm = $container->getDefinition($container->hasDefinition('debug.security.access.decision_manager') ? 'debug.security.access.decision_manager' : 'security.access.decision_manager');
43-
$adm->addMethodCall('setVoters', array($voters));
43+
$adm = $container->getDefinition('security.access.decision_manager');
44+
$adm->replaceArgument(0, new IteratorArgument($voters));
4445
}
4546
}

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Compiler/AddSecurityVotersPassTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public function testThatSecurityVotersAreProcessedInPriorityOrder()
5959
$compilerPass = new AddSecurityVotersPass();
6060
$compilerPass->process($container);
6161

62-
$calls = $container->getDefinition('security.access.decision_manager')->getMethodCalls();
63-
$refs = $calls[0][1][0];
62+
$argument = $container->getDefinition('security.access.decision_manager')->getArgument(0);
63+
$refs = $argument->getValues();
6464
$this->assertEquals(new Reference('highest_prio_service'), $refs[0]);
6565
$this->assertEquals(new Reference('lowest_prio_service'), $refs[1]);
6666
$this->assertCount(4, $refs);

src/Symfony/Component/Security/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* deprecated `setVoters()` of the `AccessDecisionManager` class in favor of passing the voters to the constructor.
8+
49
3.2.0
510
-----
611

src/Symfony/Component/Security/Core/Authorization/AccessDecisionManager.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,14 @@ class AccessDecisionManager implements AccessDecisionManagerInterface
3232
private $allowIfEqualGrantedDeniedDecisions;
3333

3434
/**
35-
* Constructor.
36-
*
37-
* @param VoterInterface[] $voters An array of VoterInterface instances
38-
* @param string $strategy The vote strategy
39-
* @param bool $allowIfAllAbstainDecisions Whether to grant access if all voters abstained or not
40-
* @param bool $allowIfEqualGrantedDeniedDecisions Whether to grant access if result are equals
35+
* @param iterable|VoterInterface[] $voters An iterator of VoterInterface instances
36+
* @param string $strategy The vote strategy
37+
* @param bool $allowIfAllAbstainDecisions Whether to grant access if all voters abstained or not
38+
* @param bool $allowIfEqualGrantedDeniedDecisions Whether to grant access if result are equals
4139
*
4240
* @throws \InvalidArgumentException
4341
*/
44-
public function __construct(array $voters = array(), $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true)
42+
public function __construct($voters = array(), $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true)
4543
{
4644
$strategyMethod = 'decide'.ucfirst($strategy);
4745
if (!is_callable(array($this, $strategyMethod))) {
@@ -58,9 +56,13 @@ public function __construct(array $voters = array(), $strategy = self::STRATEGY_
5856
* Configures the voters.
5957
*
6058
* @param VoterInterface[] $voters An array of VoterInterface instances
59+
*
60+
* @deprecated since version 3.3, to be removed in 4.0. Pass the voters to the constructor instead.
6161
*/
6262
public function setVoters(array $voters)
6363
{
64+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Pass the voters to the constructor instead.', __METHOD__), E_USER_DEPRECATED);
65+
6466
$this->voters = $voters;
6567
}
6668

@@ -162,8 +164,8 @@ private function decideConsensus(TokenInterface $token, array $attributes, $obje
162164
private function decideUnanimous(TokenInterface $token, array $attributes, $object = null)
163165
{
164166
$grant = 0;
165-
foreach ($attributes as $attribute) {
166-
foreach ($this->voters as $voter) {
167+
foreach ($this->voters as $voter) {
168+
foreach ($attributes as $attribute) {
167169
$result = $voter->vote($token, $object, array($attribute));
168170

169171
switch ($result) {

src/Symfony/Component/Security/Core/Authorization/TraceableAccessDecisionManager.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ public function __construct(AccessDecisionManagerInterface $manager)
3333
$this->manager = $manager;
3434

3535
if ($this->manager instanceof AccessDecisionManager) {
36-
// The strategy is stored in a private property of the decorated service
36+
// The strategy and voters are stored in a private properties of the decorated service
3737
$reflection = new \ReflectionProperty(AccessDecisionManager::class, 'strategy');
3838
$reflection->setAccessible(true);
3939
$this->strategy = $reflection->getValue($manager);
40+
$reflection = new \ReflectionProperty(AccessDecisionManager::class, 'voters');
41+
$reflection->setAccessible(true);
42+
$this->voters = $reflection->getValue($manager);
4043
}
4144
}
4245

@@ -58,9 +61,13 @@ public function decide(TokenInterface $token, array $attributes, $object = null)
5861

5962
/**
6063
* {@inheritdoc}
64+
*
65+
* @deprecated since version 3.3, to be removed in 4.0. Pass voters to the decorated AccessDecisionManager instead.
6166
*/
6267
public function setVoters(array $voters)
6368
{
69+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Pass voters to the decorated AccessDecisionManager instead.', __METHOD__), E_USER_DEPRECATED);
70+
6471
if (!method_exists($this->manager, 'setVoters')) {
6572
return;
6673
}
@@ -81,7 +88,7 @@ public function getStrategy()
8188
}
8289

8390
/**
84-
* @return array
91+
* @return iterable|VoterInterface[]
8592
*/
8693
public function getVoters()
8794
{

0 commit comments

Comments
 (0)