. * --------------------------------------------------------------------- */ namespace Glpi\Console\Security; if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } use Glpi\Console\AbstractCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; use GLPIKey; class ChangekeyCommand extends AbstractCommand { /** * Error code returned when unable to renew key. * * @var integer */ const ERROR_UNABLE_TO_RENEW_KEY = 1; protected function configure() { parent::configure(); $this->setName('glpi:security:change_key'); $this->setDescription(__('Change password storage key and update values in database.')); } protected function execute(InputInterface $input, OutputInterface $output) { $glpikey = new GLPIKey(); $fields = $glpikey->getFields(); $configs = $glpikey->getConfigs(); $conf_count = 0; foreach ($configs as $config) { $conf_count += count($config); } $output->writeln( sprintf( '' . __('Found %1$s field(s) and %2$s configuration entries requiring migration.') . '', count($fields), $conf_count ) ); if (!$input->getOption('no-interaction')) { // Ask for confirmation (unless --no-interaction) $question_helper = $this->getHelper('question'); $run = $question_helper->ask( $input, $output, new ConfirmationQuestion(__('Do you want to continue ?') . ' [Yes/no]', true) ); if (!$run) { $output->writeln( '' . __('Aborted.') . '', OutputInterface::VERBOSITY_VERBOSE ); return 0; } } $created = $glpikey->generate(); if (!$created) { $output->writeln( '' . __('Unable to change security key!') . '', OutputInterface::VERBOSITY_QUIET ); return self::ERROR_UNABLE_TO_RENEW_KEY; } $this->output->write(PHP_EOL); $output->writeln('' . __('New security key generated; database updated.') . ''); return 0; // Success } }