|
17 | 17 | use Symfony\Bundle\SecurityBundle\Security\FirewallConfig;
|
18 | 18 | use Symfony\Bundle\SecurityBundle\Security\FirewallMap;
|
19 | 19 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
| 20 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
20 | 21 | use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
21 | 22 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
22 | 23 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
23 | 24 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
| 25 | +use Symfony\Component\Security\Core\Authorization\AccessDecisionManager; |
| 26 | +use Symfony\Component\Security\Core\Authorization\TraceableAccessDecisionManager; |
| 27 | +use Symfony\Component\Security\Core\Authorization\Voter\TraceableVoter; |
| 28 | +use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
24 | 29 | use Symfony\Component\Security\Core\Role\Role;
|
25 | 30 | use Symfony\Component\Security\Core\Role\RoleHierarchy;
|
26 | 31 | use Symfony\Component\Security\Core\Role\SwitchUserRole;
|
@@ -221,6 +226,137 @@ public function testGetListeners()
|
221 | 226 | $this->addToAssertionCount(1);
|
222 | 227 | }
|
223 | 228 |
|
| 229 | + public function providerCollectDecisionLog(): \Generator |
| 230 | + { |
| 231 | + $voter1 = $this->getMockBuilder(VoterInterface::class)->getMockForAbstractClass(); |
| 232 | + $voter2 = $this->getMockBuilder(VoterInterface::class)->getMockForAbstractClass(); |
| 233 | + |
| 234 | + $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMockForAbstractClass(); |
| 235 | + $decoratedVoter1 = new TraceableVoter($voter1, $eventDispatcher); |
| 236 | + $decoratedVoter2 = new TraceableVoter($voter2, $eventDispatcher); |
| 237 | + |
| 238 | + yield array( |
| 239 | + AccessDecisionManager::STRATEGY_AFFIRMATIVE, |
| 240 | + array(array( |
| 241 | + 'attributes' => array('view'), |
| 242 | + 'object' => new \stdClass(), |
| 243 | + 'result' => true, |
| 244 | + 'voterDetails' => array( |
| 245 | + array('voter' => $voter1, 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_ABSTAIN), |
| 246 | + array('voter' => $voter2, 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_ABSTAIN), |
| 247 | + ), |
| 248 | + )), |
| 249 | + array($decoratedVoter1, $decoratedVoter1), |
| 250 | + array(\get_class($voter1), \get_class($voter2)), |
| 251 | + array(array( |
| 252 | + 'attributes' => array('view'), |
| 253 | + 'object' => new \stdClass(), |
| 254 | + 'result' => true, |
| 255 | + 'voter_details' => array( |
| 256 | + array('class' => \get_class($voter1), 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_ABSTAIN), |
| 257 | + array('class' => \get_class($voter2), 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_ABSTAIN), |
| 258 | + ), |
| 259 | + )), |
| 260 | + ); |
| 261 | + |
| 262 | + yield array( |
| 263 | + AccessDecisionManager::STRATEGY_UNANIMOUS, |
| 264 | + array( |
| 265 | + array( |
| 266 | + 'attributes' => array('view', 'edit'), |
| 267 | + 'object' => new \stdClass(), |
| 268 | + 'result' => false, |
| 269 | + 'voterDetails' => array( |
| 270 | + array('voter' => $voter1, 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_DENIED), |
| 271 | + array('voter' => $voter1, 'attributes' => array('edit'), 'vote' => VoterInterface::ACCESS_DENIED), |
| 272 | + array('voter' => $voter2, 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 273 | + array('voter' => $voter2, 'attributes' => array('edit'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 274 | + ), |
| 275 | + ), |
| 276 | + array( |
| 277 | + 'attributes' => array('update'), |
| 278 | + 'object' => new \stdClass(), |
| 279 | + 'result' => true, |
| 280 | + 'voterDetails' => array( |
| 281 | + array('voter' => $voter1, 'attributes' => array('update'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 282 | + array('voter' => $voter2, 'attributes' => array('update'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 283 | + ), |
| 284 | + ), |
| 285 | + ), |
| 286 | + array($decoratedVoter1, $decoratedVoter1), |
| 287 | + array(\get_class($voter1), \get_class($voter2)), |
| 288 | + array( |
| 289 | + array( |
| 290 | + 'attributes' => array('view', 'edit'), |
| 291 | + 'object' => new \stdClass(), |
| 292 | + 'result' => false, |
| 293 | + 'voter_details' => array( |
| 294 | + array('class' => \get_class($voter1), 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_DENIED), |
| 295 | + array('class' => \get_class($voter1), 'attributes' => array('edit'), 'vote' => VoterInterface::ACCESS_DENIED), |
| 296 | + array('class' => \get_class($voter2), 'attributes' => array('view'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 297 | + array('class' => \get_class($voter2), 'attributes' => array('edit'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 298 | + ), |
| 299 | + ), |
| 300 | + array( |
| 301 | + 'attributes' => array('update'), |
| 302 | + 'object' => new \stdClass(), |
| 303 | + 'result' => true, |
| 304 | + 'voter_details' => array( |
| 305 | + array('class' => \get_class($voter1), 'attributes' => array('update'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 306 | + array('class' => \get_class($voter2), 'attributes' => array('update'), 'vote' => VoterInterface::ACCESS_GRANTED), |
| 307 | + ), |
| 308 | + ), |
| 309 | + ), |
| 310 | + ); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Test the returned data when AccessDecisionManager is a TraceableAccessDecisionManager. |
| 315 | + * |
| 316 | + * @param string $strategy strategy returned by the AccessDecisionManager |
| 317 | + * @param array $voters voters returned by AccessDecisionManager |
| 318 | + * @param array $decisionLog log of the votes and final decisions from AccessDecisionManager |
| 319 | + * @param array $expectedVoterClasses expected voter classes returned by the collector |
| 320 | + * @param array $expectedDecisionLog expected decision log returned by the collector |
| 321 | + * |
| 322 | + * @dataProvider providerCollectDecisionLog |
| 323 | + */ |
| 324 | + public function testCollectDecisionLog(string $strategy, array $decisionLog, array $voters, array $expectedVoterClasses, array $expectedDecisionLog): void |
| 325 | + { |
| 326 | + $accessDecisionManager = $this |
| 327 | + ->getMockBuilder(TraceableAccessDecisionManager::class) |
| 328 | + ->disableOriginalConstructor() |
| 329 | + ->setMethods(array('getStrategy', 'getVoters', 'getDecisionLog')) |
| 330 | + ->getMock(); |
| 331 | + |
| 332 | + $accessDecisionManager |
| 333 | + ->expects($this->any()) |
| 334 | + ->method('getStrategy') |
| 335 | + ->willReturn($strategy); |
| 336 | + |
| 337 | + $accessDecisionManager |
| 338 | + ->expects($this->any()) |
| 339 | + ->method('getVoters') |
| 340 | + ->willReturn($voters); |
| 341 | + |
| 342 | + $accessDecisionManager |
| 343 | + ->expects($this->any()) |
| 344 | + ->method('getDecisionLog') |
| 345 | + ->willReturn($decisionLog); |
| 346 | + |
| 347 | + $dataCollector = new SecurityDataCollector(null, null, null, $accessDecisionManager); |
| 348 | + $dataCollector->collect($this->getRequest(), $this->getResponse()); |
| 349 | + |
| 350 | + $this->assertEquals($dataCollector->getAccessDecisionLog(), $expectedDecisionLog, 'Wrong value returned by getAccessDecisionLog'); |
| 351 | + |
| 352 | + $this->assertSame( |
| 353 | + array_map(function ($classStub) { return (string) $classStub; }, $dataCollector->getVoters()), |
| 354 | + $expectedVoterClasses, |
| 355 | + 'Wrong value returned by getVoters' |
| 356 | + ); |
| 357 | + $this->assertSame($dataCollector->getVoterStrategy(), $strategy, 'Wrong value returned by getVoterStrategy'); |
| 358 | + } |
| 359 | + |
224 | 360 | public function provideRoles()
|
225 | 361 | {
|
226 | 362 | return array(
|
|
0 commit comments