-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Make AccessDecisionStrategy extensible. And add highestnotabstainedvoter strategy #15295
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
solilokiam
wants to merge
7
commits into
symfony:2.8
from
solilokiam:extensible-accessdecision-strategy
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
38b8cf5
[Security] Fixed #14049
solilokiam 66457b0
Make AccessDecisionManager work as a proxy class for other concrete a…
solilokiam 5894731
Remove AccessDecisionStrategyInterface
solilokiam 78c18ab
Fix wrong coding standards
solilokiam f78b3df
Make access decision manager configurable.
solilokiam ffb411b
Improve CustomAccessDecisionManagerCompilerPass
solilokiam 5d5f4ef
Change strategy namespace and naming
solilokiam 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
40 changes: 40 additions & 0 deletions
40
...ny/Bundle/SecurityBundle/DependencyInjection/Compiler/CustomAccessDecisionManagerPass.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,40 @@ | ||
<?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\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class CustomAccessDecisionManagerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$serviceName = $container->getParameter('security.access.manager.service'); | ||
|
||
if ($container->hasDefinition('security.authorization_checker') && $container->hasDefinition($serviceName)) { | ||
// We must assume that the class value has been correctly filled, even if the service is created by a factory | ||
$class = $container->getParameterBag()->resolveValue($container->getDefinition($serviceName)->getClass()); | ||
$refClass = new \ReflectionClass($class); | ||
|
||
if ($refClass->implementsInterface('Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface')) { | ||
$definition = $container->getDefinition('security.authorization_checker'); | ||
$definition->replaceArgument(2, new Reference($serviceName)); | ||
} else { | ||
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "AccessDecisionManagerInterface".', $serviceName)); | ||
} | ||
} | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,10 @@ | |
|
||
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager\AffirmativeAccessDecisionManager; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager\ConsensusAccessDecisionManager; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager\HighestNotAbstainedVoterAccessDecisionManager; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager\UnanimousAccessDecisionManager; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
|
||
|
@@ -25,8 +29,8 @@ class AccessDecisionManager implements AccessDecisionManagerInterface | |
const STRATEGY_AFFIRMATIVE = 'affirmative'; | ||
const STRATEGY_CONSENSUS = 'consensus'; | ||
const STRATEGY_UNANIMOUS = 'unanimous'; | ||
const STRATEGY_HIGHEST_NOT_ABSTAINED = 'highest'; | ||
|
||
private $voters; | ||
private $strategy; | ||
private $allowIfAllAbstainDecisions; | ||
private $allowIfEqualGrantedDeniedDecisions; | ||
|
@@ -43,15 +47,10 @@ class AccessDecisionManager implements AccessDecisionManagerInterface | |
*/ | ||
public function __construct(array $voters = array(), $strategy = self::STRATEGY_AFFIRMATIVE, $allowIfAllAbstainDecisions = false, $allowIfEqualGrantedDeniedDecisions = true) | ||
{ | ||
$strategyMethod = 'decide'.ucfirst($strategy); | ||
if (!is_callable(array($this, $strategyMethod))) { | ||
throw new \InvalidArgumentException(sprintf('The strategy "%s" is not supported.', $strategy)); | ||
} | ||
|
||
$this->voters = $voters; | ||
$this->strategy = $strategyMethod; | ||
$this->allowIfAllAbstainDecisions = (bool) $allowIfAllAbstainDecisions; | ||
$this->allowIfEqualGrantedDeniedDecisions = (bool) $allowIfEqualGrantedDeniedDecisions; | ||
$this->strategy = $this->createStrategy($strategy); | ||
$this->setVoters($voters); | ||
} | ||
|
||
/** | ||
|
@@ -61,159 +60,50 @@ public function __construct(array $voters = array(), $strategy = self::STRATEGY_ | |
*/ | ||
public function setVoters(array $voters) | ||
{ | ||
$this->voters = $voters; | ||
$this->strategy->setVoters($voters); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function decide(TokenInterface $token, array $attributes, $object = null) | ||
{ | ||
return $this->{$this->strategy}($token, $attributes, $object); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsAttribute($attribute) | ||
{ | ||
foreach ($this->voters as $voter) { | ||
if ($voter->supportsAttribute($attribute)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
return $this->strategy->decide($token, $attributes, $object); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsClass($class) | ||
{ | ||
foreach ($this->voters as $voter) { | ||
if ($voter->supportsClass($class)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Grants access if any voter returns an affirmative response. | ||
* | ||
* If all voters abstained from voting, the decision will be based on the | ||
* allowIfAllAbstainDecisions property value (defaults to false). | ||
*/ | ||
private function decideAffirmative(TokenInterface $token, array $attributes, $object = null) | ||
{ | ||
$deny = 0; | ||
foreach ($this->voters as $voter) { | ||
$result = $voter->vote($token, $object, $attributes); | ||
switch ($result) { | ||
case VoterInterface::ACCESS_GRANTED: | ||
return true; | ||
|
||
case VoterInterface::ACCESS_DENIED: | ||
++$deny; | ||
|
||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
if ($deny > 0) { | ||
return false; | ||
} | ||
|
||
return $this->allowIfAllAbstainDecisions; | ||
return $this->strategy->supportsClass($class); | ||
} | ||
|
||
/** | ||
* Grants access if there is consensus of granted against denied responses. | ||
* | ||
* Consensus means majority-rule (ignoring abstains) rather than unanimous | ||
* agreement (ignoring abstains). If you require unanimity, see | ||
* UnanimousBased. | ||
* Checks if the access decision manager supports the given attribute. | ||
* | ||
* If there were an equal number of grant and deny votes, the decision will | ||
* be based on the allowIfEqualGrantedDeniedDecisions property value | ||
* (defaults to true). | ||
* @param string $attribute An attribute | ||
* | ||
* If all voters abstained from voting, the decision will be based on the | ||
* allowIfAllAbstainDecisions property value (defaults to false). | ||
* @return bool true if this decision manager supports the attribute, false otherwise | ||
*/ | ||
private function decideConsensus(TokenInterface $token, array $attributes, $object = null) | ||
public function supportsAttribute($attribute) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep these methods before |
||
{ | ||
$grant = 0; | ||
$deny = 0; | ||
foreach ($this->voters as $voter) { | ||
$result = $voter->vote($token, $object, $attributes); | ||
|
||
switch ($result) { | ||
case VoterInterface::ACCESS_GRANTED: | ||
++$grant; | ||
|
||
break; | ||
|
||
case VoterInterface::ACCESS_DENIED: | ||
++$deny; | ||
|
||
break; | ||
} | ||
} | ||
|
||
if ($grant > $deny) { | ||
return true; | ||
} | ||
|
||
if ($deny > $grant) { | ||
return false; | ||
} | ||
|
||
if ($grant > 0) { | ||
return $this->allowIfEqualGrantedDeniedDecisions; | ||
} | ||
|
||
return $this->allowIfAllAbstainDecisions; | ||
return $this->strategy->supportsAttribute($attribute); | ||
} | ||
|
||
/** | ||
* Grants access if only grant (or abstain) votes were received. | ||
* | ||
* If all voters abstained from voting, the decision will be based on the | ||
* allowIfAllAbstainDecisions property value (defaults to false). | ||
*/ | ||
private function decideUnanimous(TokenInterface $token, array $attributes, $object = null) | ||
private function createStrategy($strategyName) | ||
{ | ||
$grant = 0; | ||
foreach ($attributes as $attribute) { | ||
foreach ($this->voters as $voter) { | ||
$result = $voter->vote($token, $object, array($attribute)); | ||
|
||
switch ($result) { | ||
case VoterInterface::ACCESS_GRANTED: | ||
++$grant; | ||
|
||
break; | ||
|
||
case VoterInterface::ACCESS_DENIED: | ||
return false; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
switch ($strategyName) { | ||
case self::STRATEGY_UNANIMOUS: | ||
return new UnanimousAccessDecisionManager($this->allowIfAllAbstainDecisions); | ||
case self::STRATEGY_CONSENSUS: | ||
return new ConsensusAccessDecisionManager($this->allowIfEqualGrantedDeniedDecisions, $this->allowIfAllAbstainDecisions); | ||
case self::STRATEGY_AFFIRMATIVE: | ||
return new AffirmativeAccessDecisionManager($this->allowIfAllAbstainDecisions); | ||
case self::STRATEGY_HIGHEST_NOT_ABSTAINED: | ||
return new HighestNotAbstainedVoterAccessDecisionManager($this->allowIfAllAbstainDecisions); | ||
default: | ||
throw new \InvalidArgumentException(sprintf('The strategy "%s" is not supported.', $strategyName)); | ||
} | ||
|
||
// no deny votes | ||
if ($grant > 0) { | ||
return true; | ||
} | ||
|
||
return $this->allowIfAllAbstainDecisions; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...onent/Security/Core/Authorization/AccessDecisionManager/AbstractAccessDecisionManager.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,52 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\Security\Core\Authorization\AccessDecisionManager; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
|
||
abstract class AbstractAccessDecisionManager implements AccessDecisionManagerInterface | ||
{ | ||
protected $voters; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setVoters(array $voters) | ||
{ | ||
$this->voters = $voters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsAttribute($attribute) | ||
{ | ||
foreach ($this->voters as $voter) { | ||
if ($voter->supportsAttribute($attribute)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsClass($class) | ||
{ | ||
foreach ($this->voters as $voter) { | ||
if ($voter->supportsClass($class)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
abstract public function decide(TokenInterface $token, array $attributes, $object = null); | ||
} |
Oops, something went wrong.
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.
These 2 double empty lines should become just one empty line. You should also include the license header between line 1 and 3 as you can see in other files in the code base.