8000 restart from 0 · symfony/symfony@2f1bc20 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f1bc20

Browse files
1 parent 7abffb1 commit 2f1bc20

File tree

59 files changed

+1423
-1947
lines changed

Some content is hidden

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

59 files changed

+1423
-1947
lines changed

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
3838
use Symfony\Component\Security\Core\Authorization\AccessDecision;
3939
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
40-
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
4140
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
4241
use Symfony\Component\Security\Core\User\UserInterface;
4342
use Symfony\Component\Security\Csrf\CsrfToken;
@@ -205,17 +204,20 @@ protected function isGranted(mixed $attribute, mixed $subject = null): bool
205204
}
206205

207206
/**
208-
* Checks decision of the attribute against the current authentication token and optionally supplied subject.
207+
* Checks if the attribute is granted against the current authentication token and optionally supplied subject.
209208
*
210209
* @throws \LogicException
211210
*/
212-
protected function getDecision(mixed $attribute, mixed $subject = null): AccessDecision
211+
protected function getAccessDecision(mixed $attribute, mixed $subject = null): AccessDecision
213212
{
214213
if (!$this->container->has('security.authorization_checker')) {
215214
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
216215
}
217216

218-
return $this->container->get('security.authorization_checker')->getDecision($attribute, $subject);
217+
$accessDecision = null;
218+
$decision = $this->container->get('security.authorization_checker')->isGranted($attribute, $subject, $accessDecision);
219+
220+
return null === $accessDecision ? new AccessDecision($decision) : $accessDecision;
219221
}
220222

221223
/**
@@ -226,23 +228,13 @@ protected function getDecision(mixed $attribute, mixed $subject = null): AccessD
226228
*/
227229
protected function denyAccessUnlessGranted(mixed $attribute, mixed $subject = null, string $message = 'Access Denied.'): void
228230
{
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-
}
231+
$decision = $this->getAccessDecision($attribute, $subject);
239232

240-
if (!$decision->isGranted()) {
233+
if ($decision->isDenied()) {
241234
$exception = $this->createAccessDeniedException($message);
242235
$exception->setAttributes([$attribute]);
243236
$exception->setSubject($subject);
244237
$exception->setAccessDecision($decision);
245-
246238
throw $exception;
247239
}
248240
}

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/AbstractControllerTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
use Symfony\Component\Routing\RouterInterface;
4141
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
4242
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
43+
use Symfony\Component\Security\Core\Authorization\AccessDecision;
4344
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
45+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
4446
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
4547
use Symfony\Component\Security\Core\User\InMemoryUser;
4648
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
@@ -362,7 +364,14 @@ public function testdenyAccessUnlessGranted()
362364

363365
$this->expectException(AccessDeniedException::class);
364366

365-
$controller->denyAccessUnlessGranted('foo');
367+
try {
368+
$controller->denyAccessUnlessGranted('foo');
369+
} catch (AccessDeniedException $exception) {
370+
$this->assertFalse($exception->getAccessDecision()->getAccess());
371+
$this->assertEmpty($exception->getAccessDecision()->getVotes());
372+
$this->assertEmpty($exception->getAccessDecision()->getMessage());
373+
throw $exception;
374+
}
366375
}
367376

368377
/**
@@ -644,4 +653,32 @@ public function testSendEarlyHints()
644653

645654
$this->assertSame('</style.css>; rel="preload"; as="stylesheet",</script.js>; rel="preload"; as="script"', $response->headers->get('Link'));
646655
}
656+
657+
public function testdenyAccessUnlessGrantedWithAccessDecisionObject()
658+
{
659+
$authorizationChecker = new class implements AuthorizationCheckerInterface {
660+
public function isGranted(mixed $attribute, mixed $subject = null, ?AccessDecision &$accessDecision = null): bool
661+
{
662+
$accessDecision = new AccessDecision(false, [new Vote(-1)], 'access denied');
663+
return $accessDecision->getAccess();
664+
}
665+
};
666+
667+
$container = new Container();
668+
$container->set('security.authorization_checker', $authorizationChecker);
669+
670+
$controller = $this->createController();
671+
$controller->setContainer($container);
672+
673+
$this->expectException(AccessDeniedException::class);
674+
675+
try {
676+
$controller->denyAccessUnlessGranted('foo');
677+
} catch (AccessDeniedException $exception) {
678+
$this->assertFalse($exception->getAccessDecision()->getAccess());
679+
$this->assertCount(1, $exception->getAccessDecision()->getVotes());
680+
$this->assertSame('access denied', $exception->getAccessDecision()->getMessage());
681+
throw $exception;
682+
}
683+
}
647684
}

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
2121
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
2222
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
23+
use Symfony\Component\Security\Core\Authorization\AccessDecision;
2324
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
2425
use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager;
2526
use Symfony\Component\Security\Core\Authorization\Voter\TraceableVoter;
27+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
28+
use Symfony\Component\Security\Core\Authorization\Voter\VoteInterface;
2629
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
2730
use Symfony\Component\Security\Http\Firewall\SwitchUserListener;
2831
use Symfony\Component\Security\Http\FirewallMapInterface;
@@ -138,6 +141,7 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
138141

139142
// collect voter details
140143
$decisionLog = $this->accessDecisionManager->getDecisionLog();
144+
141145
foreach ($decisionLog as $key => $log) {
142146
$decisionLog[$key]['voter_details'] = [];
143147
foreach ($log['voterDetails'] as $voterDetail) {
@@ -146,10 +150,14 @@ public function collect(Request $request, Response $response, ?\Throwable $excep
146150
$decisionLog[$key]['voter_details'][] = [
147151
'class' => $classData,
148152
'attributes' => $voterDetail['attributes'], // Only displayed for unanimous strategy
149-
'vote' => $voterDetail['vote'],
153+
'vote' => $voterDetail['vote'] instanceof VoteInterface ? $voterDetail['vote'] : new Vote($voterDetail['vote']),
150154
];
151155
}
152156
unset($decisionLog[$key]['voterDetails']);
157+
158+
if (!$decisionLog[$key]['result'] instanceof AccessDecision) {
159+
$decisionLog[$key]['result'] = new AccessDecision($decisionLog[$key]['result']);
160+
}
153161
}
154162

155163
$this->data['access_decision_log'] = $decisionLog;

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

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

4240
/**
4341
* @param array<AuthenticatorFactoryInterface> $factories
@@ -475,7 +473,6 @@ private function getAccessDecisionStrategies(): array
475473
self::STRATEGY_CONSENSUS,
476474
self::STRATEGY_UNANIMOUS,
477475
self::STRATEGY_PRIORITY,
478-
self::STRATEGY_SCORING,
479476
];
480477
}
481478
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
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;
5655
use Symfony\Component\Security\Core\Authorization\Strategy\UnanimousStrategy;
5756
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
5857
use Symfony\Component\Security\Core\User\ChainUserChecker;
@@ -195,7 +194,6 @@ private function createStrategyDefinition(string $strategy, bool $allowIfAllAbst
195194
MainConfiguration::STRATEGY_CONSENSUS => new Definition(ConsensusStrategy::class, [$allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions]),
196195
MainConfiguration::STRATEGY_UNANIMOUS => new Definition(UnanimousStrategy::class, [$allowIfAllAbstainDecisions]),
197196
MainConfiguration::STRATEGY_PRIORITY => new Definition(PriorityStrategy::class, [$allowIfAllAbstainDecisions]),
198-
MainConfiguration::STRATEGY_SCORING => new Definition(ScoringStrategy::class, [$allowIfAllAbstainDecisions]),
199197
default => throw new InvalidConfigurationException(\sprintf('The strategy "%s" is not supported.', $strategy)),
200198
};
201199
}

src/Symfony/Bundle/SecurityBundle/EventListener/VoteListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(
3131

3232
public function onVoterVote(VoteEvent $event): void
3333
{
34-
$this->traceableAccessDecisionManager->addVoterVote($event->getVoter(), $event->getAttributes(), $event->getVoteObject());
34+
$this->traceableAccessDecisionManager->addVoterVote($event->getVoter(), $event->getAttributes(), $event->getVote(true));
3535
}
3636

3737
public static function getSubscribedEvents(): array

0 commit comments

Comments
 (0)
0