8000 vote scoring and messages · symfony/symfony@fc77e53 · GitHub
[go: up one dir, main page]

Skip to content

Commit fc77e53

Browse files
committed
vote scoring and messages
1 parent ad5d178 commit fc77e53

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2043
-536
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/AbstractController.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
3636
use Symfony\Component\Routing\RouterInterface;
3737
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
38+
use Symfony\Component\Security\Core\Authorization\AccessDecision;
3839
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
40+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
3941
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
4042
use Symfony\Component\Security\Core\User\UserInterface;
4143
use Symfony\Component\Security\Csrf\CsrfToken;
@@ -202,6 +204,20 @@ protected function isGranted(mixed $attribute, mixed $subject = null): bool
202204
return $this->container->get('security.authorization_checker')->isGranted($attribute, $subject);
203205
}
204206

207+
/**
208+
* Checks decision of the attribute against the current authentication token and optionally supplied subject.
209+
*
210+
* @throws \LogicException
211+
*/
212+
protected function getDecision(mixed $attribute, mixed $subject = null): AccessDecision
213+
{
214+
if (!$this->container->has('security.authorization_checker')) {
215+
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
216+
}
217+
218+
return $this->container->get('security.authorization_checker')->getDecision($attribute, $subject);
219+
}
220+
205221
/**
206222
* Throws an exception unless the attribute is granted against the current authentication token and optionally
207223
* supplied subject.
@@ -210,10 +226,22 @@ protected function isGranted(mixed $attribute, mixed $subject = null): bool
210226
*/
211227
protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = null, string $message = 'Access Denied.'): void
212228
{
213-
if (!$this->isGranted($attribute, $subject)) {
229+
if (!$this->container->has('security.authorization_checker')) {
230+
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
231+
}
232+
233+
$checker = $this->container->get('security.authorization_checker');
234+
if (method_exists($checker, 'getDecision')) {
235+
$decision = $checker->getDecision($attribute, $subject);
236+
} else {
237+
$decision = new AccessDecision($checker->isGranted($attribute, $subject) ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED);
238+
}
239+
240+
if (!$decision->isGranted()) {
214241
$exception = $this->createAccessDeniedException($message);
215242
$exception->setAttributes([$attribute]);
216243
$exception->setSubject($subject);
244+
$exception->setAccessDecision($decision);
217245

218246
throw $exception;
219247
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/MainConfiguration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class MainConfiguration implements ConfigurationInterface
3535
public const STRATEGY_UNANIMOUS = 'unanimous';
3636
/** @internal */
3737
public const STRATEGY_PRIORITY = 'priority';
38+
/** @internal */
39+
public const STRATEGY_SCORING = 'scoring';
3840

3941
/**
4042
* @param array<AuthenticatorFactoryInterface> $factories
@@ -455,6 +457,7 @@ private function getAccessDecisionStrategies(): array
455457
self::STRATEGY_CONSENSUS,
456458
self::STRATEGY_UNANIMOUS,
457459
self::STRATEGY_PRIORITY,
460+
self::STRATEGY_SCORING,
458461
];
459462
}
460463
}

src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Symfony\Component\Security\Core\Authorization\Strategy\AffirmativeStrategy;
5353
use Symfony\Component\Security\Core\Authorization\Strategy\ConsensusStrategy;
5454
use Symfony\Component\Security\Core\Authorization\Strategy\PriorityStrategy;
55+
use Symfony\Component\Security\Core\Authorization\Strategy\ScoringStrategy;
5556
use Symfony\Component\Security\Core\Authorization\Strategy\UnanimousStrategy;
5657
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
5758
use Symfony\Component\Security\Core\User\ChainUserChecker;
@@ -192,6 +193,7 @@ private function createStrategyDefinition(string $strategy, bool $allowIfAllAbst
192193
MainConfiguration::STRATEGY_CONSENSUS => new Definition(ConsensusStrategy::class, [$allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions]),
193194
MainConfiguration::STRATEGY_UNANIMOUS => new Definition(UnanimousStrategy::class, [$allowIfAllAbstainDecisions]),
194195
MainConfiguration::STRATEGY_PRIORITY => new Definition(PriorityStrategy::class, [$allowIfAllAbstainDecisions]),
196+
MainConfiguration::STRATEGY_SCORING => new Definition(ScoringStrategy::class, [$allowIfAllAbstainDecisions]),
195197
default => throw new InvalidConfigurationException(\sprintf('The strategy "%s" is not supported.', $strategy)),
196198
};
197199
}

0 commit comments

Comments
 (0)
0