Skip to content

Add service definition for PsrAclCache #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ composer.lock
phpunit.xml
.php_cs.cache
.php_cs
.phpunit.result.cache
16 changes: 16 additions & 0 deletions src/DependencyInjection/AclExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Security\Acl\Domain\PsrAclCache;

/**
* AclExtension.
Expand All @@ -42,7 +44,21 @@ public function load(array $configs, ContainerBuilder $container)

if (isset($config['cache']['id'])) {
$container->setAlias('security.acl.cache', $config['cache']['id']);
} elseif (isset($config['cache']['pool'])) {
if (!class_exists(PsrAclCache::class)) {
throw new \LogicException('The "cache.pool" option requires "symfony/security-acl" 3.2 or higher, try upgrading the package.');
}

$container->register('security.acl.cache.psr', PsrAclCache::class)
->setArguments([
new Reference($config['cache']['pool']),
new Reference('security.acl.permission_granting_strategy'),
$config['cache']['prefix'],
]);

$container->setAlias('security.acl.cache', 'security.acl.cache.psr');
}

$container->getDefinition('security.acl.voter.basic_permissions')->addArgument($config['voter']['allow_if_object_identity_unavailable']);

// custom ACL provider
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ public function getConfigTreeBuilder()
->addDefaultsIfNotSet()
->children()
->scalarNode('id')->end()
->scalarNode('pool')->info('The cache pool used to store ACLs')->end()
->scalarNode('prefix')->defaultValue('sf_acl_')->end()
->end()
->validate()
->ifTrue(static function (array $config): bool { return isset($config['id'], $config['pool']); })
->thenInvalid('You cannot set both a cache service id and cache pool')
->end()
->end()
->scalarNode('provider')->end()
->arrayNode('tables')
Expand Down
29 changes: 29 additions & 0 deletions tests/DependencyInjection/CompleteConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\AclBundle\AclBundle;
use Symfony\Bundle\AclBundle\DependencyInjection\AclExtension;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Security\Acl\Dbal\Schema;
use Symfony\Component\Security\Acl\Domain\PsrAclCache;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Core\Role\RoleHierarchy;

Expand All @@ -42,6 +44,33 @@ public function testCustomAclProvider()
$this->assertEquals('foo', (string) $container->getAlias('security.acl.provider'));
}

public function testCachePool()
{
if (!class_exists(PsrAclCache::class)) {
$this->markTestSkipped('Requires symfony/security-acl >=3.2');
}

$container = $this->getContainer('cache_pool');

$this->assertTrue($container->hasDefinition('security.acl.cache.psr'));
$this->assertEquals('security.acl.cache.psr', (string) $container->getAlias('security.acl.cache'));
}

public function testCacheService()
{
$container = $this->getContainer('cache_service');

$this->assertEquals('security.acl.cache.doctrine', (string) $container->getAlias('security.acl.cache'));
}

public function testInvalidCacheConfig()
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Invalid configuration for path "acl.cache": You cannot set both a cache service id and cache pool');

$this->getContainer('invalid_cache_config');
}

protected function getContainer($file)
{
if (isset(self::$containerCache[$file])) {
Expand Down
9 changes: 9 additions & 0 deletions tests/DependencyInjection/Fixtures/php/cache_pool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$this->load('container1.php', $container);

$container->loadFromExtension('acl', [
'cache' => [
'pool' => 'cache.app',
],
]);
9 changes: 9 additions & 0 deletions tests/DependencyInjection/Fixtures/php/cache_service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$this->load('container1.php', $container);

$container->loadFromExtension('acl', [
'cache' => [
'id' => 'security.acl.cache.doctrine',
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('acl', [
'cache' => [
'id' => 'security.acl.cache.doctrine',
'pool' => 'cache.app',
],
]);
13 changes: 13 additions & 0 deletions tests/DependencyInjection/Fixtures/xml/cache_pool.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<srv:container xmlns="http://symfony.com/schema/dic/acl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<config>
<cache>
<pool>cache.app</pool>
</cache>
</config>
</srv:container>
13 changes: 13 additions & 0 deletions tests/DependencyInjection/Fixtures/xml/cache_service.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<srv:container xmlns="http://symfony.com/schema/dic/acl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<config>
<cache>
<id>security.acl.cache.doctrine</id>
</cache>
</config>
</srv:container>
14 changes: 14 additions & 0 deletions tests/DependencyInjection/Fixtures/xml/invalid_cache_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<srv:container xmlns="http://symfony.com/schema/dic/acl"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<config>
<cache>
<id>security.acl.cache.doctrine</id>
<pool>cache.app</pool>
</cache>
</config>
</srv:container>
3 changes: 3 additions & 0 deletions tests/DependencyInjection/Fixtures/yml/cache_pool.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
acl:
cache:
pool: cache.app
3 changes: 3 additions & 0 deletions tests/DependencyInjection/Fixtures/yml/cache_service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
acl:
cache:
id: security.acl.cache.doctrine
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
acl:
cache:
id: security.acl.cache.doctrine
pool: cache.app