From bb330991aeed5e6d779e27debb42a0242a3decbd Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 22 Aug 2018 15:53:14 +0200 Subject: [PATCH] prepare mergeability Signed-off-by: Thierry Bugier --- hook.php | 25 +- ...{install.class.php => installer.class.php} | 213 ++++---- .../update_to_dev.php} | 0 install/upgrade_to_2.0.php | 490 ------------------ setup.php | 3 - tests/src/Flyvemdm/Tests/CommonTestCase.php | 50 -- tests/suite-install/Config.php | 10 - tests/suite-uninstall/Config.php | 1 + 8 files changed, 106 insertions(+), 686 deletions(-) rename install/{install.class.php => installer.class.php} (90%) rename install/{update_to_develop.php => upgrade/update_to_dev.php} (100%) delete mode 100644 install/upgrade_to_2.0.php diff --git a/hook.php b/hook.php index bda7d3c9..edd9837c 100644 --- a/hook.php +++ b/hook.php @@ -33,15 +33,12 @@ * Entry point for installation process */ function plugin_flyvemdm_install() { - $version = plugin_version_flyvemdm(); - $migration = new Migration($version['version']); - require_once(PLUGIN_FLYVEMDM_ROOT . "/install/install.class.php"); - spl_autoload_register([PluginFlyvemdmInstall::class, 'autoload']); - $install = new PluginFlyvemdmInstall(); - if (!$install->isPluginInstalled()) { - return $install->install($migration); - } - return $install->upgrade($migration); + global $DB; + + require_once(PLUGIN_FLYVEMDM_ROOT . "/install/installer.class.php"); + $installer = new PluginFlyvemdmInstaller(); + + return $installer->install(); } /** @@ -49,10 +46,10 @@ function plugin_flyvemdm_install() { * @return boolean True if success */ function plugin_flyvemdm_uninstall() { - require_once(PLUGIN_FLYVEMDM_ROOT . "/install/install.class.php"); - $install = new PluginFlyvemdmInstall(); + require_once(PLUGIN_FLYVEMDM_ROOT . "/install/installer.class.php"); + $installer = new PluginFlyvemdmInstaller(); - return $install->uninstall(); + return $installer->uninstall(); } /** @@ -158,11 +155,9 @@ function plugin_Flyvemdm_addDefaultWhere($itemtype) { case PluginFlyvemdmAgent::class: return PluginFlyvemdmAgent::addDefaultWhere(); - case PluginFlyvemdmFDroidApplication::class: { + case PluginFlyvemdmFDroidApplication::class: return PluginFlyvemdmFDroidApplication::addDefaultWhere(); - } } - } /** diff --git a/install/install.class.php b/install/installer.class.php similarity index 90% rename from install/install.class.php rename to install/installer.class.php index 0440c933..5544b88d 100644 --- a/install/install.class.php +++ b/install/installer.class.php @@ -39,12 +39,36 @@ * @since 0.1.0 * */ -class PluginFlyvemdmInstall { +class PluginFlyvemdmInstaller { const DEFAULT_CIPHERS_LIST = 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:AES128:AES256:RC4-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!PSK'; const BACKEND_MQTT_USER = 'flyvemdm-backend'; + // Order of this array is mandatory due tu dependancies on install and uninstall + protected static $itemtypesToInstall = [ + 'mqttuser', + // Must be before config because config creates a mqtt user for the plugin + 'mqttacl', + // Must be before config because config creates a mqtt ACL for the plugin + 'config', + 'entityconfig', + 'mqttlog', + 'agent', + 'package', + 'file', + 'fleet', + 'profile', + 'notificationtargetinvitation', + 'geolocation', + 'policy', + 'policycategory', + 'fleet_policy', + 'wellknownpath', + 'invitation', + 'invitationlog', + ]; + protected static $currentVersion = null; protected $migration; @@ -54,7 +78,7 @@ class PluginFlyvemdmInstall { * @param string $classname * @return bool */ - public static function autoload($classname) { + public function autoload($classname) { // useful only for installer GLPI autoloader already handles inc/ folder $filename = dirname(__DIR__) . '/inc/' . strtolower(str_replace('PluginFlyvemdm', '', $classname)) . '.class.php'; @@ -71,34 +95,52 @@ public static function autoload($classname) { * @return boolean true (assume success, needs enhancement) * */ - public function install(Migration $migration) { + public function install() { global $DB; - $this->migration = $migration; spl_autoload_register([__CLASS__, 'autoload']); - $this->installSchema(); - $this->createInitialConfig(); - $this->migration->executeMigration(); - $this->installUpgradeCommonTasks(); - - return true; - } - - protected function installSchema() { - global $DB; - - $this->migration->displayMessage("create database schema"); + $this->migration = new Migration(PLUGIN_FLYVEMDM_VERSION); + $this->migration->setVersion(PLUGIN_FLYVEMDM_VERSION); + + // adding DB model from sql file + // TODO : migrate in-code DB model setup here + if (self::getCurrentVersion() == '') { + // Setup DB model + $dbFile = PLUGIN_FLYVEMDM_ROOT . "/install/mysql/plugin_flyvemdm_empty.sql"; + if (!$DB->runFile($dbFile)) { + $this->migration->displayWarning("Error creating tables : " . $DB->error(), true); + return false; + } - $dbFile = __DIR__ . '/mysql/plugin_flyvemdm_empty.sql'; - if (!$DB->runFile($dbFile)) { - $this->migration->displayWarning("Error creating tables : " . $DB->error(), true); - return false; + $this->createInitialConfig(); + } else { + if (PluginFlyvemdmCommon::endsWith(PLUGIN_FLYVEMDM_VERSION, + "-dev") || (version_compare(self::getCurrentVersion(), + PLUGIN_FLYVEMDM_VERSION) != 0)) { + // TODO : Upgrade (or downgrade) + $this->upgrade(self::getCurrentVersion()); + } } + $this->migration->executeMigration(); + if (version_compare(GLPI_VERSION, '9.3.0') >= 0) { $this->migrateToInnodb(); } + $this->createDirectories(); + $this->createFirstAccess(); + $this->createGuestProfileAccess(); + $this->createAgentProfileAccess(); + $this->createDefaultFleet(); + $this->createPolicies(); + $this->createNotificationTargetInvitation(); + $this->createJobs(); + $this->createRootEntityConfig(); + $this->createDisplayPreferences(); + + Config::setConfigurationValues('flyvemdm', ['version' => PLUGIN_FLYVEMDM_VERSION]); + return true; } @@ -156,45 +198,25 @@ public function createDirectories() { } // Create cache directory for the template engine - if (!file_exists(FLYVEMDM_TEMPLATE_CACHE_PATH)) { - if (!mkdir(FLYVEMDM_TEMPLATE_CACHE_PATH, 0770, true)) { - $this->migration->displayWarning("Cannot create " . FLYVEMDM_TEMPLATE_CACHE_PATH . " directory"); - } + PluginFlyvemdmCommon::recursiveRmdir(FLYVEMDM_TEMPLATE_CACHE_PATH); + if (!mkdir(FLYVEMDM_TEMPLATE_CACHE_PATH, 0770, true)) { + $this->migration->displayWarning("Cannot create " . FLYVEMDM_TEMPLATE_CACHE_PATH . " directory"); } } /** * @return null|string */ - public function getSchemaVersion() { - if ($this->isPluginInstalled()) { - $config = Config::getConfigurationValues('flyvemdm', ['schema_version']); - if (!isset($config['schema_version'])) { - return '0.0'; - } - return $config['schema_version']; - } - - return null; - } - - /** - * is the plugin already installed ? - * - * @return boolean - */ - public function isPluginInstalled() { - global $DB; - - // Check tables of the plugin between 1.1 and 2.0 releases - $result = $DB->query("SHOW TABLES LIKE 'glpi_plugin_flyvemdm_%'"); - if ($result) { - if ($DB->numrows($result) > 0) { - return true; + public static function getCurrentVersion() { + if (self::$currentVersion === null) { + $config = \Config::getConfigurationValues('flyvemdm', ['version']); + if (!isset($config['version'])) { + self::$currentVersion = ''; + } else { + self::$currentVersion = $config['version']; } } - - return false; + return self::$currentVersion; } protected function createRootEntityConfig() { @@ -235,8 +257,6 @@ protected function createFirstAccess() { PluginFlyvemdmInvitation::$rightname => ALLSTANDARDRIGHT, PluginFlyvemdmInvitationLog::$rightname => READ, PluginFlyvemdmTaskstatus::$rightname => READ, - PluginFlyvemdmFDroidApplication::$rightname => READ | UPDATE | READNOTE | UPDATENOTE, - PluginFlyvemdmFDroidMarket::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE, ]; $profileRight->updateProfileRights($_SESSION['glpiactiveprofile']['id'], $newRights); @@ -475,53 +495,22 @@ public function createNotificationTargetInvitation() { /** * Upgrade the plugin to the current code version * - * @param string version to upgrade from + * @param string $fromVersion */ - public function upgrade(Migration $migration) { - spl_autoload_register([__CLASS__, 'autoload']); - - $this->migration = $migration; - $fromSchemaVersion = $this->getSchemaVersion(); - - switch ($fromSchemaVersion) { - case '0.0': - // Upgrade to 2.0 - $this->upgradeOneStep('2.0'); - - case '2.0': - // Example : upgrade to version 2.1 - // $this->upgradeOneStep('2.1'); - - case '3.0': - // Example : upgrade to version 3.0 - // $this->upgradeOneStep('3.0'); - + protected function upgrade($fromVersion) { + switch ($fromVersion) { + case '2.0.0': + // Example : upgrade to version 3.0.0 + // $this->upgradeOneStep('3.0.0'); + case '3.0.0': + // Example : upgrade to version 4.0.0 + // $this->upgradeOneStep('4.0.0'); + + default: } - if (!PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE) { - $this->upgradeOneStep('develop'); + if (PluginFlyvemdmCommon::endsWith(PLUGIN_FLYVEMDM_VERSION, "-dev")) { + $this->upgradeOneStep('dev'); } - $this->installUpgradeCommonTasks(); - return true; - } - - private function installUpgradeCommonTasks() { - $this->createDirectories(); - $this->createFirstAccess(); - $this->createGuestProfileAccess(); - $this->createAgentProfileAccess(); - $this->createDefaultFleet(); - $this->createPolicies(); - $this->createNotificationTargetInvitation(); - $this->createJobs(); - $this->createRootEntityConfig(); - $this->createDisplayPreferences(); - - Config::setConfigurationValues( - 'flyvemdm', [ - 'version' => PLUGIN_FLYVEMDM_VERSION, - 'schema_version' => PLUGIN_FLYVEMDM_SCHEMA_VERSION, - ] - ); } /** @@ -530,19 +519,21 @@ private function installUpgradeCommonTasks() { * @param string $toVersion */ protected function upgradeOneStep($toVersion) { + ini_set("max_execution_time", "0"); ini_set("memory_limit", "-1"); $suffix = str_replace('.', '_', $toVersion); - $includeFile = __DIR__ . "/update_to_$suffix.php"; + $includeFile = __DIR__ . "/upgrade/update_to_$suffix.php"; if (is_readable($includeFile) && is_file($includeFile)) { include_once $includeFile; - $updateClass = "PluginFlyvemdmUpgradeTo$suffix"; - $this->migration->addNewMessageArea("Upgrade to $toVersion"); - $upgradeStep = new $updateClass(); - $upgradeStep->upgrade($this->migration); - $this->migration->executeMigration(); - $this->migration->displayMessage('Done'); + $updateFunction = "plugin_flyvemdm_update_to_$suffix"; + if (function_exists($updateFunction)) { + $this->migration->addNewMessageArea("Upgrade to $toVersion"); + $updateFunction($this->migration); + $this->migration->executeMigration(); + $this->migration->displayMessage('Done'); + } } } @@ -552,18 +543,6 @@ protected function createJobs() { 'comment' => __('Parse uploaded applications to collect metadata', 'flyvemdm'), 'mode' => CronTask::MODE_EXTERNAL, ]); - - CronTask::Register(PluginFlyvemdmFDroidMarket::class, 'UpdateRepositories', DAY_TIMESTAMP, - [ - 'comment' => __('Update the list of applications available from F-Droid like repositories', 'flyvemdm'), - 'mode' => CronTask::MODE_EXTERNAL - ]); - - CronTask::Register(PluginFlyvemdmFDroidApplication::class, 'DownloadApplications', DAY_TIMESTAMP, - [ - 'comment' => __('Imports applications for deployment', 'flyvemdm'), - 'mode' => CronTask::MODE_EXTERNAL - ]); } /** @@ -833,8 +812,6 @@ protected function deleteTables() { PluginFlyvemdmPolicyCategory::getTable(), PluginFlyvemdmWellknownpath::getTable(), PluginFlyvemdmTaskstatus::getTable(), - PluginFlyvemdmFDroidApplication::getTable(), - PluginFlyvemdmFDroidMarket::getTable(), ]; foreach ($tables as $table) { diff --git a/install/update_to_develop.php b/install/upgrade/update_to_dev.php similarity index 100% rename from install/update_to_develop.php rename to install/upgrade/update_to_dev.php diff --git a/install/upgrade_to_2.0.php b/install/upgrade_to_2.0.php deleted file mode 100644 index 920191f2..00000000 --- a/install/upgrade_to_2.0.php +++ /dev/null @@ -1,490 +0,0 @@ - ALLSTANDARDRIGHT , - PluginFlyvemdmInvitationlog::$rightname => READ, - PluginFlyvemdmGeolocation::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE, - ]); - $profileRight->updateProfileRights($profiles_id, $newRights); - - $migration->setContext('flyvemdm'); - $migration->addConfig([ - 'default_agent_url' => PLUGIN_FLYVEMDM_AGENT_DOWNLOAD_URL, - 'show_wizard' => '0', - 'debug_save_inventory' => '0', - 'android_bugcollecctor_url' => '', - 'android_bugcollector_login' => '', - 'android_bugcollector_passwd' => '', - 'invitation_deeplink' => PLUGIN_FLYVEMDM_DEEPLINK, - ]); - - $config = Config::getConfigurationValues('flyvemdm', ['mqtt_broker_tls']); - if (isset($config['mqtt_broker_tls'])) { - if ($config['mqtt_broker_tls'] !== '0') { - $config['mqtt_broker_tls_port'] = $config['mqtt_broker_port']; - $config['mqtt_broker_port'] = '1883'; - } else { - $config['mqtt_broker_tls_port'] = '8883'; - } - // Split TLS setting for client in one hand and backend in the other hand - $config['mqtt_tls_for_clients'] = $config['mqtt_broker_tls']; - $config['mqtt_tls_for_backend'] = $config['mqtt_broker_tls']; - Config::setConfigurationValues('flyvemdm', $config); - Config::deleteConfigurationValues('flyvemdm', ['mqtt_broker_tls']); - } - - // remove download base URL setting - Config::deleteConfigurationValues('flyvemdm', ['deploy_base_url']); - - // update Entity config table - $table = 'glpi_plugin_flyvemdm_entityconfigs'; - $migration->addField($table, 'support_name', 'text', ['after' => 'agent_token_life']); - $migration->addField($table, 'support_phone', 'string', ['after' => 'support_name']); - $migration->addField($table, 'support_website', 'string', ['after' => 'support_phone']); - $migration->addField($table, 'support_email', 'string', ['after' => 'support_website']); - $migration->addField($table, 'support_address', 'text', ['after' => 'support_email']); - $migration->addKey($table, 'entities_id', 'entities_id'); - - // update Agent table - $table = 'glpi_plugin_flyvemdm_agents'; - if (!$DB->fieldExists($table, 'enroll_status')) { - $query = "ALTER TABLE `$table` - ADD COLUMN `enroll_status` ENUM('enrolled', 'unenrolling', 'unenrolled') NOT NULL DEFAULT 'enrolled' AFTER `lock`"; - $DB->query($query) or plugin_flyvemdm_upgrade_error($migration); - } - $migration->addField($table, 'version', 'string', ['after' => 'name']); - $migration->addField($table, 'users_id', 'integer', ['after' => 'computers_id']); - $migration->addField($table, 'is_online', 'integer', ['after' => 'last_contact']); - $migration->addField($table, 'has_system_permission', 'bool', ['after' => 'mdm_type']); - $migration->addKey($table, 'computers_id', 'computers_id'); - $migration->addKey($table, 'users_id', 'users_id'); - $migration->addKey($table, 'entities_id', 'entities_id'); - $migration->changeField($table, 'wipe', 'wipe', 'bool'); - $migration->changeField($table, 'lock', 'lock', 'bool'); - - $enumMdmType = PluginFlyvemdmAgent::getEnumMdmType(); - $currentEnumMdmType = PluginFlyvemdmCommon::getEnumValues($table, 'mdm_type'); - if (count($currentEnumMdmType) > 0) { - // The field exists - if (count($currentEnumMdmType) != count($enumMdmType)) { - reset($enumMdmType); - $defaultValue = key($enumMdmType); - $enumMdmType = "'" . implode("', '", array_keys($enumMdmType)) . "'"; - $query = "ALTER TABLE `$table` - CHANGE COLUMN `mdm_type` `mdm_type` - ENUM($enumMdmType) - NOT NULL DEFAULT '$defaultValue'"; - $DB->query($query) or plugin_flyvemdm_upgrade_error($migration); - } - } else { - // The field does not exists - reset($enumMdmType); - $defaultValue = key($enumMdmType); - $enumMdmType = "'" . implode("', '", array_keys($enumMdmType)) . "'"; - $query = "ALTER TABLE `$table` - ADD COLUMN `mdm_type` - ENUM($enumMdmType) - NOT NULL DEFAULT '$defaultValue'"; - $DB->query($query) or plugin_flyvemdm_upgrade_error($migration); - } - - // Create task status table - $table = 'glpi_plugin_flyvemdm_taskstatuses'; - $query = "CREATE TABLE IF NOT EXISTS `$table` ( - `id` INT(11) NOT NULL AUTO_INCREMENT, - `name` VARCHAR(255) NOT NULL DEFAULT '', - `date_creation` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', - `date_mod` DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00', - `plugin_flyvemdm_agents_id` INT(11) NOT NULL DEFAULT '0', - `plugin_flyvemdm_tasks_id` INT(11) NOT NULL DEFAULT '0', - `status` VARCHAR(255) NOT NULL DEFAULT '', - PRIMARY KEY (`id`), - KEY `plugin_flyvemdm_agents_id` (`plugin_flyvemdm_agents_id`), - KEY `plugin_flyvemdm_tasks_id` (`plugin_flyvemdm_tasks_id`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; - $DB->query($query) or plugin_flyvemdm_upgrade_error($migration); - - $migration->addKey($table, 'plugin_flyvemdm_agents_id', 'plugin_flyvemdm_agents_id'); - $migration->addKey($table, 'plugin_flyvemdm_tasks_id', 'plugin_flyvemdm_tasks_id'); - - // update Policy table - $table = 'glpi_plugin_flyvemdm_policies'; - $migration->addField($table, 'recommended_value', 'string', ['after' => 'default_value']); - $migration->addField($table, 'is_android_system', 'bool', ['after' => 'recommended_value']); - $migration->addField($table, 'android_min_version', 'string', ['after' => 'is_android_system', 'value' => '0']); - $migration->addField($table, 'android_max_version', 'string', ['after' => 'android_min_version', 'value' => '0']); - $migration->addField($table, 'apple_min_version', 'string', ['after' => 'android_max_version', 'value' => '0']); - $migration->addField($table, 'apple_max_version', 'string', ['after' => 'apple_min_version', 'value' => '0']); - $migration->addKey($table, 'group', 'group'); - $migration->addKey($table, 'plugin_flyvemdm_policycategories_id', 'plugin_flyvemdm_policycategories_id'); - $migration->dropField($table, 'is_android_policy'); - $migration->dropField($table, 'is_apple_policy'); - - // Rename and update fleet_policy into task - $table = 'glpi_plugin_flyvemdm_tasks'; - $migration->renameTable('glpi_plugin_flyvemdm_fleets_policies', $table); - $migration->changeField($table, 'plugin_flyvemdm_fleets_policies_id', 'plugin_flyvemdm_tasks_id', - 'integer'); - $migration->addKey($table, 'plugin_flyvemdm_policies_id', 'plugin_flyvemdm_policies_id'); - - // Upgrade schema to apply policies on fleets and agents - if (!$DB->fieldExists($table, 'items_id_applied')) { - $migration->changeField($table, 'plugin_flyvemdm_fleets_id', 'items_id_applied', 'integer'); - $migration->dropKey($table, 'plugin_flyvemdm_fleets_id'); - $migration->addField($table, 'itemtype_applied', 'string', ['after' => 'id']); - $migration->addKey($table, ['itemtype_applied', 'items_id_applied'], 'FK_applied'); - // All tasks already created were applied on fleets - $migration->addPostQuery("UPDATE `$table` SET `itemtype_applied` = 'PluginFlyvemdmFleet'"); - $migration->executeMigration(); - } - - $table = 'glpi_plugin_flyvemdm_policies'; - $policies = [ - 'disableFmRadio', - 'disableVoiceMail', - 'disableCallAutoAnswer', - 'disableVoiceDictation', - 'disableUsbOnTheGo', - 'resetPassword', - 'inventoryFrequency', - 'disableSmsMms', - 'disableStreamVoiceCall', - 'disableCreateVpnProfiles', - ]; - $tasksTable = 'glpi_plugin_flyvemdm_tasks'; - $fleetTable = 'glpi_plugin_flyvemdm_fleets'; - $agentsTable = 'glpi_plugin_flyvemdm_agents'; - $request = [ - 'FIELDS' => [ - $table => ['symbol'], - $tasksTable => ['id', 'itemtype_applied', 'items_id_applied'], - $fleetTable => ['entities_id'], - ], - 'FROM' => $table, - 'INNER JOIN' => [ - $tasksTable => [ - 'FKEY' => [ - $tasksTable => 'plugin_flyvemdm_policies_id', - $table => 'id', - ], - ], - ], - 'LEFT JOIN' => [ - $fleetTable => [ - 'itemtype_applied' => 'PluginFlyvemdmFleet', - 'FKEY' => [ - $tasksTable => 'items_id_applied', - $fleetTable => 'id', - ], - ], - $agentsTable => [ - 'itemtype_applied' => 'PluginFlyvemdmAgent', - 'FKEY' => [ - $tasksTable => 'items_id_applied', - $agentsTable => 'id', - ], - ], - ], - 'WHERE' => [ - 'symbol' => $policies, - ], - ]; - $result = $DB->request($request); - if (count($result) > 0) { - $mqttClient = PluginFlyvemdmMqttclient::getInstance(); - foreach ($result as $data) { - switch ($data['itemtype_applied']) { - case PluginFlyvemdmFleet::class: - $type = 'fleet'; - $entityId = $data['entities_id']; - break; - - case PluginFlyvemdmAgent::class: - $agent = new PluginFlyvemdmAgent(); - $agent->getFromDB($data['items_id_applied']); - $type = 'agent'; - $entityId = $agent->getEntityID(); - break; - - default: - $type = ''; - } - if ($type === '') { - continue; - } - $topic = implode('/', [ - '/', - $entityId, - $type, - $data['items_id_applied'], - 'Policy', - $data['symbol'], - 'Task', - $data['id'] - ]); - $mqttClient->publish($topic, null, 0, 1); - } - } - $policiesStr = implode("','", $policies); - $migration->addPostQuery("DELETE FROM `$table` WHERE `symbol` IN ('" . $policiesStr . "')"); - - // update Applications table - $table = 'glpi_plugin_flyvemdm_packages'; - $migration->addField($table, 'parse_status', "enum('pending', 'parsed', 'failed') NOT NULL DEFAULT 'pending'", - ['after' => 'dl_filename', 'default' => 'pending']); - $migration->addfield($table, 'name', 'string', ['after' => 'id']); - $migration->migrationOneTable($table); - $migration->dropField($table, 'filesize'); - $migration->addField($table, 'name', 'string', ['after' => 'id']); - $migration->addKey($table, 'entities_id', 'entities_id'); - $migration->addPostQuery("UPDATE `$table` SET `parse_status` = 'parsed'"); - $migration->addPostQuery("UPDATE `$table` SET `name` = `package_name`"); - - $result = $DB->request(['FROM' => $table, 'LIMIT' => '1']); - if ($result->count() > 0) { - $result->rewind(); - $row = $result->current(); - if (strpos($row['filename'], 'flyvemdm/package/') !== 0) { - // If there is at least one package and the path does starts with the new prefix, then update all the table - $migration->addPostQuery("UPDATE `$table` SET `filename` = CONCAT('flyvemdm/package/', `filename`)"); - } - } - - $table = 'glpi_plugin_flyvemdm_files'; - $migration->addKey($table, 'entities_id', 'entities_id'); - $migration->addField($table, 'comment', 'text'); - - // Add display preferences for PluginFlyvemdmFile - $query = "SELECT * FROM `glpi_displaypreferences` - WHERE `itemtype` = 'PluginFlyvemdmFile' - AND `users_id`='0'"; - $result=$DB->query($query); - if ($DB->numrows($result) == '0') { - $query = "INSERT INTO `glpi_displaypreferences` (`id`, `itemtype`, `num`, `rank`, `users_id`) - VALUES (NULL, 'PluginFlyvemdmFile', '1', '1', '0'), - (NULL, 'PluginFlyvemdmFile', '4', '2', '0');"; - $DB->query($query); - } - - $table = 'glpi_plugin_flyvemdm_fleets'; - $migration->addField($table, 'is_recursive', 'bool'); - $migration->addKey($table, 'entities_id', 'entities_id'); - - $table = 'glpi_plugin_flyvemdm_geolocations'; - $migration->addKey($table, 'computers_id', 'computers_id'); - - $table = 'glpi_plugin_flyvemdm_mqttacls'; - $migration->addKey($table, 'plugin_flyvemdm_mqttusers_id', 'plugin_flyvemdm_mqttusers_id'); - - $table = 'glpi_plugin_flyvemdm_policycategories'; - $migration->addKey($table, 'plugin_flyvemdm_policycategories_id', 'plugin_flyvemdm_policycategories_id'); - - // Create invitation log table - $table = 'glpi_plugin_flyvemdm_invitationlogs'; - $query = "CREATE TABLE IF NOT EXISTS `$table` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `plugin_flyvemdm_invitations_id` int(11) NOT NULL DEFAULT '0', - `date_creation` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', - `event` varchar(255) NOT NULL DEFAULT '', - PRIMARY KEY (`id`), - INDEX `plugin_flyvemdm_invitations_id` (`plugin_flyvemdm_invitations_id`), - INDEX `date_creation` (`date_creation`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"; - $DB->query($query) or plugin_flyvemdm_upgrade_error($migration); - $migration->addKey($table, 'plugin_flyvemdm_invitations_id', 'plugin_flyvemdm_invitations_id'); - - $table = 'glpi_plugin_flyvemdm_invitations'; - if (!$DB->fieldExists($table, 'name')) { - $invitationName = _n('Invitation', 'Invitations', 1, 'flyvemdm'); - $migration->addField($table, 'name', 'string', ['after' => 'id']); - $migration->addPostQuery("UPDATE `$table` SET `name` = '$invitationName'"); - } - $migration->addKey($table, 'users_id', 'users_id'); - $migration->addKey($table, 'entities_id', 'entities_id'); - $migration->addKey($table, 'documents_id', 'documents_id'); - - // drop Mqtt Update queue - $cronTask = new CronTask(); - $cronTask->deleteByCriteria(['itemtype' => 'PluginFlyvemdmMqttupdatequeue']); - $table = 'glpi_plugin_flyvemdm_mqttupdatequeues'; - $migration->dropTable($table); - - // Fix PascalCase symbols - $query = "UPDATE `glpi_plugin_flyvemdm_policies` - SET `symbol` = 'maximumFailedPasswordsForWipe' - WHERE `symbol`='MaximumFailedPasswordsForWipe'"; - $DB->query($query); - $query = "UPDATE `glpi_plugin_flyvemdm_policies` - SET `symbol` = 'maximumTimeToLock' - WHERE `symbol`='MaximumTimeToLock'"; - $DB->query($query); - - // change MQTT topics tree layout : remove leading slash - $mqttClient = PluginFlyvemdmMqttclient::getInstance(); - $request = [ - 'FIELDS' => [ - 'glpi_plugin_flyvemdm_agents' => ['entities_id'], - 'glpi_computers' => ['serial'], - ], - 'FROM' => 'glpi_plugin_flyvemdm_agents', - 'INNER JOIN' => [ - 'glpi_computers' => ['FKEY' => [ - 'glpi_plugin_flyvemdm_agents' => 'computers_id', - 'glpi_computers' => 'id' - ]] - ], - 'WHERE' => ['lock' => ['<>' => '0']] - ]; - $mqttMessage = ['lock' => 'now']; - $mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES); - foreach ($DB->request($request) as $row) { - $topic = implode('/', [ - $row['entities_id'], - 'agent', - $row['serial'], - 'Command', - 'Lock', - ]); - $mqttClient->publish($topic, $mqttMessage, 0, 1); - $mqttClient->publish('/' . $topic, null, 0, 1); - } - - // re-use previous request array - $request['WHERE'] = ['wipe' => ['<>' => '0']]; - $mqttMessage = ['wipe' => 'now']; - $mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES); - foreach ($DB->request($request) as $row) { - $topic = implode('/', [ - $row['entities_id'], - 'agent', - $row['serial'], - 'Command', - 'Wipe', - ]); - $mqttClient->publish($topic, $mqttMessage, 0, 1); - $mqttClient->publish('/' . $topic, null, 0, 1); - } - - // re-use previous request array - $request['WHERE'] = ['enroll_status' => ['=' => 'unenrolling']]; - $mqttMessage = ['unenroll' => 'now']; - $mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES); - foreach ($DB->request($request) as $row) { - $topic = implode('/', [ - $row['entities_id'], - 'agent', - $row['serial'], - 'Command', - 'Unenroll', - ]); - $mqttClient->publish($topic, $mqttMessage, 0, 1); - $mqttClient->publish('/' . $topic, null, 0, 1); - } - - $request = [ - 'FIELDS' => [ - 'glpi_plugin_flyvemdm_tasks' => ['id', 'itemtype_applied', 'items_id_applied', 'plugin_flyvemdm_policies_id', 'itemtype', 'items_id', 'value'], - 'glpi_plugin_flyvemdm_policies' => ['symbol'], - 'glpi_plugin_flyvemdm_fleets' => ['entities_id'] - ], - 'FROM' => 'glpi_plugin_flyvemdm_tasks', - 'INNER JOIN' => [ - 'glpi_plugin_flyvemdm_policies' => [ - 'FKEY' => [ - 'glpi_plugin_flyvemdm_tasks' => 'plugin_flyvemdm_policies_id', - 'glpi_plugin_flyvemdm_policies' => 'id' - ] - ], - 'glpi_plugin_flyvemdm_fleets' => [ - 'FKEY' => [ - 'glpi_plugin_flyvemdm_tasks' => 'items_id_applied', - 'glpi_plugin_flyvemdm_fleets' => 'id' - ] - ] - ], - 'WHERE' => [ - 'glpi_plugin_flyvemdm_tasks.itemtype_applied' => 'PluginFlyvemdmFleet' - ] - ]; - foreach ($DB->request($request) as $row) { - switch ($row['itemtype_applied']) { - case PluginFlyvemdmFleet::class: - $type = 'fleet'; - break; - - case PluginFlyvemdmAgent::class: - $type = 'agent'; - break; - - default: - $type = ''; - } - if ($type === '') { - continue; - } - $topic = implode('/', [ - $row['entities_id'], - 'fleet', - $row['items_id_applied'], - 'Policy', - $row['symbol'], - ]); - $policyFactory = new PluginFlyvemdmPolicyFactory(); - $appliedPolicy = $policyFactory->createFromDBByID($row['plugin_flyvemdm_policies_id']); - $policyMessage = $appliedPolicy->getMqttMessage( - $row['value'], - $row['itemtype'], - $row['items_id'] - ); - $policyMessage['taskId'] = $row['id']; - $encodedMessage = json_encode($policyMessage, JSON_UNESCAPED_SLASHES); - $mqttClient->publish("$topic/Task/" . $row['id'], $encodedMessage, 0, 1); - $mqttClient->publish('/' . $topic, null, 0, 1); - } - } -} \ No newline at end of file diff --git a/setup.php b/setup.php index 2e62b0ff..a097266c 100644 --- a/setup.php +++ b/setup.php @@ -29,10 +29,7 @@ * ------------------------------------------------------------------------------ */ -// Version of the plugin define('PLUGIN_FLYVEMDM_VERSION', 'develop'); -// Schema version of this version -define('PLUGIN_FLYVEMDM_SCHEMA_VERSION', '2.0'); // is or is not an official release of the plugin define('PLUGIN_FLYVEMDM_IS_OFFICIAL_RELEASE', false); // Minimal GLPI version, inclusive diff --git a/tests/src/Flyvemdm/Tests/CommonTestCase.php b/tests/src/Flyvemdm/Tests/CommonTestCase.php index 6ad77356..cbfa8f5c 100644 --- a/tests/src/Flyvemdm/Tests/CommonTestCase.php +++ b/tests/src/Flyvemdm/Tests/CommonTestCase.php @@ -186,56 +186,6 @@ protected function createFleet($input) { return $fleet; } - /** - * @param array $input input data - * @return \PluginFlyvemdmTask - */ - protected function createTask($input) { - $task = $this->newMockInstance(\PluginFlyvemdmTask::class, '\MyMock'); - $task->getMockController()->post_addItem = function () {}; - $taskId = $task->add($input); - $this->boolean($task->isNewItem())->isFalse(); - - $task = new \PluginFlyvemdmTask(); - $task->getFromDB($taskId); - - return $task; - } - - /** - * @param string $policySymbol - * @return array - */ - protected function createFleetAndTask($policySymbol = 'storageEncryption') { - $fleet = $this->createFleet(['name' => $this->getUniqueString()]); - $policy = $this->newMockInstance(\PluginFlyvemdmPolicy::class, '\MyMock'); - $policy->getFromDbBySymbol($policySymbol); - $task = $this->createTask([ - 'value' => '0', - 'plugin_flyvemdm_policies_id' => $policy->getID(), - 'itemtype_applied' => \PluginFlyvemdmFleet::class, - 'items_id_applied' => $fleet->getID(), - 'itemtype' => '', - 'items_id' => '', - ]); - return [$fleet, $task]; - } - - /** - * @return array - */ - protected function createAgentTaskstatus() { - list($fleet, $task) = $this->createFleetAndTask(); - $agent = $this->createAgent(); - $agent->update([\PluginFlyvemdmFleet::getForeignKeyField() => $fleet->getID()]); - $taskStatus = $this->newMockInstance(\PluginFlyvemdmTaskStatus::class, '\MyMock'); - $taskStatus->getFromDBByCrit([ - \PluginFlyvemdmAgent::getForeignKeyField() => $agent->getID(), - \PluginFlyvemdmTask::getForeignKeyField() => $task->getID(), - ]); - return [$taskStatus, $fleet, $task]; - } - /** * Create a new enrolled agent in the database * diff --git a/tests/suite-install/Config.php b/tests/suite-install/Config.php index c6490f82..360d43df 100644 --- a/tests/suite-install/Config.php +++ b/tests/suite-install/Config.php @@ -114,16 +114,6 @@ public function testInstallPlugin() { $plugin->activate($plugin->fields['id']); $this->boolean($plugin->isActivated($pluginName))->isTrue('Cannot enable the plugin'); - // Check version and schema version are in the configuration - $config = \Config::getConfigurationValues( - $pluginName, [ - 'version', - 'schema_version' - ] - ); - $this->string($config['version']); - $this->string($config['schema_version']); - // Enable debug mode for enrollment messages \Config::setConfigurationValues($pluginName, ['debug_enrolment' => '1']); diff --git a/tests/suite-uninstall/Config.php b/tests/suite-uninstall/Config.php index 6b001826..4be84def 100644 --- a/tests/suite-uninstall/Config.php +++ b/tests/suite-uninstall/Config.php @@ -37,6 +37,7 @@ class Config extends CommonTestCase { public function beforeTestMethod($method) { + $this->resetState(); parent::beforeTestMethod($method); $this->setupGLPIFramework(); }