Skip to content

Commit 78cf382

Browse files
committed
feature #14733 [Security] Add setVoters() on AccessDecisionManager (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [Security] Add setVoters() on AccessDecisionManager | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | partially #12360 | License | MIT | Doc PR | - Alternative for #14550 Commits ------- 3fd7cea [Security] Add setVoters() on AccessDecisionManager
2 parents 2983ecb + 3fd7cea commit 78cf382

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\DependencyInjection\Reference;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
use Symfony\Component\DependencyInjection\Exception\LogicException;
1718

1819
/**
1920
* Adds all configured security voters to the access decision manager.
@@ -40,6 +41,10 @@ public function process(ContainerBuilder $container)
4041
$voters = iterator_to_array($voters);
4142
ksort($voters);
4243

43-
$container->getDefinition('security.access.decision_manager')->replaceArgument(0, array_values($voters));
44+
if (!$voters) {
45+
throw new LogicException('No security voters found. You need to tag at least one with "security.voter"');
46+
}
47+
48+
$container->getDefinition('security.access.decision_manager')->addMethodCall('setVoters', array(array_values($voters)));
4449
}
4550
}

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": ">=5.3.9",
20-
"symfony/security": "~2.7|~3.0.0",
20+
"symfony/security": "~2.8|~3.0.0",
2121
"symfony/http-kernel": "~2.2|~3.0.0"
2222
},
2323
"require-dev": {

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ class AccessDecisionManager implements AccessDecisionManagerInterface
4141
*
4242
* @throws \InvalidArgumentException
4343
*/
44-
public function __construct(array $voters, $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true)
44+
public function __construct(array $voters = array(), $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true)
4545
{
46-
if (!$voters) {
47-
throw new \InvalidArgumentException('You must at least add one voter.');
48-
}
49-
5046
$strategyMethod = 'decide'.ucfirst($strategy);
5147
if (!is_callable(array($this, $strategyMethod))) {
5248
throw new \InvalidArgumentException(sprintf('The strategy "%s" is not supported.', $strategy));
@@ -58,6 +54,16 @@ public function __construct(array $voters, $strategy = self::STRATEGY_AFFIRMATIV
5854
$this->allowIfEqualGrantedDeniedDecisions = (bool) $allowIfEqualGrantedDeniedDecisions;
5955
}
6056

57+
/**
58+
* Configures the voters.
59+
*
60+
* @param VoterInterface[] $voters An array of VoterInterface instances
61+
*/
62+
public function setVoters(array $voters)
63+
{
64+
$this->voters = $voters;
65+
}
66+
6167
/**
6268
* {@inheritdoc}
6369
*/

src/Symfony/Component/Security/Core/Tests/Authorization/AccessDecisionManagerTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ public function testSupportsAttribute()
4646
$this->assertFalse($manager->supportsAttribute('foo'));
4747
}
4848

49-
/**
50-
* @expectedException \InvalidArgumentException
51-
*/
52-
public function testSetVotersEmpty()
53-
{
54-
$manager = new AccessDecisionManager(array());
55-
}
56-
5749
/**
5850
* @expectedException \InvalidArgumentException
5951
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy