8000 merged branch vicb/acl (PR #3560) · symfony/symfony@fb7b88b · GitHub
[go: up one dir, main page]

Skip to content

Commit fb7b88b

Browse files
committed
merged branch vicb/acl (PR #3560)
Commits ------- 6d27aec [SecurityBundle] Improve the init:acl command e809458 [Security]replaced acl:init command with postGenerateSchema listener Discussion ---------- [WIP][Security]replaced acl:init command with postGenerateSchema listener Attempt to fix #2091. The code is from @schmittjoh (see #1313) but was never merged. The difference is that the `init:acl` command has not been dropped in order to overcome [the limitations](#1313 (comment)) described by @stof. @stof do you think this is ok ? Left to fix: * Using the code from this PR, the generated migration does not drop the tables but still alter them, @stof you know this part of the code far better than me. I would appreacite your feedback on this PR and any hint on solving the remaining item. Thanks.
2 parents d2d7aec + 6d27aec commit fb7b88b

File tree

6 files changed

+99
-23
lines changed

6 files changed

+99
-23
lines changed

src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Acl\Dbal\Schema;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
18+
use Doctrine\DBAL\Schema\SchemaException;
1819

1920
/**
2021
* Installs the tables required by the ACL system
@@ -50,26 +51,19 @@ protected function configure()
5051
*/
5152
protected function execute(InputInterface $input, OutputInterface $output)
5253
{
53-
$connection = $this->getContainer()->get('security.acl.dbal.connection');
54-
$sm = $connection->getSchemaManager();
55-
$tableNames = $sm->listTableNames();
56-
$tables = array(
57-
'class_table_name' => $this->getContainer()->getParameter('security.acl.dbal.class_table_name'),
58-
'sid_table_name' => $this->getContainer()->getParameter('security.acl.dbal.sid_table_name'),
59-
'oid_table_name' => $this->getContainer()->getParameter('security.acl.dbal.oid_table_name'),
60-
'oid_ancestors_table_name' => $this->getContainer()->getParameter('security.acl.dbal.oid_ancestors_table_name'),
61-
'entry_table_name' => $this->getContainer()->getParameter('security.acl.dbal.entry_table_name'),
62-
);
54+
$container = $this->getContainer();
6355

64-
foreach ($tables as $table) {
65-
if (in_array($table, $tableNames, true)) {
66-
$output->writeln(sprintf('The table "%s" already exists. Aborting.', $table));
56+
$connection = $container->get('security.acl.dbal.connection');
57+
$schema = $container->get('security.acl.dbal.schema');
6758

68-
return;
69-
}
59+
try {
60+
$schema->addToSchema($connection->getSchemaManager()->createSchema());
61+
} catch (SchemaException $e) {
62+
$output->writeln("Aborting: " . $e->getMessage());
63+
64+
return;
7065
}
7166

72-
$schema = new Schema($tables, $sm->createSchemaConfig());
7367
foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) {
7468
$connection->exec($sql);
7569
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ private function addAclSection(ArrayNodeDefinition $rootNode)
8989
->children()
9090
->arrayNode('acl')
9191
->children()
92-
->scalarNode('connection')->setInfo('any name configured in doctrine.dbal section')->end()
92+
->scalarNode('connection')
93+
->defaultValue('default')
94+
->cannotBeEmpty()
95+
->setInfo('any name configured in doctrine.dbal section')
96+
->end()
9397
->arrayNode('cache')
9498
->addDefaultsIfNotSet()
9599
->children()

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,16 @@ private function configureDbalAclProvider(array $config, ContainerBuilder $conta
133133
{
134134
$loader->load('security_acl_dbal.xml');
135135

136-
if (isset($config['connection'])) {
137-
$container->setAlias('security.acl.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));
138-
}
136+
$container->setAlias('security.acl.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));
137+
138+
$container
139+
->getDefinition('security.acl.dbal.schema_listener')
140+
->addTag('doctrine.event_listener', array(
141+
'connection' => $config['connection'],
142+
'event' => 'postGenerateSchema'
143+
))
144+
;
145+
139146
$container->getDefinition('security.acl.cache.doctrine')->addArgument($config['cache']['prefix']);
140147

141148
$container->setParameter('security.acl.dbal.class_table_name', $config['tables']['class']);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony framework.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\EventListener;
13+
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
15+
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
16+
17+
/**
18+
* Merges ACL schema into the given schema.
19+
*
20+
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
21+
*/
22+
class AclSchemaListener
23+
{
24+
private $container;
25+
26+
public function __construct(ContainerInterface $container)
27+
{
28+
$this->container = $container;
29+
}
30+
31+
public function postGenerateSchema(GenerateSchemaEventArgs $args)
32+
{
33+
$schema = $args->getSchema();
34+
$this->container->get('security.acl.dbal.schema')->addToSchema($schema);
35+
}
36+
}

src/Symfony/Bundle/SecurityBundle/Resources/config/security_acl_dbal.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<parameters>
88
<parameter key="security.acl.dbal.provider.class">Symfony\Component\Security\Acl\Dbal\MutableAclProvider</parameter>
9+
<parameter key="security.acl.dbal.schema.class">Symfony\Component\Security\Acl\Dbal\Schema</parameter>
10+
<parameter key="security.acl.dbal.schema_listener.class">Symfony\Bundle\SecurityBundle\EventListener\AclSchemaListener</parameter>
911
</parameters>
1012

1113
<services>
@@ -24,6 +26,20 @@
2426
<argument type="service" id="security.acl.cache" on-invalid="null" />
2527
</service>
2628

29+
<service id="security.acl.dbal.schema" class="%security.acl.dbal.schema.class%">
30+
<argument type="collection">
31+
<argument key="class_table_name">%security.acl.dbal.class_table_name%</argument>
32+
<argument key="entry_table_name">%security.acl.dbal.entry_table_name%</argument>
33+
<argument key="oid_table_name">%security.acl.dbal.oid_table_name%</argument>
34+
<argument key="oid_ancestors_table_name">%security.acl.dbal.oid_ancestors_table_name%</argument>
35+
<argument key="sid_table_name">%security.acl.dbal.sid_table_name%</argument>
36+
</argument>
37+
<argument type="service" id="security.acl.dbal.connection" />
38+
</service>
39+
<service id="security.acl.dbal.schema_listener" class="%security.acl.dbal.schema_listener.class%" public="false">
40+
<argument type="service" id="service_container" />
41+
</service>
42+
2743
<service id="security.acl.provider" alias="security.acl.dbal.provider" />
2844

2945
<service id="security.acl.cache.doctrine" class="%security.acl.cache.doctrine.class%" public="false">

src/Symfony/Component/Security/Acl/Dbal/Schema.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Schema\Schema as BaseSchema;
1515
use Doctrine\DBAL\Schema\SchemaConfig;
16+
use Doctrine\DBAL\Connection;
1617

1718
/**
1819
* The schema used for the ACL system.
@@ -26,11 +27,13 @@ final class Schema extends BaseSchema
2627
/**
2728
* Constructor
2829
*
29-
* @param array $options the names for tables
30-
* @param SchemaConfig $schemaConfig
30+
* @param array $options the names for tables
31+
* @param Connection $connection
3132
*/
32-
public function __construct(array $options, SchemaConfig $schemaConfig = null)
33+
public function __construct(array $options, Connection $connection = null)
3334
{
35+
$schemaConfig = null === $connection ? null : $connection->getSchemaManager()->createSchemaConfig();
36+
3437
parent::__construct(array(), array(), $schemaConfig);
3538

3639
$this->options = $options;
@@ -42,6 +45,22 @@ public function __construct(array $options, SchemaConfig $schemaConfig = null)
4245
$this->addEntryTable();
4346
}
4447

48+
/**
49+
* Merges ACL schema with the given schema.
50+
*
51+
* @param BaseSchema $schema
52+
*/
53+
public function addToSchema(BaseSchema $schema)
54+
{
55+
foreach ($this->getTables() as $table) {
56+
$schema->_addTable($table);
57+
}
58+
59+
foreach ($this->getSequences() as $sequence) {
60+
$schema->_addSequence($sequence);
61+
}
62+
}
63+
4564
/**
4665
* Adds the class table to the schema
4766
*/

0 commit comments

Comments
 (0)
0