From 6d27aecb02748f6059a194a4eabb8f86e2a894eb Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Sun, 11 Mar 2012 14:50:40 +0100 Subject: [PATCH] [SecurityBundle] Improve the init:acl command --- .../SecurityBundle/Command/InitAclCommand.php | 26 +++++++------------ .../Resources/config/security_acl_dbal.xml | 1 + .../Component/Security/Acl/Dbal/Schema.php | 9 ++++--- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php b/src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php index 0821b42363..5c14a3b58e 100644 --- a/src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php +++ b/src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php @@ -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); } diff --git a/src/Symfony/Bundle/SecurityBundle/Resources/config/security_acl_dbal.xml b/src/Symfony/Bundle/SecurityBundle/Resources/config/security_acl_dbal.xml index d5cbb34f31..6a921159b7 100644 --- a/src/Symfony/Bundle/SecurityBundle/Resources/config/security_acl_dbal.xml +++ b/src/Symfony/Bundle/SecurityBundle/Resources/config/security_acl_dbal.xml @@ -34,6 +34,7 @@ %security.acl.dbal.oid_ancestors_table_name% %security.acl.dbal.sid_table_name% + diff --git a/src/Symfony/Component/Security/Acl/Dbal/Schema.php b/src/Symfony/Component/Security/Acl/Dbal/Schema.php index 7d47e1c023..97376acac6 100644 --- a/src/Symfony/Component/Security/Acl/Dbal/Schema.php +++ b/src/Symfony/Component/Security/Acl/Dbal/Schema.php @@ -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;