-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Make it possible to give voters a weight in consensus decisions #16828
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 all commits
Commits
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
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
src/Symfony/Component/Security/Core/Authorization/Voter/Decorator/Weight.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 <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization\Voter\Decorator; | ||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\WeightedVoterInterface; | ||
|
||
/** | ||
* A decorator to decorate existing voters with the weighted feature. | ||
* | ||
* @author Thomas Ploch <[email protected]> | ||
*/ | ||
final class Weight implements WeightedVoterInterface | ||
{ | ||
private $voter; | ||
private $weight; | ||
|
||
/** | ||
* Weight constructor. | ||
* | ||
* @param VoterInterface $voter | ||
* @param int $weight | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct(VoterInterface $voter, $weight) | ||
{ | ||
$this->voter = $voter; | ||
$this->weight = (int) $weight; | ||
|
||
if ($this->weight < 1) { | ||
throw new InvalidArgumentException(sprintf('Weight decorator for class "%s" needs to have an integer weight >= 1', get_class($this->voter))); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function vote(TokenInterface $token, $subject, array $attributes) | ||
{ | ||
return $this->voter->vote($token, $subject, $attributes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getWeight() | ||
{ | ||
return $this->weight; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsAttribute($attribute) | ||
{ | ||
return $this->voter->supportsAttribute($attribute); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsClass($class) | ||
{ | ||
return $this->voter->supportsClass($class); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Security/Core/Authorization/Voter/WeightedVoter.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,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization\Voter; | ||
|
||
/** | ||
* WeightedVoter is an abstract implementation of a weighted voter. | ||
* | ||
* @author Thomas Ploch <[email protected]> | ||
*/ | ||
abstract class WeightedVoter extends Voter implements WeightedVoterInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
abstract public function getWeight(); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Symfony/Component/Security/Core/Authorization/Voter/WeightedVoterInterface.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,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Authorization\Voter; | ||
|
||
/** | ||
* WeightedVoterInterface is the interface implemented by voters that have a higher weight in decisions. | ||
* | ||
* @author Thomas Ploch <[email protected]> | ||
*/ | ||
interface WeightedVoterInterface extends VoterInterface | ||
{ | ||
/** | ||
* This method provides the weight used to come to a weighted authorization decision. | ||
* | ||
* The weight has to be an integer value >= 1. | ||
* | ||
* @return int | ||
*/ | ||
public function getWeight(); | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Security/Core/Tests/Authorization/Voter/BaseVoterTest.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,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter; | ||
|
||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
|
||
abstract class BaseVoterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
protected $token; | ||
|
||
protected function setUp() | ||
{ | ||
$this->token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface'); | ||
} | ||
|
||
public function getTests() | ||
{ | ||
return array( | ||
array(array('EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if attribute and class are supported and attribute grants access'), | ||
array(array('CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if attribute and class are supported and attribute does not grant access'), | ||
|
||
array(array('DELETE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute is supported and grants access'), | ||
array(array('DELETE', 'CREATE'), VoterInterface::ACCESS_DENIED, new \stdClass(), 'ACCESS_DENIED if one attribute is supported and denies access'), | ||
|
||
array(array('CREATE', 'EDIT'), VoterInterface::ACCESS_GRANTED, new \stdClass(), 'ACCESS_GRANTED if one attribute grants access'), | ||
|
||
array(array('DELETE'), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attribute is supported'), | ||
|
||
array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, $this, 'ACCESS_ABSTAIN if class is not supported'), | ||
|
||
array(array('EDIT'), VoterInterface::ACCESS_ABSTAIN, null, 'ACCESS_ABSTAIN if object is null'), | ||
|
||
array(array(), VoterInterface::ACCESS_ABSTAIN, new \stdClass(), 'ACCESS_ABSTAIN if no attributes were provided'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider getTests | ||
*/ | ||
public function testVote(array $attributes, $expectedVote, $object, $message) | ||
{ | ||
$voter = $this->getVoter(); | ||
|
||
$this->assertEquals($expectedVote, $voter->vote($this->token, $object, $attributes), $message); | ||
} | ||
|
||
/** | ||
* @return VoterInterface | ||
*/ | ||
abstract protected function getVoter(); | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Security/Core/Tests/Authorization/Voter/Decorator/WeightTest.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,41 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Tests\Authorization\Voter\Decorator; | ||
|
||
use Symfony\Component\Security\Core\Authorization\Voter\Decorator\Weight; | ||
use Symfony\Component\Security\Core\Tests\Authorization\Voter\BaseVoterTest; | ||
use Symfony\Component\Security\Core\Tests\Authorization\Voter\VoterTest_Voter; | ||
|
||
class WeightTest extends BaseVoterTest | ||
{ | ||
public function testInterface() | ||
{ | ||
$voter = $this->getVoter(); | ||
|
||
$this->assertInstanceOf('\Symfony\Component\Security\Core\Authorization\Voter\WeightedVoterInterface', $voter); | ||
$this->assertInstanceOf('\Symfony\Component\Security\Core\Authorization\Voter\VoterInterface', $voter); | ||
} | ||
|
||
public function testWeight() | ||
{ | ||
$voter = $this->getVoter(); | ||
|
||
$this->assertEquals(3, $voter->getWeight()); | ||
} | ||
|
||
protected function getVoter() | ||
{ | ||
$baseVoter = new VoterTest_Voter(); | ||
|
||
return new Weight($baseVoter, 3); | ||
} | ||
} |
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.
Don't put a use statement here, use
\InvalidArgumentException
directly.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.
@dosten This change came from the fabbot CS checks, so I guess that's the right way.
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.
Mmm strange.. AFAIK we use
\InvalidArgumentException
ping @fabpot @stof