Skip to content

Commit

Permalink
bug #29721 [SecurityBundle] Fix traceable voters (ro0NL)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.2 branch (closes #29721).

Discussion
----------

[SecurityBundle] Fix traceable voters

| Q             | A
| ------------- | ---
| Branch?       | 4.2
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? |  no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #29385
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

lets solve the BC break when autowiring individual voters.

Commits
-------

a7df429 [SecurityBundle] Fix traceable voters
  • Loading branch information
Robin Chalas committed Jan 1, 2019
2 parents 85b313d + a7df429 commit 4f31408
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
Expand Up @@ -44,7 +44,7 @@ public function process(ContainerBuilder $container)
}

$debug = $container->getParameter('kernel.debug');

$voterServices = array();
foreach ($voters as $voter) {
$voterServiceId = (string) $voter;
$definition = $container->getDefinition($voterServiceId);
Expand All @@ -56,17 +56,18 @@ public function process(ContainerBuilder $container)
}

if ($debug) {
// Decorate original voters with TraceableVoter
$debugVoterServiceId = 'debug.security.voter.'.$voterServiceId;
$voterServices[] = new Reference($debugVoterServiceId = 'debug.security.voter.'.$voterServiceId);

$container
->register($debugVoterServiceId, TraceableVoter::class)
->setDecoratedService($voterServiceId)
->addArgument(new Reference($debugVoterServiceId.'.inner'))
->addArgument($voter)
->addArgument(new Reference('event_dispatcher'));
} else {
$voterServices[] = $voter;
}
}

$adm = $container->getDefinition('security.access.decision_manager');
$adm->replaceArgument(0, new IteratorArgument($voters));
$container->getDefinition('security.access.decision_manager')
->replaceArgument(0, new IteratorArgument($voterServices));
}
}
Expand Up @@ -72,10 +72,7 @@ public function testThatSecurityVotersAreProcessedInPriorityOrder()
$this->assertCount(4, $refs);
}

/**
* Test that in debug mode, voters are correctly decorated.
*/
public function testThatVotersAreDecoratedInDebugMode(): void
public function testThatVotersAreTraceableInDebugMode(): void
{
$container = new ContainerBuilder();

Expand All @@ -96,21 +93,18 @@ public function testThatVotersAreDecoratedInDebugMode(): void
$compilerPass->process($container);

$def1 = $container->getDefinition('debug.security.voter.voter1');
$this->assertEquals(array('voter1', null, 0), $def1->getDecoratedService(), 'voter1: wrong return from getDecoratedService');
$this->assertEquals(new Reference('debug.security.voter.voter1.inner'), $def1->getArgument(0), 'voter1: wrong decorator argument');
$this->assertNull($def1->getDecoratedService(), 'voter1: should not be decorated');
$this->assertEquals(new Reference('voter1'), $def1->getArgument(0), 'voter1: wrong argument');

$def2 = $container->getDefinition('debug.security.voter.voter2');
$this->assertEquals(array('voter2', null, 0), $def2->getDecoratedService(), 'voter2: wrong return from getDecoratedService');
$this->assertEquals(new Reference('debug.security.voter.voter2.inner'), $def2->getArgument(0), 'voter2: wrong decorator argument');
$this->assertNull($def2->getDecoratedService(), 'voter2: should not be decorated');
$this->assertEquals(new Reference('voter2'), $def2->getArgument(0), 'voter2: wrong argument');

$voters = $container->findTaggedServiceIds('security.voter');
$this->assertCount(2, $voters, 'Incorrect count of voters');
}

/**
* Test that voters are not decorated if the application is not in debug mode.
*/
public function testThatVotersAreNotDecoratedWithoutDebugMode(): void
public function testThatVotersAreNotTraceableWithoutDebugMode(): void
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', false);
Expand All @@ -130,8 +124,8 @@ public function testThatVotersAreNotDecoratedWithoutDebugMode(): void
$compilerPass = new AddSecurityVotersPass();
$compilerPass->process($container);

$this->assertFalse($container->has('debug.security.voter.voter1'), 'voter1 should not be decorated');
$this->assertFalse($container->has('debug.security.voter.voter2'), 'voter2 should not be decorated');
$this->assertFalse($container->has('debug.security.voter.voter1'), 'voter1 should not be traced');
$this->assertFalse($container->has('debug.security.voter.voter2'), 'voter2 should not be traced');
}

/**
Expand Down

0 comments on commit 4f31408

Please sign in to comment.