InitAclCommand.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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 Doctrine\DBAL\Schema\SchemaException;
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. /**
  16. * Installs the tables required by the ACL system.
  17. *
  18. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  19. */
  20. class InitAclCommand extends ContainerAwareCommand
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function isEnabled()
  26. {
  27. if (!$this->getContainer()->has('security.acl.dbal.connection')) {
  28. return false;
  29. }
  30. return parent::isEnabled();
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function configure()
  36. {
  37. $this
  38. ->setName('init:acl')
  39. ->setDescription('Mounts ACL tables in the database')
  40. ->setHelp(<<<'EOF'
  41. The <info>%command.name%</info> command mounts ACL tables in the database.
  42. <info>php %command.full_name%</info>
  43. 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.
  44. <info>security:
  45. acl:
  46. connection: default</info>
  47. EOF
  48. )
  49. ;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. $container = $this->getContainer();
  57. $connection = $container->get('security.acl.dbal.connection');
  58. $schema = $container->get('security.acl.dbal.schema');
  59. try {
  60. $schema->addToSchema($connection->getSchemaManager()->createSchema());
  61. } catch (SchemaException $e) {
  62. $output->writeln('Aborting: '.$e->getMessage());
  63. return 1;
  64. }
  65. foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) {
  66. $connection->exec($sql);
  67. }
  68. $output->writeln('ACL tables have been initialized successfully.');
  69. }
  70. }