[SecurityBundle] Improve the init:acl command

This commit is contained in:
Victor Berchet 2012-03-11 14:50:40 +01:00
parent e8094589f0
commit 6d27aecb02
3 changed files with 17 additions and 19 deletions

View File

@ -15,6 +15,7 @@ use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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
@ -50,26 +51,19 @@ EOT
*/
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);
}

View File

@ -34,6 +34,7 @@
<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" />

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\Security\Acl\Dbal;
use Doctrine\DBAL\Schema\Schema as BaseSchema;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Connection;
/**
* The schema used for the ACL system.
@ -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;