Commit c3b757df authored by Cédric Anne's avatar Cédric Anne Committed by Johan Cwiklinski

Prevent console commands to be executed if DB is not up-to-date

parent 5bd77827
......@@ -64,12 +64,19 @@ abstract class AbstractCommand extends Command implements GlpiCommandInterface {
protected $output;
/**
* Flag to indicate if command requires a BD connection.
* Flag to indicate if command requires a DB connection.
*
* @var boolean
*/
protected $requires_db = true;
/**
* Flag to indicate if command requires an up-to-date DB.
*
* @var boolean
*/
protected $requires_db_up_to_date = true;
protected function initialize(InputInterface $input, OutputInterface $output) {
$this->input = $input;
......@@ -199,4 +206,9 @@ abstract class AbstractCommand extends Command implements GlpiCommandInterface {
return true;
}
public function requiresUpToDateDb(): bool {
return $this->requires_db && $this->requires_db_up_to_date;
}
}
......@@ -68,6 +68,13 @@ class Application extends BaseApplication {
*/
const ERROR_MISSING_REQUIREMENTS = 128; // start application codes at 128 be sure to be different from commands codes
/**
* Error code returned when DB is not up-to-date.
*
* @var integer
*/
const ERROR_DB_OUTDATED = 129;
/**
* Pointer to $CFG_GLPI.
* @var array
......@@ -224,6 +231,16 @@ class Application extends BaseApplication {
$begin_time = microtime(true);
if ($command instanceof GlpiCommandInterface && $command->requiresUpToDateDb()
&& (!array_key_exists('dbversion', $this->config) || (trim($this->config['dbversion']) != GLPI_SCHEMA_VERSION))) {
$output->writeln(
'<error>'
. __('The version of the database is not compatible with the version of the installed files. An update is necessary.')
. '</error>'
);
return self::ERROR_DB_OUTDATED;
}
if ($command instanceof GlpiCommandInterface && $command->mustCheckMandatoryRequirements()
&& !$this->checkCoreMandatoryRequirements()) {
return self::ERROR_MISSING_REQUIREMENTS;
......
......@@ -39,9 +39,16 @@ if (!defined('GLPI_ROOT')) {
interface GlpiCommandInterface {
/**
* Defines whether or mandatory requirements must be checked before running command.
* Defines whether or not mandatory requirements must be checked before running command.
*
* @return boolean
*/
public function mustCheckMandatoryRequirements(): bool;
/**
* Defines whether or not command requires an up-to-date database to be executed.
*
* @return boolean
*/
public function requiresUpToDateDb(): bool;
}
......@@ -93,6 +93,8 @@ abstract class AbstractConfigureCommand extends AbstractCommand implements Force
*/
const ERROR_DB_CONFIG_FILE_NOT_SAVED = 4;
protected $requires_db_up_to_date = false;
protected function configure() {
parent::configure();
......
......@@ -64,6 +64,8 @@ class UpdateCommand extends AbstractCommand implements ForceNoPluginsOptionComma
*/
const ERROR_MISSING_SECURITY_KEY_FILE = 2;
protected $requires_db_up_to_date = false;
protected function configure() {
parent::configure();
......
......@@ -44,6 +44,8 @@ use Symfony\Component\Console\Output\OutputInterface;
class DisableMaintenanceModeCommand extends AbstractCommand {
protected $requires_db_up_to_date = false;
protected function configure() {
parent::configure();
......
......@@ -45,6 +45,8 @@ use Symfony\Component\Console\Output\OutputInterface;
class EnableMaintenanceModeCommand extends AbstractCommand {
protected $requires_db_up_to_date = false;
protected function configure() {
parent::configure();
......
......@@ -42,6 +42,8 @@ use Symfony\Component\Console\Output\OutputInterface;
class ClearCacheCommand extends Command {
protected $requires_db_up_to_date = false;
protected function configure() {
parent::configure();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment