[SecurityBundle] simplified code

This commit is contained in:
Fabien Potencier 2017-02-21 18:40:03 -08:00
parent 677df7b57b
commit 512742be52
2 changed files with 23 additions and 20 deletions

View File

@ -11,9 +11,9 @@
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\Exception\LogicException;
/**
@ -23,6 +23,8 @@ use Symfony\Component\DependencyInjection\Exception\LogicException;
*/
class AddSecurityVotersPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;
/**
* {@inheritdoc}
*/
@ -32,15 +34,7 @@ class AddSecurityVotersPass implements CompilerPassInterface
return;
}
$voters = array();
foreach ($container->findTaggedServiceIds('security.voter') as $id => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$voters[$priority][] = new Reference($id);
}
krsort($voters);
$voters = call_user_func_array('array_merge', $voters);
$voters = $this->findAndSortTaggedServices('security.voter', $container);
if (!$voters) {
throw new LogicException('No security voters found. You need to tag at least one with "security.voter"');
}

View File

@ -18,6 +18,21 @@ use Symfony\Component\DependencyInjection\Reference;
class AddSecurityVotersPassTest extends TestCase
{
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\LogicException
*/
public function testNoVoters()
{
$container = new ContainerBuilder();
$container
->register('security.access.decision_manager', 'Symfony\Component\Security\Core\Authorization\AccessDecisionManager')
->addArgument(array())
;
$compilerPass = new AddSecurityVotersPass();
$compilerPass->process($container);
}
public function testThatSecurityVotersAreProcessedInPriorityOrder()
{
$container = new ContainerBuilder();
@ -45,15 +60,9 @@ class AddSecurityVotersPassTest extends TestCase
$compilerPass->process($container);
$calls = $container->getDefinition('security.access.decision_manager')->getMethodCalls();
$this->assertEquals(
array(
new Reference('highest_prio_service'),
new Reference('lowest_prio_service'),
new Reference('no_prio_service'),
new Reference('zero_prio_service'),
),
$calls[0][1][0]
);
$refs = $calls[0][1][0];
$this->assertEquals(new Reference('highest_prio_service'), $refs[0]);
$this->assertEquals(new Reference('lowest_prio_service'), $refs[1]);
$this->assertCount(4, $refs);
}
}