8000 [WIP][Security]replaced acl:init command with postGenerateSchema listener by vicb · Pull Request #3560 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP][Security]replaced acl:init command with postGenerateSchema listener #3560

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 2 commits into from
Mar 11, 2012
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
26 changes: 10 additions & 16 deletions src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Security\Acl\Dbal\Schema;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\DBAL\Schema\SchemaException;

/**
* Installs the tables required by the ACL system
Expand Down Expand Up @@ -50,26 +51,19 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$connection = $this->getContainer()->get('security.acl.dbal.connection');
$sm = $connection->getSchemaManager();
$tableNames = $sm->listTableNames();
$tables = array(
'class_table_name' => $this->getContainer()->getParameter('security.acl.dbal.class_table_name'),
'sid_table_name' => $this->getContainer()->getParameter('security.acl.dbal.sid_table_name'),
'oid_table_name' => $this->getContainer()->getParameter('security.acl.dbal.oid_table_name'),
'oid_ancestors_table_name' => $this->getContainer()->getParameter('security.acl.dbal.oid_ancestors_table_name'),
'entry_table_name' => $this->getContainer()->getParameter('security.acl.dbal.entry_table_name'),
);
$container = $this->getContainer();

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

return;
}
try {
$schema->addToSchema($connection->getSchemaManager()->createSchema());
} catch (SchemaException $e) {
$output->writeln("Aborting: " . $e->getMessage());

return;
}

$schema = new Schema($tables, $sm->createSchemaConfig());
foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) {
$connection->exec($sql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ private function addAclSection(ArrayNodeDefinition $rootNode)
->children()
->arrayNode('acl')
->children()
->scalarNode('connection')->setInfo('any name configured in doctrine.dbal section')->end()
->scalarNode('connection')
->defaultValue('default')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the default value should be null to mean the default connection IMO. Nothing enforces to have a connection named default (and this is a regression as it was handled properly before)

->cannotBeEmpty()
->setInfo('any name configured in doctrine.dbal section')
->end()
->a 8000 rrayNode('cache')
->addDefaultsIfNotSet()
->children()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@ private function configureDbalAclProvider(array $config, ContainerBuilder $conta
{
$loader->load('security_acl_dbal.xml');

if (isset($config['connection'])) {
$container->setAlias('security.acl.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));
}
$container->setAlias('security.acl.dbal.connection', sprintf('doctrine.dbal.%s_connection', $config['connection']));

$container
->getDefinition('security.acl.dbal.schema_listener')
->addTag('doctrine.event_listener', array(
'connection' => $config['connection'],
'event' => 'postGenerateSchema'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong indentation :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and you should register the listener lazily as this is supported in DoctrineBundle for 2.1

))
;

$container->getDefinition('security.acl.cache.doctrine')->addArgument($config['cache']['prefix']);

$container->setParameter('security.acl.dbal.class_table_name', $config['tables']['class']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Bundle\SecurityBundle\EventListener;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;

/**
* Merges ACL schema into the given schema.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AclSchemaListener
{
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function postGenerateSchema(GenerateSchemaEventArgs $args)
{
$schema = $args->getSchema();
$this->container->get('security.acl.dbal.schema')->addToSchema($schema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

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

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

<service id="security.acl.dbal.schema" class="%security.acl.dbal.schema.class%">
<argument type="collection">
<argument key="class_table_name">%security.acl.dbal.class_table_name%</argument>
<argument key="entry_table_name">%security.acl.dbal.entry_table_name%</argument>
<argument key="oid_table_name">%security.acl.dbal.oid_table_name%</argument>
<argument key="oid_ancestors_table_name">%security.acl.dbal.oid_ancestors_table_name%</argument>
<argument key="sid_table_name">%security.acl.dbal.sid_table_name%</argument>
</argument>
<argument type="service" id="security.acl.dbal.connection" />
</service>
<service id="security.acl.dbal.schema_listener" class="%security.acl.dbal.schema_listener.class%" public="false">
<argument type="service" id="service_container" />
</service>

<service id="security.acl.provider" alias="security.acl.dbal.provider" />

<service id="security.acl.cache.doctrine" class="%security.acl.cache.doctrine.class%" public="false">
Expand Down
25 changes: 22 additions & 3 deletions src/Symfony/Component/Security/Acl/Dbal/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Doctrine\DBAL\Schema\Schema as BaseSchema;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Connection;

/**
* The schema used for the ACL system.
Expand All @@ -26,11 +27,13 @@ final class Schema extends BaseSchema
/**
* Constructor
*
* @param array $options the names for tables
* @param SchemaConfig $schemaConfig
* @param array $options the names for tables
* @param Connection $connection
*/
public function __construct(array $options, SchemaConfig $schemaConfig = null)
public function __construct(array $options, Connection $connection = null)
{
$schemaConfig = null === $connection ? null : $connection->getSchemaManager()->createSchemaConfig();

parent::__construct(array(), array(), $schemaConfig);

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

/**
* Merges ACL schema with the given schema.
*
* @param BaseSchema $schema
*/
public function addToSchema(BaseSchema $schema)
{
foreach ($this->getTables() as $table) {
$schema->_addTable($table);
}

foreach ($this->getSequences() as $sequence) {
$schema->_addSequence($sequence);
}
}

/**
* Adds the class table to the schema
*/
Expand Down
0