diff --git a/bin/console b/bin/console index aebb0ead0b..d32d3e625d 100755 --- a/bin/console +++ b/bin/console @@ -26,7 +26,7 @@ use Alchemy\Phrasea\Command\WebsocketServer; use Alchemy\Phrasea\Core\Version; use Alchemy\Phrasea\Command\BuildMissingSubdefs; use Alchemy\Phrasea\Command\CreateCollection; -use Alchemy\Phrasea\Command\CreateDataboxCommand; +use Alchemy\Phrasea\Command\Databox\CreateDataboxCommand; use Alchemy\Phrasea\Command\MailTest; use Alchemy\Phrasea\Command\Compile\Configuration; use Alchemy\Phrasea\Command\RecordAdd; diff --git a/lib/Alchemy/Phrasea/Command/CreateDataboxCommand.php b/lib/Alchemy/Phrasea/Command/Databox/CreateDataboxCommand.php similarity index 96% rename from lib/Alchemy/Phrasea/Command/CreateDataboxCommand.php rename to lib/Alchemy/Phrasea/Command/Databox/CreateDataboxCommand.php index 068ae5a674..695948f720 100644 --- a/lib/Alchemy/Phrasea/Command/CreateDataboxCommand.php +++ b/lib/Alchemy/Phrasea/Command/Databox/CreateDataboxCommand.php @@ -1,7 +1,8 @@ setName('databox:mount') + ->addArgument('databox', InputArgument::REQUIRED, 'Database name for the databox', null) + ->addArgument('owner', InputArgument::REQUIRED, 'Email of the databox admin user', null) + ->addOption('connection', 'c', InputOption::VALUE_NONE, 'Flag to set new database settings') + ->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host', 'localhost') + ->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port', 3306) + ->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user', 'phrasea') + ->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'MySQL server password', null); + } + + protected function doExecute(InputInterface $input, OutputInterface $output) + { + $databoxName = $input->getArgument('databox'); + $connectionSettings = $input->getOption('connection') == false ? null : new DataboxConnectionSettings( + $input->getOption('db-host'), + $input->getOption('db-port'), + $input->getOption('db-user'), + $input->getOption('db-password') + ); + + /** @var UserRepository $userRepository */ + $userRepository = $this->container['repo.users']; + /** @var DataboxService $databoxService */ + $databoxService = $this->container['databox.service']; + + $owner = $userRepository->findByEmail($input->getArgument('owner')); + + $databoxService->mountDatabox( + $databoxName, + $owner, + $connectionSettings + ); + + $output->writeln('Databox mounted'); + } +} diff --git a/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php b/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php index 93ad9a59a8..d204ed9d8c 100644 --- a/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php +++ b/lib/Alchemy/Phrasea/Controller/Admin/DataboxesController.php @@ -200,6 +200,7 @@ class DataboxesController extends Controller && (null !== $passwordDb = $request->request->get('new_password')) ) { $connection = $this->getApplicationBox()->get_connection(); + try { $connection->beginTransaction(); $base = \databox::mount($this->app, $hostname, $port, $userDb, $passwordDb, $dbName); diff --git a/lib/Alchemy/Phrasea/Core/Provider/DataboxServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/DataboxServiceProvider.php index 5272c1ca47..d1ea0486d6 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/DataboxServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/DataboxServiceProvider.php @@ -2,6 +2,7 @@ namespace Alchemy\Phrasea\Core\Provider; +use Alchemy\Phrasea\Application as PhraseaApplication; use Alchemy\Phrasea\Databox\DataboxService; use Silex\Application; use Silex\ServiceProviderInterface; @@ -11,9 +12,10 @@ class DataboxServiceProvider implements ServiceProviderInterface public function register(Application $app) { - $app['databox.service'] = $app->share(function (Application $app) { + $app['databox.service'] = $app->share(function (PhraseaApplication $app) { return new DataboxService( $app, + $app->getApplicationBox(), $app['dbal.provider'], $app['repo.databoxes'], $app['conf'], diff --git a/lib/Alchemy/Phrasea/Databox/DataboxService.php b/lib/Alchemy/Phrasea/Databox/DataboxService.php index 7d45969936..ec667bebcb 100644 --- a/lib/Alchemy/Phrasea/Databox/DataboxService.php +++ b/lib/Alchemy/Phrasea/Databox/DataboxService.php @@ -18,6 +18,11 @@ class DataboxService */ private $app; + /** + * @var \appbox + */ + private $applicationBox; + /** * @var PropertyAccess */ @@ -40,6 +45,7 @@ class DataboxService /** * @param Application $application + * @param \appbox $appbox * @param callable $connectionFactory * @param DataboxRepository $databoxRepository * @param PropertyAccess $defaultDbConfiguration @@ -47,12 +53,14 @@ class DataboxService */ public function __construct( Application $application, + \appbox $appbox, callable $connectionFactory, DataboxRepository $databoxRepository, PropertyAccess $defaultDbConfiguration, $rootPath ) { $this->app = $application; + $this->applicationBox = $appbox; $this->connectionFactory = $connectionFactory; $this->databoxRepository = $databoxRepository; $this->configuration = $defaultDbConfiguration; @@ -98,14 +106,36 @@ class DataboxService } /** - * @param $databaseName + * @param string $databaseName + * @param User $owner * @param DataboxConnectionSettings $connectionSettings * @return \databox */ - public function mountDatabox($databaseName, DataboxConnectionSettings $connectionSettings = null) + public function mountDatabox($databaseName, User $owner, DataboxConnectionSettings $connectionSettings = null) { $connectionSettings = $connectionSettings ?: DataboxConnectionSettings::fromArray( $this->configuration->get(['main', 'database']) ); + + $this->applicationBox->get_connection()->beginTransaction(); + + try { + $databox = \databox::mount( + $this->app, + $connectionSettings->getHost(), + $connectionSettings->getPort(), + $connectionSettings->getUser(), + $connectionSettings->getPassword(), + $databaseName + ); + + $databox->registerAdmin($owner); + $this->applicationBox->get_connection()->commit(); + } + catch (\Exception $exception) { + $this->applicationBox->get_connection()->rollBack(); + + throw new \RuntimeException($exception->getMessage(), 0, $exception); + } } }