InitAclCommand.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Command\Command;
  12. use Symfony\Component\Security\Acl\Dbal\Schema;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Doctrine\DBAL\DriverManager;
  16. /**
  17. * Installs the tables required by the ACL system
  18. *
  19. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  20. */
  21. class InitAclCommand extends Command
  22. {
  23. /**
  24. * @see Command
  25. */
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('init:acl')
  30. ;
  31. }
  32. /**
  33. * @see Command
  34. */
  35. protected function execute(InputInterface $input, OutputInterface $output)
  36. {
  37. $connection = $this->container->get('security.acl.dbal.connection');
  38. $sm = $connection->getSchemaManager();
  39. $tableNames = $sm->listTableNames();
  40. $tables = array(
  41. 'class_table_name' => $this->container->getParameter('security.acl.dbal.class_table_name'),
  42. 'sid_table_name' => $this->container->getParameter('security.acl.dbal.sid_table_name'),
  43. 'oid_table_name' => $this->container->getParameter('security.acl.dbal.oid_table_name'),
  44. 'oid_ancestors_table_name' => $this->container->getParameter('security.acl.dbal.oid_ancestors_table_name'),
  45. 'entry_table_name' => $this->container->getParameter('security.acl.dbal.entry_table_name'),
  46. );
  47. foreach ($tables as $table) {
  48. if (in_array($table, $tableNames, true)) {
  49. $output->writeln(sprintf('The table "%s" already exists. Aborting.', $table));
  50. return;
  51. }
  52. }
  53. $schema = new Schema($tables);
  54. foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) {
  55. $connection->exec($sql);
  56. }
  57. $output->writeln('ACL tables have been initialized successfully.');
  58. }
  59. }