|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\SecurityBundle\Command; |
|
13 |
|
14 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
15 use Symfony\Component\Security\Acl\Dbal\Schema; |
|
16 use Symfony\Component\Console\Input\InputInterface; |
|
17 use Symfony\Component\Console\Output\OutputInterface; |
|
18 use Doctrine\DBAL\DriverManager; |
|
19 |
|
20 /** |
|
21 * Installs the tables required by the ACL system |
|
22 * |
|
23 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
24 */ |
|
25 class InitAclCommand extends ContainerAwareCommand |
|
26 { |
|
27 /** |
|
28 * @see Command |
|
29 */ |
|
30 protected function configure() |
|
31 { |
|
32 $this |
|
33 ->setName('init:acl') |
|
34 ->setDescription('Mounts ACL tables in the database') |
|
35 ->setHelp(<<<EOT |
|
36 The <info>init:acl</info> command mounts ACL tables in the database. |
|
37 |
|
38 <info>php app/console ini:acl</info> |
|
39 |
|
40 The name of the DBAL connection must be configured in your <info>app/config/security.yml</info> configuration file in the <info>security.acl.connection</info> variable. |
|
41 |
|
42 <info>security: |
|
43 acl: |
|
44 connection: default</info> |
|
45 EOT |
|
46 ); |
|
47 } |
|
48 |
|
49 /** |
|
50 * @see Command |
|
51 */ |
|
52 protected function execute(InputInterface $input, OutputInterface $output) |
|
53 { |
|
54 $connection = $this->getContainer()->get('security.acl.dbal.connection'); |
|
55 $sm = $connection->getSchemaManager(); |
|
56 $tableNames = $sm->listTableNames(); |
|
57 $tables = array( |
|
58 'class_table_name' => $this->getContainer()->getParameter('security.acl.dbal.class_table_name'), |
|
59 'sid_table_name' => $this->getContainer()->getParameter('security.acl.dbal.sid_table_name'), |
|
60 'oid_table_name' => $this->getContainer()->getParameter('security.acl.dbal.oid_table_name'), |
|
61 'oid_ancestors_table_name' => $this->getContainer()->getParameter('security.acl.dbal.oid_ancestors_table_name'), |
|
62 'entry_table_name' => $this->getContainer()->getParameter('security.acl.dbal.entry_table_name'), |
|
63 ); |
|
64 |
|
65 foreach ($tables as $table) { |
|
66 if (in_array($table, $tableNames, true)) { |
|
67 $output->writeln(sprintf('The table "%s" already exists. Aborting.', $table)); |
|
68 |
|
69 return; |
|
70 } |
|
71 } |
|
72 |
|
73 $schema = new Schema($tables); |
|
74 foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { |
|
75 $connection->exec($sql); |
|
76 } |
|
77 |
|
78 $output->writeln('ACL tables have been initialized successfully.'); |
|
79 } |
|
80 } |