Skip to content

Commit

Permalink
feature #14733 [Security] Add setVoters() on AccessDecisionManager (n…
Browse files Browse the repository at this point in the history
…icolas-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
  • Loading branch information
fabpot committed Jun 1, 2015
2 parents 2983ecb + 3fd7cea commit 78cf382
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Exception\LogicException;

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

$container->getDefinition('security.access.decision_manager')->replaceArgument(0, array_values($voters));
if (!$voters) {
throw new LogicException('No security voters found. You need to tag at least one with "security.voter"');
}

$container->getDefinition('security.access.decision_manager')->addMethodCall('setVoters', array(array_values($voters)));
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/SecurityBundle/composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.3.9",
"symfony/security": "~2.7|~3.0.0",
"symfony/security": "~2.8|~3.0.0",
"symfony/http-kernel": "~2.2|~3.0.0"
},
"require-dev": {
Expand Down
Expand Up @@ -41,12 +41,8 @@ class AccessDecisionManager implements AccessDecisionManagerInterface
*
* @throws \InvalidArgumentException
*/
public function __construct(array $voters, $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true)
public function __construct(array $voters = array(), $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true)
{
if (!$voters) {
throw new \InvalidArgumentException('You must at least add one voter.');
}

$strategyMethod = 'decide'.ucfirst($strategy);
if (!is_callable(array($this, $strategyMethod))) {
throw new \InvalidArgumentException(sprintf('The strategy "%s" is not supported.', $strategy));
Expand All @@ -58,6 +54,16 @@ public function __construct(array $voters, $strategy = self::STRATEGY_AFFIRMATIV
$this->allowIfEqualGrantedDeniedDecisions = (bool) $allowIfEqualGrantedDeniedDecisions;
}

/**
* Configures the voters.
*
* @param VoterInterface[] $voters An array of VoterInterface instances
*/
public function setVoters(array $voters)
{
$this->voters = $voters;
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -46,14 +46,6 @@ public function testSupportsAttribute()
$this->assertFalse($manager->supportsAttribute('foo'));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testSetVotersEmpty()
{
$manager = new AccessDecisionManager(array());
}

/**
* @expectedException \InvalidArgumentException
*/
Expand Down

0 comments on commit 78cf382

Please sign in to comment.