From cfff23082036eef763be50d625487bfd5ba91b7d Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 20 Feb 2020 16:02:50 +0300 Subject: [PATCH 1/2] command databox:mount --- bin/console | 2 + .../Command/Databox/MountDataboxCommand.php | 107 +++++++++++++----- 2 files changed, 81 insertions(+), 28 deletions(-) diff --git a/bin/console b/bin/console index 552cb672d0..fb03725915 100755 --- a/bin/console +++ b/bin/console @@ -24,6 +24,7 @@ use Alchemy\Phrasea\Command\Thesaurus\FindConceptsCommand; use Alchemy\Phrasea\Core\Version; use Alchemy\Phrasea\Command\CreateCollection; use Alchemy\Phrasea\Command\Databox\CreateDataboxCommand; +use Alchemy\Phrasea\Command\Databox\MountDataboxCommand; use Alchemy\Phrasea\Command\MailTest; use Alchemy\Phrasea\Command\Compile\Configuration; use Alchemy\Phrasea\Command\RecordAdd; @@ -109,6 +110,7 @@ $cli->command(new \module_console_fieldsMerge('fields:merge')); $cli->command(new CreateCollection('collection:create')); $cli->command(new CreateDataboxCommand('databox:create')); +$cli->command(new MountDataboxCommand('databox:mount')); $cli->command(new RecordAdd('records:add')); $cli->command(new RescanTechnicalDatas('records:rescan-technical-datas')); diff --git a/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php b/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php index ce77c7dbb2..8b1c9c6f98 100644 --- a/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php +++ b/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php @@ -1,7 +1,16 @@ 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); + parent::__construct('databox:mount'); + + $this->setDescription('Mount databox') + ->addArgument('databox', InputArgument::REQUIRED, 'Database name in Mysql', null) + ->addArgument('owner', InputArgument::REQUIRED, 'The Phraseanet login of the Phraseanet owner (this account became full admin on this databox)', null) + ->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host') + ->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port') + ->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user') + ->addOption('db-password', null, InputOption::VALUE_OPTIONAL, 'MySQL server password') + ; + + return $this; } 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') - ); + try { - /** @var UserRepository $userRepository */ - $userRepository = $this->container['repo.users']; - /** @var DataboxService $databoxService */ - $databoxService = $this->container['databox.service']; + /** @var UserRepository $userRepository */ + $userRepository = $this->container['repo.users']; - $owner = $userRepository->findByEmail($input->getArgument('owner')); + $owner = $userRepository->findByLogin($input->getArgument('owner')); - $databoxService->mountDatabox( - $databoxName, - $owner, - $connectionSettings - ); + if (empty($owner)) { + $output->writeln('User not found ! '); - $output->writeln('Databox mounted'); + return; + } + + if ($owner->isGuest() || !$this->container->getAclForUser($owner)->is_admin()) { + $output->writeln('Admin role is required for the owner ! '); + + return; + } + + $databoxName = $input->getArgument('databox'); + $dialog = $this->getHelperSet()->get('dialog'); + + $connectionSettings = new DataboxConnectionSettings( + $input->getOption('db-host')?:$this->container['conf']->get(['main', 'database', 'host']), + $input->getOption('db-port')?:$this->container['conf']->get(['main', 'database', 'port']), + $input->getOption('db-user')?:$this->container['conf']->get(['main', 'database', 'user']), + $input->getOption('db-password')?:$this->container['conf']->get(['main', 'database', 'password']) + ); + + do { + $continue = mb_strtolower($dialog->ask($output, ' Do you want really mount this databox? (y/N)', 'N')); + } + while ( ! in_array($continue, ['y', 'n'])); + + if ($continue !== 'y') { + $output->writeln('Aborting !'); + + return; + } + + /** @var DataboxService $databoxService */ + $databoxService = $this->container['databox.service']; + + \phrasea::clear_sbas_params($this->container); + + $databox = $databoxService->mountDatabox( + $databoxName, + $owner, + $connectionSettings + ); + + $output->writeln("\n\tData-Box ID ".$databox->get_sbas_id()." mounted successful !\n"); + } catch (\Exception $e) { + $output->writeln('Mount databox failed :'.$e->getMessage().''); + } + + return 0; } + } From 0e116ca07f415a6f4c59820af8cd3a0b9a630250 Mon Sep 17 00:00:00 2001 From: aynsix Date: Fri, 28 Feb 2020 16:30:44 +0300 Subject: [PATCH 2/2] change argument login to user ID --- lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php b/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php index 8b1c9c6f98..bcfe53ae4f 100644 --- a/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php +++ b/lib/Alchemy/Phrasea/Command/Databox/MountDataboxCommand.php @@ -31,7 +31,7 @@ class MountDataboxCommand extends Command $this->setDescription('Mount databox') ->addArgument('databox', InputArgument::REQUIRED, 'Database name in Mysql', null) - ->addArgument('owner', InputArgument::REQUIRED, 'The Phraseanet login of the Phraseanet owner (this account became full admin on this databox)', null) + ->addArgument('user_id', InputArgument::REQUIRED, 'The Id of user owner (this account became full admin on this databox)', null) ->addOption('db-host', null, InputOption::VALUE_OPTIONAL, 'MySQL server host') ->addOption('db-port', null, InputOption::VALUE_OPTIONAL, 'MySQL server port') ->addOption('db-user', null, InputOption::VALUE_OPTIONAL, 'MySQL server user') @@ -48,7 +48,7 @@ class MountDataboxCommand extends Command /** @var UserRepository $userRepository */ $userRepository = $this->container['repo.users']; - $owner = $userRepository->findByLogin($input->getArgument('owner')); + $owner = $userRepository->find($input->getArgument('user_id')); if (empty($owner)) { $output->writeln('User not found ! ');