Skip to content

Commit 2297c26

Browse files
aschemppchalasr
authored andcommitted
Added access decision strategy to respect voter priority
1 parent f7d29b4 commit 2297c26

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

Authorization/AccessDecisionManager.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class AccessDecisionManager implements AccessDecisionManagerInterface
2626
const STRATEGY_AFFIRMATIVE = 'affirmative';
2727
const STRATEGY_CONSENSUS = 'consensus';
2828
const STRATEGY_UNANIMOUS = 'unanimous';
29+
const STRATEGY_PRIORITY = 'priority';
2930

3031
private $voters;
3132
private $strategy;
@@ -181,4 +182,28 @@ private function decideUnanimous(TokenInterface $token, array $attributes, $obje
181182

182183
return $this->allowIfAllAbstainDecisions;
183184
}
185+
186+
/**
187+
* Grant or deny access depending on the first voter that does not abstain.
188+
* The priority of voters can be used to overrule a decision.
189+
*
190+
* If all voters abstained from voting, the decision will be based on the
191+
* allowIfAllAbstainDecisions property value (defaults to false).
192+
*/
193+
private function decidePriority(TokenInterface $token, array $attributes, $object = null)
194+
{
195+
foreach ($this->voters as $voter) {
196+
$result = $voter->vote($token, $object, $attributes);
197+
198+
if (VoterInterface::ACCESS_GRANTED === $result) {
199+
return true;
200+
}
201+
202+
if (VoterInterface::ACCESS_DENIED === $result) {
203+
return false;
204+
}
205+
}
206+
207+
return $this->allowIfAllAbstainDecisions;
208+
}
184209
}

Tests/Authorization/AccessDecisionManagerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ public function getStrategyTests()
6666

6767
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(0, 0, 2), false, true, false],
6868
[AccessDecisionManager::STRATEGY_UNANIMOUS, $this->getVoters(0, 0, 2), true, true, true],
69+
70+
// priority
71+
[AccessDecisionManager::STRATEGY_PRIORITY, [
72+
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
73+
$this->getVoter(VoterInterface::ACCESS_GRANTED),
74+
$this->getVoter(VoterInterface::ACCESS_DENIED),
75+
$this->getVoter(VoterInterface::ACCESS_DENIED),
76+
], true, true, true],
77+
78+
[AccessDecisionManager::STRATEGY_PRIORITY, [
79+
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
80+
$this->getVoter(VoterInterface::ACCESS_DENIED),
81+
$this->getVoter(VoterInterface::ACCESS_GRANTED),
82+
$this->getVoter(VoterInterface::ACCESS_GRANTED),
83+
], true, true, false],
84+
85+
[AccessDecisionManager::STRATEGY_PRIORITY, [
86+
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
87+
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
88+
], false, true, false],
89+
90+
[AccessDecisionManager::STRATEGY_PRIORITY, [
91+
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
92+
$this->getVoter(VoterInterface::ACCESS_ABSTAIN),
93+
], true, true, true],
6994
];
7095
}
7196

0 commit comments

Comments
 (0)