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

Skip to content

Commit 70f7b1c

Browse files
committed
feature #16754 [Security] allow arbitrary types in VoterInterface::vote() (xabbuh)
This PR was merged into the 3.0-dev branch. Discussion ---------- [Security] allow arbitrary types in VoterInterface::vote() | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #16600 | License | MIT | Doc PR | TODO Commits ------- 9054bdf allow arbitrary types in VoterInterface::vote()
2 parents a23444a + 9054bdf commit 70f7b1c

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

UPGRADE-3.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,10 @@ UPGRADE FROM 2.x to 3.0
615615

616616
### Security
617617

618+
* The `vote()` method from the `VoterInterface` was changed to now accept arbitrary
619+
types and not only objects. You can rely on the new abstract `Voter` class introduced
620+
in 2.8 to ease integrating your own voters.
621+
618622
* The `Resources/` directory was moved to `Core/Resources/`
619623

620624
* The `key` settings of `anonymous`, `remember_me` and `http_digest` are

src/Symfony/Component/Security/Core/Authorization/Voter/AuthenticatedVoter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(AuthenticationTrustResolverInterface $authentication
4444
/**
4545
* {@inheritdoc}
4646
*/
47-
public function vote(TokenInterface $token, $object, array $attributes)
47+
public function vote(TokenInterface $token, $subject, array $attributes)
4848
{
4949
$result = VoterInterface::ACCESS_ABSTAIN;
5050
foreach ($attributes as $attribute) {

src/Symfony/Component/Security/Core/Authorization/Voter/ExpressionVoter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function addExpressionLanguageProvider(ExpressionFunctionProviderInterfac
5252
/**
5353
* {@inheritdoc}
5454
*/
55-
public function vote(TokenInterface $token, $object, array $attributes)
55+
public function vote(TokenInterface $token, $subject, array $attributes)
5656
{
5757
$result = VoterInterface::ACCESS_ABSTAIN;
5858
$variables = null;
@@ -62,7 +62,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
6262
}
6363

6464
if (null === $variables) {
65-
$variables = $this->getVariables($token, $object);
65+
$variables = $this->getVariables($token, $subject);
6666
}
6767

6868
$result = VoterInterface::ACCESS_DENIED;
@@ -74,7 +74,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
7474
return $result;
7575
}
7676

77-
private function getVariables(TokenInterface $token, $object)
77+
private function getVariables(TokenInterface $token, $subject)
7878
{
7979
if (null !== $this->roleHierarchy) {
8080
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
@@ -85,16 +85,16 @@ private function getVariables(TokenInterface $token, $object)
8585
$variables = array(
8686
'token' => $token,
8787
'user' => $token->getUser(),
88-
'object' => $object,
88+
'object' => $subject,
8989
'roles' => array_map(function ($role) { return $role->getRole(); }, $roles),
9090
'trust_resolver' => $this->trustResolver,
9191
);
9292

9393
// this is mainly to propose a better experience when the expression is used
9494
// in an access control rule, as the developer does not know that it's going
9595
// to be handled by this voter
96-
if ($object instanceof Request) {
97-
$variables['request'] = $object;
96+
if ($subject instanceof Request) {
97+
$variables['request'] = $subject;
9898
}
9999

100100
return $variables;

src/Symfony/Component/Security/Core/Authorization/Voter/RoleVoter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct($prefix = 'ROLE_')
3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function vote(TokenInterface $token, $object, array $attributes)
38+
public function vote(TokenInterface $token, $subject, array $attributes)
3939
{
4040
$result = VoterInterface::ACCESS_ABSTAIN;
4141
$roles = $this->extractRoles($token);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ abstract class Voter implements VoterInterface
2424
/**
2525
* {@inheritdoc}
2626
*/
27-
public function vote(TokenInterface $token, $object, array $attributes)
27+
public function vote(TokenInterface $token, $subject, array $attributes)
2828
{
2929
// abstain vote by default in case none of the attributes are supported
3030
$vote = self::ACCESS_ABSTAIN;
3131

3232
foreach ($attributes as $attribute) {
33-
if (!$this->supports($attribute, $object)) {
33+
if (!$this->supports($attribute, $subject)) {
3434
continue;
3535
}
3636

3737
// as soon as at least one attribute is supported, default is to deny access
3838
$vote = self::ACCESS_DENIED;
3939

40-
if ($this->voteOnAttribute($attribute, $object, $token)) {
40+
if ($this->voteOnAttribute($attribute, $subject, $token)) {
4141
// grant access as soon as at least one attribute returns a positive response
4242
return self::ACCESS_GRANTED;
4343
}

src/Symfony/Component/Security/Core/Authorization/Voter/VoterInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ interface VoterInterface
3131
* ACCESS_GRANTED, ACCESS_DENIED, or ACCESS_ABSTAIN.
3232
*
3333
* @param TokenInterface $token A TokenInterface instance
34-
* @param object|null $object The object to secure
34+
* @param mixed $subject The subject to secure
3535
* @param array $attributes An array of attributes associated with the method being invoked
3636
*
3737
* @return int either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
3838
*/
39-
public function vote(TokenInterface $token, $object, array $attributes);
39+
public function vote(TokenInterface $token, $subject, array $attributes);
4040
}

0 commit comments

Comments
 (0)