-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Be able to know the reasons of the denied access #40711
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
[Security] be able to know the reasons of the denied access
Co-Authored-By: Dany Maillard <danymaillard93b@gmail.com> Co-Authored-By: Antoine Makdessi <antoine.makdessi@agriconomie.com>
- Loading branch information
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
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
93 changes: 93 additions & 0 deletions
93
src/Symfony/Component/Security/Core/Authorization/AccessDecision.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,93 @@ | ||
<?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\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authorization\Voter\AccessTrait; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Vote; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
|
||
/** | ||
* An AccessDecision is returned by an AccessDecisionManager and contains the access verdict and all the related votes. | ||
* | ||
* @author Dany Maillard <danymaillard93b@gmail.com> | ||
*/ | ||
final class AccessDecision | ||
{ | ||
use AccessTrait; | ||
|
||
/** @var Vote[] */ | ||
private $votes = []; | ||
|
||
/** | ||
* @param int $access One of the VoterInterface::ACCESS_* constants | ||
* @param Vote[] $votes | ||
*/ | ||
private function __construct(int $access, array $votes = []) | ||
{ | ||
$this->access = $access; | ||
$this->votes = $votes; | ||
} | ||
|
||
/** | ||
* @param Vote[] $votes | ||
*/ | ||
public static function createGranted(array $votes = []): self | ||
{ | ||
return new self(VoterInterface::ACCESS_GRANTED, $votes); | ||
} | ||
|
||
/** | ||
* @param Vote[] $votes | ||
*/ | ||
public static function createDenied(array $votes = []): self | ||
{ | ||
return new self(VoterInterface::ACCESS_DENIED, $votes); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getVotes(): array | ||
{ | ||
return $this->votes; | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getGrantedVotes(): array | ||
{ | ||
return $this->getVotesByAccess(Voter::ACCESS_GRANTED); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getAbstainedVotes(): array | ||
{ | ||
return $this->getVotesByAccess(Voter::ACCESS_ABSTAIN); | ||
} | ||
|
||
/** | ||
* @return Vote[] | ||
*/ | ||
public function getDeniedVotes(): array | ||
{ | ||
return $this->getVotesByAccess(Voter::ACCESS_DENIED); | ||
} | ||
|
||
private function getVotesByAccess(int $access): array | ||
{ | ||
return array_filter($this->votes, function (Vote $vote) use ($access) { return $vote->getAccess() === $access; }); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
namespace Symfony\Component\Security\Core\Authorization; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Vote; | ||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; | ||
use Symfony\Component\Security\Core\Exception\InvalidArgumentException; | ||
|
||
|
@@ -77,28 +78,29 @@ public function decide(TokenInterface $token, array $attributes, $object = null/ | |
* 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): bool | ||
private function decideAffirmative(TokenInterface $token, array $attributes, $object = null): AccessDecision | ||
{ | ||
$votes = []; | ||
$deny = 0; | ||
foreach ($this->voters as $voter) { | ||
$result = $voter->vote($token, $object, $attributes); | ||
$votes[] = $vote = $this->vote($voter, $token, $object, $attributes); | ||
|
||
if (VoterInterface::ACCESS_GRANTED === $result) { | ||
return true; | ||
if ($vote->isGranted()) { | ||
return AccessDecision::createGranted($votes); | ||
} | ||
|
||
if (VoterInterface::ACCESS_DENIED === $result) { | ||
if ($vote->isDenied()) { | ||
++$deny; | ||
} elseif (VoterInterface::ACCESS_ABSTAIN !== $result) { | ||
trigger_deprecation('symfony/security-core', '5.3', 'Returning "%s" in "%s::vote()" is deprecated, return one of "%s" constants: "ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN".', var_export($result, true), get_debug_type($voter), VoterInterface::class); | ||
} | ||
} | ||
|
||
if ($deny > 0) { | ||
return false; | ||
return AccessDecision::createDenied($votes); | ||
} | ||
|
||
return $this->allowIfAllAbstainDecisions; | ||
return $this->decideIfAllAbstainDecisions(); | ||
} | ||
|
||
/** | ||
|
@@ -115,35 +117,39 @@ private function decideAffirmative(TokenInterface $token, array $attributes, $ob | |
* If all voters abstained from voting, the decision will be based on the | ||
* allowIfAllAbstainDecisions property value (defaults to false). | ||
*/ | ||
private function decideConsensus(TokenInterface $token, array $attributes, $object = null): bool | ||
private function decideConsensus(TokenInterface $token, array $attributes, $object = null): AccessDecision | ||
{ | ||
$votes = []; | ||
$grant = 0; | ||
$deny = 0; | ||
foreach ($this->voters as $voter) { | ||
$result = $voter->vote($token, $object, $attributes); | ||
$votes[] = $vote = $this->vote($voter, $token, $object, $attributes); | ||
|
||
if (VoterInterface::ACCESS_GRANTED === $result) { | ||
if ($vote->isGranted()) { | ||
++$grant; | ||
} elseif (VoterInterface::ACCESS_DENIED === $result) { | ||
} elseif ($vote->isDenied()) { | ||
++$deny; | ||
} elseif (VoterInterface::ACCESS_ABSTAIN !== $result) { | ||
trigger_deprecation('symfony/security-core', '5.3', 'Returning "%s" in "%s::vote()" is deprecated, return one of "%s" constants: "ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN".', var_export($result, true), get_debug_type($voter), VoterInterface::class); | ||
} | ||
} | ||
|
||
if ($grant > $deny) { | ||
return true; | ||
return AccessDecision::createGranted($votes); | ||
} | ||
|
||
if ($deny > $grant) { | ||
return false; | ||
return AccessDecision::createDenied($votes); | ||
} | ||
|
||
if ($grant > 0) { | ||
return $this->allowIfEqualGrantedDeniedDecisions; | ||
return $this->allowIfEqualGrantedDeniedDecisions | ||
? AccessDecision::createGranted() | ||
: AccessDecision::createDenied() | ||
; | ||
} | ||
|
||
return $this->allowIfAllAbstainDecisions; | ||
return $this->decideIfAllAbstainDecisions(); | ||
} | ||
|
||
/** | ||
|
@@ -152,18 +158,19 @@ private function decideConsensus(TokenInterface $token, array $attributes, $obje | |
* 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): bool | ||
private function decideUnanimous(TokenInterface $token, array $attributes, $object = null): AccessDecision | ||
{ | ||
$votes = []; | ||
$grant = 0; | ||
foreach ($this->voters as $voter) { | ||
foreach ($attributes as $attribute) { | ||
$result = $voter->vote($token, $object, [$attribute]); | ||
$votes[] = $vote = $this->vote($voter, $token, $object, [$attribute]); | ||
|
||
if (VoterInterface::ACCESS_DENIED === $result) { | ||
return false; | ||
if ($vote->isDenied()) { | ||
return AccessDecision::createDenied($votes); | ||
} | ||
|
||
if (VoterInterface::ACCESS_GRANTED === $result) { | ||
if ($vote->isGranted()) { | ||
++$grant; | ||
} elseif (VoterInterface::ACCESS_ABSTAIN !== $result) { | ||
trigger_deprecation('symfony/security-core', '5.3', 'Returning "%s" in "%s::vote()" is deprecated, return one of "%s" constants: "ACCESS_GRANTED", "ACCESS_DENIED" or "ACCESS_ABSTAIN".', var_export($result, true), get_debug_type($voter), VoterInterface::class); | ||
|
@@ -173,10 +180,10 @@ private function decideUnanimous(TokenInterface $token, array $attributes, $obje | |
|
||
// no deny votes | ||
if ($grant > 0) { | ||
return true; | ||
return AccessDecision::createGranted($votes); | ||
} | ||
|
||
return $this->allowIfAllAbstainDecisions; | ||
return $this->decideIfAllAbstainDecisions(); | ||
} | ||
|
||
/** | ||
|
@@ -206,4 +213,22 @@ private function decidePriority(TokenInterface $token, array $attributes, $objec | |
|
||
return $this->allowIfAllAbstainDecisions; | ||
} | ||
|
||
private function decideIfAllAbstainDecisions(): AccessDecision | ||
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. i would directly write this code, the function seem not mandatory |
||
{ | ||
return $this->allowIfAllAbstainDecisions | ||
? AccessDecision::createGranted() | ||
: AccessDecision::createDenied() | ||
; | ||
} | ||
|
||
private function vote(VoterInterface $voter, TokenInterface $token, $subject, array $attributes): Vote | ||
{ | ||
if (\is_int($vote = $voter->vote($token, $subject, $attributes))) { | ||
trigger_deprecation('symfony/security', 5.1, 'Returning an int from the "%s::vote()" method is deprecated. Return a "%s" object instead.', \get_class($this->voter), Vote::class); | ||
$vote = Vote::create($vote); | ||
} | ||
|
||
return $vote; | ||
} | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/Symfony/Component/Security/Core/Authorization/Voter/AccessTrait.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 <fabien@symfony.com> | ||
* | ||
* 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; | ||
|
||
/** | ||
* @author Dany Maillard <danymaillard93b@gmail.com> | ||
*/ | ||
trait AccessTrait | ||
{ | ||
/** @var int One of the VoterInterface::ACCESS_* constants */ | ||
protected $access; | ||
|
||
public function getAccess(): int | ||
{ | ||
return $this->access; | ||
} | ||
|
||
public function isGranted(): bool | ||
{ | ||
return VoterInterface::ACCESS_GRANTED === $this->access; | ||
} | ||
|
||
public function isAbstain(): bool | ||
{ | ||
return VoterInterface::ACCESS_ABSTAIN === $this->access; | ||
} | ||
|
||
public function isDenied(): bool | ||
{ | ||
return VoterInterface::ACCESS_DENIED === $this->access; | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.