This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Bundle/SecurityBundle/Command/InitAclCommand.php

116 lines
3.2 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\SecurityBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2017-08-06 11:59:30 +01:00
use Symfony\Component\Security\Acl\Dbal\Schema;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\SchemaException;
/**
2014-12-21 17:00:50 +00:00
* Installs the tables required by the ACL system.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*
* @final since version 3.4
*/
2011-06-21 07:09:24 +01:00
class InitAclCommand extends ContainerAwareCommand
{
2017-08-06 11:59:30 +01:00
private $connection;
private $schema;
/**
* @param Connection $connection
* @param Schema $schema
*/
public function __construct($connection = null, Schema $schema = null)
{
if (!$connection instanceof Connection) {
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
parent::__construct($connection);
return;
}
parent::__construct();
$this->connection = $connection;
$this->schema = $schema;
}
/**
* {@inheritdoc}
2017-08-06 11:59:30 +01:00
*
* BC to be removed in 4.0
*/
public function isEnabled()
{
2017-08-06 11:59:30 +01:00
if (!$this->connection && !$this->getContainer()->has('security.acl.dbal.connection')) {
return false;
}
return parent::isEnabled();
}
/**
2014-03-19 20:43:12 +00:00
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('init:acl')
->setDescription('Mounts ACL tables in the database')
2015-12-21 11:01:57 +00:00
->setHelp(<<<'EOF'
The <info>%command.name%</info> command mounts ACL tables in the database.
<info>php %command.full_name%</info>
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.
<info>security:
acl:
connection: default</info>
EOF
)
;
}
/**
2014-03-19 20:43:12 +00:00
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
2017-08-06 11:59:30 +01:00
// BC to be removed in 4.0
if (null === $this->connection) {
$this->connection = $this->getContainer()->get('security.acl.dbal.connection');
$this->schema = $this->getContainer()->get('security.acl.dbal.schema');
}
2011-06-08 11:12:55 +01:00
try {
2017-08-06 11:59:30 +01:00
$this->schema->addToSchema($this->connection->getSchemaManager()->createSchema());
} catch (SchemaException $e) {
$output->writeln('Aborting: '.$e->getMessage());
2012-03-11 17:00:25 +00:00
return 1;
}
2011-12-13 02:38:22 +00:00
2017-08-06 11:59:30 +01:00
foreach ($this->schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
$this->connection->exec($sql);
}
$output->writeln('ACL tables have been initialized successfully.');
}
2011-12-13 02:38:22 +00:00
}