From 2b4ffcffa3046471aff2ff2de595399cd68dbea4 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 10 Oct 2018 17:49:46 +0200 Subject: [PATCH 1/8] fix(install): fix tables detection for isntall / upgrade branching Signed-off-by: Thierry Bugier --- install/install.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/install.class.php b/install/install.class.php index 51c82a6e..c980239c 100644 --- a/install/install.class.php +++ b/install/install.class.php @@ -186,7 +186,7 @@ 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_%'"); + $result = $DB->query("SHOW TABLES LIKE 'glpi_plugin_flyvemdm\\_%'"); if ($result) { if ($DB->numrows($result) > 0) { return true; From 1a9e59fae4933548f000e85b43ca4873fade4923 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 10 Oct 2018 17:50:18 +0200 Subject: [PATCH 2/8] fix(notification): quite problem in generated emails impacts only some email clients Signed-off-by: Thierry Bugier --- install/install.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install/install.class.php b/install/install.class.php index c980239c..a2964f20 100644 --- a/install/install.class.php +++ b/install/install.class.php @@ -440,7 +440,7 @@ public function createNotificationTargetInvitation() { 'language' => '', 'subject' => addcslashes($data['subject'], "'\""), 'content_text' => addcslashes($data['content_text'], "'\""), - 'content_html' => addcslashes(htmlentities($contentHtml), "'\""), + 'content_html' => addcslashes(htmlentities($contentHtml, ENT_NOQUOTES | ENT_HTML401), "'\""), ]); // Create the notification From 94d899fcfe76b2ee21dd04f7291eaf2bd1c44d6d Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Fri, 5 Oct 2018 16:30:20 +0200 Subject: [PATCH 3/8] ci(travis): update matrix Signed-off-by: Thierry Bugier --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index e18efbb5..5e061496 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,8 +13,8 @@ env: - AFTER_SUCCESS_BRANCH=9.3/bugfixes - secure: Y9sG3lZ3Fn3t5BXvcMJQxWsdSUVQgF4M08E6oouYrRc95HEj3ZwZOqp6Df58u8CQFA0EKJyvCBLn8UicvHGMKAD0RwGLBdSP4Ji9gJRZkyMZi79awSshdva/c8dqVQrRd4asuTNQfcagVJpNDnY8sYusw504JUilK3vFVp+39nNZUkcvT69NGVIlXzgHTYinBkVuqDhf5eVtcLcaESLEshrg+5ZERdm+0KifdJVREzhicJxofnmTl/wBsIP7XiQqspljf2/SxLqreGmWXYXUfqIwIOVtsd9fkZChQCz8USC7P427tH6styRDYuMCtvA9b/T/XacSdKFbuDezff3NbIM3b5BebDyCrOK5MGSOdRUY5RuyZN4R5LjePUE++9QNCUPeDSkfb23v0VfuqXIRAxfdtik517GzFy6O7/e6FU1msVZlGQED7Uek9nqnupj+0lIq+99Jcm1UCNJu1NTL2Tv80XXqySaxyE4Sedq/FiYAsy1bo2cg2367I2b4FhFXaJCKkFHcdjHXAeurkRy/brSPhBNoOO5/GA3RepUErgly4P8TLZqHNZv8rgMUoQ88sdwDyXG7dY4UwWiTlCkxMBTqBqJanlTMA9zn2bYyMDioTGnA7+VYAA8cddjFOMVrmmuFJ+YV2x1+5B5qd+Wt8RPwcPQDyqaiN5amb1HGeMA= matrix: - - GLPI_BRANCH=9.3.0 FI_BRANCH=glpi9.3+1.0 - - GLPI_BRANCH=9.3/bugfixes FI_BRANCH=glpi9.3+1.0 + - GLPI_BRANCH=9.3.0 FI_BRANCH=glpi9.3+1.1 + - GLPI_BRANCH=9.3/bugfixes FI_BRANCH=glpi9.3 - GLPI_BRANCH=master FI_BRANCH=master php: From a35697de11bca503969b0570ffdff994ef2135a4 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 10 Oct 2018 21:01:45 +0200 Subject: [PATCH 4/8] test: add a patch for Fusion Inventory see PR #763 for Fusion Inventory, planned for GLPI 9.3.2 Signed-off-by: Thierry Bugier --- tests/before_script.sh | 11 +- ...atch => allow-test-on-master-branch.patch} | 0 .../fusioninventory/compat-glpi-9-3-2.diff | 142 ++++++++++++++++++ .../raise-max-version.patch} | 0 ...8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff | 0 5 files changed, 149 insertions(+), 4 deletions(-) rename tests/patches/{glpi-allow-test-on-master-branch.patch => allow-test-on-master-branch.patch} (100%) create mode 100644 tests/patches/fusioninventory/compat-glpi-9-3-2.diff rename tests/patches/{fi-raise-max-version.patch => fusioninventory/raise-max-version.patch} (100%) rename tests/patches/{ => glpi}/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff (100%) diff --git a/tests/before_script.sh b/tests/before_script.sh index ee83c6f3..6814d117 100755 --- a/tests/before_script.sh +++ b/tests/before_script.sh @@ -33,16 +33,19 @@ composer install --no-dev --no-interaction mkdir plugins/fusioninventory && git clone --depth=35 $FI_SOURCE -b $FI_BRANCH plugins/fusioninventory IFS=/ read -a repo <<< $TRAVIS_REPO_SLUG mv ../${repo[1]} plugins/flyvemdm + +# patch Fusion Inventory when needed cd plugins/fusioninventory -if [[ $FI_BRANCH == "glpi9.2+1.0" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fi-fix-obsolete-query.patch; fi -if [[ $FI_BRANCH == "master" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fi-raise-max-version.patch; fi +if [[ $FI_BRANCH == "master" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fusioninventory/fi-raise-max-version.patch; fi +if [[ $FI_BRANCH == "master" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fusioninventory/compat-glpi-9-3-2.diff; fi +if [[ $FI_BRANCH == "glpi9.3" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fusioninventory/compat-glpi-9-3-2.diff; fi cd ../.. # patch GLPI when needed -if [[ $GLPI_BRANCH == "9.2.1" ]] ; then patch -p1 --batch < plugins/flyvemdm/tests/patches/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff; fi +if [[ $GLPI_BRANCH == "9.2.1" ]] ; then patch -p1 --batch < plugins/flyvemdm/tests/patches/glpi/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff; fi # prepare plugin to test cd plugins/flyvemdm -if [[ $GLPI_BRANCH == "master" ]] ; then patch -p1 --batch < tests/patches/glpi-allow-test-on-master-branch.patch; fi +if [[ $GLPI_BRANCH == "master" ]] ; then patch -p1 --batch < tests/patches/allow-test-on-master-branch.patch; fi composer install --no-interaction diff --git a/tests/patches/glpi-allow-test-on-master-branch.patch b/tests/patches/allow-test-on-master-branch.patch similarity index 100% rename from tests/patches/glpi-allow-test-on-master-branch.patch rename to tests/patches/allow-test-on-master-branch.patch diff --git a/tests/patches/fusioninventory/compat-glpi-9-3-2.diff b/tests/patches/fusioninventory/compat-glpi-9-3-2.diff new file mode 100644 index 00000000..4697336d --- /dev/null +++ b/tests/patches/fusioninventory/compat-glpi-9-3-2.diff @@ -0,0 +1,142 @@ +diff --git a/inc/collectrule.class.php b/inc/collectrule.class.php +index 55e700c0f..9df0c229b 100644 +--- a/inc/collectrule.class.php ++++ b/inc/collectrule.class.php +@@ -115,7 +115,7 @@ class PluginFusioninventoryCollectRule extends Rule { + * @param array $params + * @return array + */ +- function executeActions($output, $params) { ++ function executeActions($output, $params, array $input = []) { + + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-collect", +@@ -181,7 +181,7 @@ class PluginFusioninventoryCollectRule extends Rule { + default: + //plugins actions + $executeaction = clone $this; +- $output = $executeaction->executePluginsActions($action, $output, $params); ++ $output = $executeaction->executePluginsActions($action, $output, $params, $input); + break; + + } +diff --git a/inc/formatconvert.class.php b/inc/formatconvert.class.php +index 506cb2aec..59ad31d96 100644 +--- a/inc/formatconvert.class.php ++++ b/inc/formatconvert.class.php +@@ -1598,7 +1598,8 @@ class PluginFusioninventoryFormatconvert { + 'VERSION' => 'version', + 'COMMENTS' => 'comment', + 'INSTALLDATE' => 'date_install', +- 'SYSTEM_CATEGORY' => '_system_category']); ++ 'SYSTEM_CATEGORY' => '_system_category', ++ 'OPERATIONG_SYSTEM' => 'operatingsystems_id']); + if (!isset($array_tmp['name']) + || $array_tmp['name'] == '') { + if (isset($a_softwares['GUID']) +@@ -1632,11 +1633,13 @@ class PluginFusioninventoryFormatconvert { + //for software dictionnary + if ($nb_RuleDictionnarySoftware > 0) { + $rule_input = [ +- "name" => $array_tmp['name'], +- "manufacturer" => $array_tmp['manufacturers_id'], +- "old_version" => $array_tmp['version'], +- "entities_id" => $entities_id_software, +- "_system_category" => $array_tmp['_system_category'] ++ "name" => $array_tmp['name'], ++ "manufacturer" => $array_tmp['manufacturers_id'], ++ "old_version" => $array_tmp['version'], ++ "entities_id" => $entities_id_software, ++ "operatingsystems_id" => $array_tmp['operatingsystems_id'], ++ "_system_category" => $array_tmp['_system_category'], ++ "device_id" => isset($a_inventory['Computer']['device_id']) ? $a_inventory['Computer']['device_id'] : 0, + ]; + $res_rule = $rulecollection->processAllRules($rule_input); + } +diff --git a/inc/inventorycomputerinventory.class.php b/inc/inventorycomputerinventory.class.php +index 8b0a1186c..aae4783a3 100644 +--- a/inc/inventorycomputerinventory.class.php ++++ b/inc/inventorycomputerinventory.class.php +@@ -474,6 +474,7 @@ class PluginFusioninventoryInventoryComputerInventory { + $a_computerinventory, + $items_id); + } ++ $a_computerinventory['Computer']['device_id'] = $this->device_id; + $a_computerinventory = $pfFormatconvert->computerSoftwareTransformation( + $a_computerinventory, + $entities_id); +@@ -581,7 +582,7 @@ class PluginFusioninventoryInventoryComputerInventory { + 'computer'); + } + } else if ($itemtype == 'PluginFusioninventoryUnmanaged') { +- ++ $a_computerinventory['Computer']['device_id'] = $this->device_id; + $a_computerinventory = $pfFormatconvert->computerSoftwareTransformation( + $a_computerinventory, + $entities_id); +diff --git a/inc/inventoryruleentity.class.php b/inc/inventoryruleentity.class.php +index c4e57a977..a13e1ad7f 100644 +--- a/inc/inventoryruleentity.class.php ++++ b/inc/inventoryruleentity.class.php +@@ -117,7 +117,7 @@ class PluginFusioninventoryInventoryRuleEntity extends Rule { + * @param array $params + * @return array + */ +- function executeActions($output, $params) { ++ function executeActions($output, $params, array $input = []) { + + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-entity", +@@ -395,7 +395,7 @@ class PluginFusioninventoryInventoryRuleEntity extends Rule { + if ($this->checkCriterias($input)) { + unset($output["_no_rule_matches"]); + $refoutput = $output; +- $output = $this->executeActions($output, $params); ++ $output = $this->executeActions($output, $params, $input); + if (!isset($output['pass_rule'])) { + $this->updateOnlyCriteria($options, $refoutput, $output); + //Hook +diff --git a/inc/inventoryruleimport.class.php b/inc/inventoryruleimport.class.php +index a9454f521..f60115f41 100644 +--- a/inc/inventoryruleimport.class.php ++++ b/inc/inventoryruleimport.class.php +@@ -716,7 +716,7 @@ class PluginFusioninventoryInventoryRuleImport extends Rule { + * @param array $params + * @return array + */ +- function executeActions($output, $params) { ++ function executeActions($output, $params, array $input = []) { + if (isset($params['class'])) { + $class = $params['class']; + } else if (isset($_SESSION['plugin_fusioninventory_classrulepassed'])) { +diff --git a/inc/inventoryrulelocation.class.php b/inc/inventoryrulelocation.class.php +index f0e8edeab..05ddf6199 100644 +--- a/inc/inventoryrulelocation.class.php ++++ b/inc/inventoryrulelocation.class.php +@@ -117,7 +117,7 @@ class PluginFusioninventoryInventoryRuleLocation extends Rule { + * @param array $params + * @return array + */ +- function executeActions($output, $params) { ++ function executeActions($output, $params, array $input = []) { + + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-location", +diff --git a/phpunit/1_Unit/SoftwareUpdateTest.php b/phpunit/1_Unit/SoftwareUpdateTest.php +index d5203f33a..f30f94428 100644 +--- a/phpunit/1_Unit/SoftwareUpdateTest.php ++++ b/phpunit/1_Unit/SoftwareUpdateTest.php +@@ -162,9 +162,12 @@ class SoftwareUpdateTest extends RestoreDatabase_TestCase { + 'PUBLISHER' => 'fusioninventory team', + 'NAME' => 'fusioninventory', + 'VERSION' => '0.85+1.0', +- 'SYSTEM_CATEGORY' => 'devel' ++ 'SYSTEM_CATEGORY' => 'devel', ++ 'OPERATIONG_SYSTEM' => 0 + ]; + ++ $a_software['Computer']['device_id'] = 0; ++ + $pfFormatconvert = new PluginFusioninventoryFormatconvert(); + $a_return = $pfFormatconvert->computerSoftwareTransformation($a_software, 0); + diff --git a/tests/patches/fi-raise-max-version.patch b/tests/patches/fusioninventory/raise-max-version.patch similarity index 100% rename from tests/patches/fi-raise-max-version.patch rename to tests/patches/fusioninventory/raise-max-version.patch diff --git a/tests/patches/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff b/tests/patches/glpi/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff similarity index 100% rename from tests/patches/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff rename to tests/patches/glpi/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff From d9e2b18fe86557e516fcbdb371a23d7d83f3c050 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Thu, 11 Oct 2018 10:31:45 +0200 Subject: [PATCH 5/8] build: remove tests on glpi 9.2 bugfixes this version is no longer maitnained, development focused on 9.3 Signed-off-by: Thierry Bugier --- tests/before_script.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/before_script.sh b/tests/before_script.sh index 6814d117..4a835f1d 100755 --- a/tests/before_script.sh +++ b/tests/before_script.sh @@ -42,7 +42,7 @@ if [[ $FI_BRANCH == "glpi9.3" ]] ; then patch -p1 --batch < ../flyvemdm/tests/pa cd ../.. # patch GLPI when needed -if [[ $GLPI_BRANCH == "9.2.1" ]] ; then patch -p1 --batch < plugins/flyvemdm/tests/patches/glpi/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff; fi +# if [[ $GLPI_BRANCH == "9.2.1" ]] ; then patch -p1 --batch < plugins/flyvemdm/tests/patches/glpi/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff; fi # prepare plugin to test cd plugins/flyvemdm From 2b1a3b0df8760a3ace52632d3e2c8c9fe0f5a440 Mon Sep 17 00:00:00 2001 From: Domingo Oropeza Date: Thu, 4 Oct 2018 15:16:39 -0400 Subject: [PATCH 6/8] refactor: rename broker message methods Signed-off-by: Domingo Oropeza --- inc/policyboolean.class.php | 2 +- inc/policydeployapplication.class.php | 2 +- inc/policydeployfile.class.php | 2 +- inc/policydropdown.class.php | 2 +- inc/policyinteger.class.php | 2 +- inc/policyinterface.class.php | 2 +- inc/policyremoveapplication.class.php | 2 +- inc/policyremovefile.class.php | 2 +- inc/policystring.class.php | 2 +- inc/task.class.php | 4 ++-- install/upgrade_to_2_0.php | 2 +- tests/suite-unit/PluginFlyvemdmPolicyBoolean.php | 6 +++--- tests/suite-unit/PluginFlyvemdmPolicyDeployapplication.php | 4 ++-- tests/suite-unit/PluginFlyvemdmPolicyDeployfile.php | 4 ++-- tests/suite-unit/PluginFlyvemdmPolicyDropdown.php | 4 ++-- tests/suite-unit/PluginFlyvemdmPolicyInteger.php | 4 ++-- tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php | 4 ++-- tests/suite-unit/PluginFlyvemdmPolicyString.php | 2 +- 18 files changed, 26 insertions(+), 26 deletions(-) diff --git a/inc/policyboolean.class.php b/inc/policyboolean.class.php index 4ac1ca7b..8b537d26 100644 --- a/inc/policyboolean.class.php +++ b/inc/policyboolean.class.php @@ -71,7 +71,7 @@ public function integrityCheck($value, $itemtype, $itemId) { * @param integer $itemId * @return array|bool */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { if (!$this->integrityCheck($value, $itemtype, $itemId)) { return false; } diff --git a/inc/policydeployapplication.class.php b/inc/policydeployapplication.class.php index 25590b33..e0c25eda 100644 --- a/inc/policydeployapplication.class.php +++ b/inc/policydeployapplication.class.php @@ -93,7 +93,7 @@ public function integrityCheck($value, $itemtype, $itemId) { * @param integer $itemId * @return array|boolean */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { $decodedValue = json_decode($value, JSON_OBJECT_AS_ARRAY); if (!$this->integrityCheck($decodedValue, $itemtype, $itemId)) { return false; diff --git a/inc/policydeployfile.class.php b/inc/policydeployfile.class.php index 0cea3081..f41df9e4 100644 --- a/inc/policydeployfile.class.php +++ b/inc/policydeployfile.class.php @@ -132,7 +132,7 @@ public function integrityCheck($value, $itemtype, $itemId) { * @param integer $itemId * @return array|bool */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { $decodedValue = json_decode($value, JSON_OBJECT_AS_ARRAY); if (!$this->integrityCheck($decodedValue, $itemtype, $itemId)) { return false; diff --git a/inc/policydropdown.class.php b/inc/policydropdown.class.php index a035eb02..4ecb1c88 100644 --- a/inc/policydropdown.class.php +++ b/inc/policydropdown.class.php @@ -73,7 +73,7 @@ public function integrityCheck($value, $itemtype, $itemId) { * @param integer $itemId * @return array|bool */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { if (!$this->integrityCheck($value, $itemtype, $itemId)) { return false; } diff --git a/inc/policyinteger.class.php b/inc/policyinteger.class.php index b83c198d..6f23f0fe 100644 --- a/inc/policyinteger.class.php +++ b/inc/policyinteger.class.php @@ -96,7 +96,7 @@ public function integrityCheck($value, $itemtype, $itemId) { * @param integer $itemId * @return array|bool */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { if (!$this->integrityCheck($value, $itemtype, $itemId)) { return false; } diff --git a/inc/policyinterface.class.php b/inc/policyinterface.class.php index 0f8dcfec..4dbda2f0 100644 --- a/inc/policyinterface.class.php +++ b/inc/policyinterface.class.php @@ -90,7 +90,7 @@ public function conflictCheck($value, $itemtype, $itemId, PluginFlyvemdmNotifiab * @param integer $itemId the id of an item * @return array */ - public function getMqttMessage($value, $itemtype, $itemId); + public function getBrokerMessage($value, $itemtype, $itemId); /** * Translate type_data field diff --git a/inc/policyremoveapplication.class.php b/inc/policyremoveapplication.class.php index 5b414e0d..1812035e 100644 --- a/inc/policyremoveapplication.class.php +++ b/inc/policyremoveapplication.class.php @@ -93,7 +93,7 @@ public function unicityCheck($value, $itemtype, $itemId, PluginFlyvemdmNotifiabl * @param integer $itemId * @return array|boolean */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { if (!$this->integrityCheck($value, $itemtype, $itemId)) { return false; } diff --git a/inc/policyremovefile.class.php b/inc/policyremovefile.class.php index 936c0251..af48b56c 100644 --- a/inc/policyremovefile.class.php +++ b/inc/policyremovefile.class.php @@ -92,7 +92,7 @@ public function unicityCheck($value, $itemtype, $itemId, PluginFlyvemdmNotifiabl * @param integer $itemId * @return array|boolean */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { if (! $this->integrityCheck($value, $itemtype, $itemId)) { return false; } diff --git a/inc/policystring.class.php b/inc/policystring.class.php index f5080b1e..fb239794 100644 --- a/inc/policystring.class.php +++ b/inc/policystring.class.php @@ -55,7 +55,7 @@ public function __construct(PluginFlyvemdmPolicy $policy) { * @param integer $itemId * @return array|bool */ - public function getMqttMessage($value, $itemtype, $itemId) { + public function getBrokerMessage($value, $itemtype, $itemId) { if (!$this->integrityCheck($value, $itemtype, $itemId)) { return false; } diff --git a/inc/task.class.php b/inc/task.class.php index 44abc990..ba57ccf2 100644 --- a/inc/task.class.php +++ b/inc/task.class.php @@ -394,7 +394,7 @@ public function publishPolicy(PluginFlyvemdmNotifiableInterface $item) { $policy->getFromDB($this->fields[$policyFk]); $policyName = $policy->getField('symbol'); $taskId = $this->getID(); - $policyMessage = $appliedPolicy->getMqttMessage( + $policyMessage = $appliedPolicy->getBrokerMessage( $this->fields['value'], $this->fields['itemtype'], $this->fields['items_id'] @@ -463,7 +463,7 @@ protected function buildMqttMessage($policiesToApply) { $groupToEncode = []; foreach ($policiesToApply as $policyToApply) { $policy = $policyToApply['policyData']; - $policyMessage = $policy->getMqttMessage( + $policyMessage = $policy->getBrokerMessage( $policyToApply['value'], $policyToApply['itemtype'], $policyToApply['items_id'] diff --git a/install/upgrade_to_2_0.php b/install/upgrade_to_2_0.php index 8fa84ce2..9217e43f 100644 --- a/install/upgrade_to_2_0.php +++ b/install/upgrade_to_2_0.php @@ -503,7 +503,7 @@ public function upgrade(Migration $migration) { ]); $policyFactory = new PluginFlyvemdmPolicyFactory(); $appliedPolicy = $policyFactory->createFromDBByID($row['plugin_flyvemdm_policies_id']); - $policyMessage = $appliedPolicy->getMqttMessage( + $policyMessage = $appliedPolicy->getBrokerMessage( $row['value'], $row['itemtype'], $row['items_id'] diff --git a/tests/suite-unit/PluginFlyvemdmPolicyBoolean.php b/tests/suite-unit/PluginFlyvemdmPolicyBoolean.php index 59d42451..c95c1e07 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyBoolean.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyBoolean.php @@ -74,15 +74,15 @@ public function testCreatePolicy() { public function testGetMqttMessage() { list($policy, $policyData) = $this->createNewPolicyInstance(); // Test the mqtt message if the policy - $array = $policy->getMqttMessage('0', null, '0'); + $array = $policy->getBrokerMessage('0', null, '0'); $symbol = $policyData->fields['symbol']; $this->array($array)->hasKey($symbol)->string($array[$symbol])->isEqualTo('false'); - $array = $policy->getMqttMessage('1', null, '0'); + $array = $policy->getBrokerMessage('1', null, '0'); $symbol = $policyData->fields['symbol']; $this->array($array)->hasKey($symbol)->string($array[$symbol])->isEqualTo('true'); - $this->boolean($policy->getMqttMessage('0', null, '1'))->isFalse(); + $this->boolean($policy->getBrokerMessage('0', null, '1'))->isFalse(); } /** diff --git a/tests/suite-unit/PluginFlyvemdmPolicyDeployapplication.php b/tests/suite-unit/PluginFlyvemdmPolicyDeployapplication.php index 67a9b837..18e892f1 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyDeployapplication.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyDeployapplication.php @@ -108,10 +108,10 @@ public function testCreatePolicy($data, $expected) { public function testGetMqttMessage() { list($policy) = $this->createNewPolicyInstance(); - $this->boolean($policy->getMqttMessage(null, null, null))->isFalse(); + $this->boolean($policy->getBrokerMessage(null, null, null))->isFalse(); $item = $this->createDummyPackage(0); $value = '{"remove_on_delete":0}'; - $result = $policy->getMqttMessage($value, $item->getType(), $item->getID()); + $result = $policy->getBrokerMessage($value, $item->getType(), $item->getID()); $this->array($result)->hasKeys(['id', 'versionCode', $this->dataField['symbol']]) ->string($result['id'])->isEqualTo($item->getID()) ->string($result['versionCode'])->isEqualTo("") diff --git a/tests/suite-unit/PluginFlyvemdmPolicyDeployfile.php b/tests/suite-unit/PluginFlyvemdmPolicyDeployfile.php index 45799289..26f608f8 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyDeployfile.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyDeployfile.php @@ -210,10 +210,10 @@ private function createNewPolicyInstance() { public function testGetMqttMessage() { list($policy) = $this->createNewPolicyInstance(); - $this->boolean($policy->getMqttMessage(null, null, null))->isFalse(); + $this->boolean($policy->getBrokerMessage(null, null, null))->isFalse(); $item = $this->createDummyFile(0); $value = '{"destination":"%SDCARD%/filename.ext","remove_on_delete":0}'; - $result = $policy->getMqttMessage($value, $item->getType(), $item->getID()); + $result = $policy->getBrokerMessage($value, $item->getType(), $item->getID()); $this->array($result)->hasKeys(['id', 'version', $this->dataField['symbol']]) ->string($result['id'])->isEqualTo($item->getID()) ->string($result['version'])->isEqualTo("1") diff --git a/tests/suite-unit/PluginFlyvemdmPolicyDropdown.php b/tests/suite-unit/PluginFlyvemdmPolicyDropdown.php index fae85b14..24bd044b 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyDropdown.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyDropdown.php @@ -77,11 +77,11 @@ public function testCreatePolicy() { public function testGetMqttMessage() { list($policy, $policyData) = $this->createNewPolicyInstance(); // Test the mqtt message if the policy - $array = $policy->getMqttMessage('VAL_1', null, '0'); + $array = $policy->getBrokerMessage('VAL_1', null, '0'); $symbol = $policyData->fields['symbol']; $this->array($array)->hasKey($symbol)->string($array[$symbol])->isEqualTo('VAL_1'); - $this->boolean($policy->getMqttMessage(null, null, '1'))->isFalse(); + $this->boolean($policy->getBrokerMessage(null, null, '1'))->isFalse(); } /** diff --git a/tests/suite-unit/PluginFlyvemdmPolicyInteger.php b/tests/suite-unit/PluginFlyvemdmPolicyInteger.php index 8895c56a..72781cc0 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyInteger.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyInteger.php @@ -86,11 +86,11 @@ public function testCreatePolicyWithConstraints() { public function testGetMqttMessage() { list($policy, $policyData) = $this->createNewPolicyInstance(); // Test the mqtt message if the policy - $array = $policy->getMqttMessage('6', null, '0'); + $array = $policy->getBrokerMessage('6', null, '0'); $symbol = $policyData->fields['symbol']; $this->array($array)->hasKey($symbol)->string($array[$symbol])->isEqualTo('6'); - $this->boolean($policy->getMqttMessage(null, null, '1'))->isFalse(); + $this->boolean($policy->getBrokerMessage(null, null, '1'))->isFalse(); } public function providerFilterStatus() { diff --git a/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php b/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php index aae00384..c3c5a9d8 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyRemoveapplication.php @@ -137,9 +137,9 @@ public function testUnicityCheck(array $data, array $expected) { */ public function testGetMqttMessage() { list($policy) = $this->createNewPolicyInstance(); - $this->boolean($policy->getMqttMessage(null, null, null))->isFalse(); + $this->boolean($policy->getBrokerMessage(null, null, null))->isFalse(); - $result = $policy->getMqttMessage($packageName = 'fake.lorem.ipsum.package', '', 0); + $result = $policy->getBrokerMessage($packageName = 'fake.lorem.ipsum.package', '', 0); $this->array($result)->hasKeys([$this->dataField['symbol']]) ->string($result[$this->dataField['symbol']])->isEqualTo($packageName); } diff --git a/tests/suite-unit/PluginFlyvemdmPolicyString.php b/tests/suite-unit/PluginFlyvemdmPolicyString.php index d23fa49e..55650b60 100644 --- a/tests/suite-unit/PluginFlyvemdmPolicyString.php +++ b/tests/suite-unit/PluginFlyvemdmPolicyString.php @@ -70,7 +70,7 @@ public function testCreatePolicy() { public function testGetMqttMessage() { list($policy, $policyData) = $this->createNewPolicyInstance(); // Test the mqtt message if the policy - $array = $policy->getMqttMessage('a little string', null, '0'); + $array = $policy->getBrokerMessage('a little string', null, '0'); $symbol = $policyData->fields['symbol']; $this->array($array)->hasKey($symbol)->string($array[$symbol])->isEqualTo('a little string'); } From 4ab5d9509adf0884e6178c6680b4f9ee20983b9f Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Mon, 24 Sep 2018 21:14:09 +0200 Subject: [PATCH 7/8] feat(agent): give ability to update an existing agent Signed-off-by: Thierry Bugier --- inc/agent.class.php | 55 +++++++++++++- inc/invitation.class.php | 10 +-- install/install.class.php | 2 +- install/upgrade_to_dev.php | 11 ++- tests/suite-integration/ProfileRight.php | 2 +- tests/suite-unit/PluginFlyvemdmAgent.php | 93 ++++++++++++++++++++++++ 6 files changed, 163 insertions(+), 10 deletions(-) diff --git a/inc/agent.class.php b/inc/agent.class.php index 9938523c..96509809 100644 --- a/inc/agent.class.php +++ b/inc/agent.class.php @@ -386,6 +386,20 @@ public function canViewItem() { return $_SESSION['glpiID'] == $computer->getField('users_id'); } + public function canUpdateItem() { + // Check the active profile + $config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']); + if ($_SESSION['glpiactiveprofile']['id'] != $config['guest_profiles_id']) { + return parent::canUpdateItem(); + } + + if (!$this->checkEntity(true)) { + return false; + } + + return $_SESSION['glpiID'] == $this->fields[User::getForeignKeyField()]; + } + /** * Sends a wipe command to the agent */ @@ -469,6 +483,11 @@ public function prepareInputForAdd($input) { } public function prepareInputForUpdate($input) { + $config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']); + if ($_SESSION['glpiactiveprofile']['id'] == $config['guest_profiles_id']) { + return $this->prepareInputForUpdateFromDevice($input); + } + if (isset($input['plugin_flyvemdm_fleets_id'])) { // Update MQTT ACL for the fleet $oldFleet = new PluginFlyvemdmFleet(); @@ -547,6 +566,31 @@ public function prepareInputForUpdate($input) { return $input; } + /** + * Prepare input for update from the agent itseld + * + * @param array $input + * @return array + */ + private function prepareInputForUpdateFromDevice($input) { + //Sanitize input + unset($input[Computer::getForeignKeyField()]); + unset($input[User::getForeignKeyField()]); + unset($input[Entity::getForeignKeyField()]); + unset($input[PluginFlyvemdmFleet::getForeignKeyField()]); + unset($input['name']); + unset($input['wipe']); + unset($input['lock']); + unset($input['enroll_status']); + unset($input['last_report']); + unset($input['last_contact']); + unset($input['is_online']); + unset($input['certificate']); + unset($input['mdm_type']); + + return $input; + } + public function post_addItem() { // Notify the agent about its fleets $this->updateSubscription(); @@ -1294,6 +1338,15 @@ protected function enrollByInvitationToken($input) { } $computerId = $pfAgent->getField(Computer::getForeignKeyField()); + // Check no Flyvemdm agent is linked to this computer + $agent = new self(); + if ($agent->getFromDBByCrit(['computers_id' => $computerId])) { + // Save the agent ID in session to allow the device to find it + // and update it. Give up creation + $_SESSION['plugin_flyvemdm_agents_id'] = $agent->getID(); + return false; + } + if ($computerId === 0) { $event = __("Cannot create the device", 'flyvemdm'); $this->filterMessages($event); @@ -1355,7 +1408,7 @@ protected function enrollByInvitationToken($input) { // Create the agent $defaultFleet = PluginFlyvemdmFleet::getDefaultFleet(); if ($defaultFleet === null) { - $event = __("No default fleet available for the device", 'flyvemdm'); + $event = __('No default fleet available for the device', 'flyvemdm'); $this->filterMessages($event); $this->logInvitationEvent($invitation, $event); return false; diff --git a/inc/invitation.class.php b/inc/invitation.class.php index e3e0b965..cea57302 100644 --- a/inc/invitation.class.php +++ b/inc/invitation.class.php @@ -95,18 +95,18 @@ public function getRights($interface = 'central') { public function prepareInputForAdd($input) { // integrity checks if (!isset($input['_useremails'])) { - Session::addMessageAfterRedirect(__("Email address is invalid", 'flyvemdm')); + Session::addMessageAfterRedirect(__('Email address is not set', 'flyvemdm')); return false; } $input['_useremails'] = filter_var($input['_useremails'], FILTER_VALIDATE_EMAIL); if (!$input['_useremails']) { - Session::addMessageAfterRedirect(__("Email address is invalid", 'flyvemdm')); + Session::addMessageAfterRedirect(__('Email address is invalid', 'flyvemdm')); return false; } // Find guest profile's id - $config = Config::getConfigurationValues("flyvemdm", ['guest_profiles_id']); + $config = Config::getConfigurationValues('flyvemdm', ['guest_profiles_id']); $guestProfileId = $config['guest_profiles_id']; $entityId = $input['entities_id']; @@ -134,7 +134,7 @@ public function prepareInputForAdd($input) { ]); if ($user->isNewItem()) { - Session::addMessageAfterRedirect(__("Cannot create the user", 'flyvemdm'), false, INFO, + Session::addMessageAfterRedirect(__('Cannot create the user', 'flyvemdm'), false, INFO, true); return false; } @@ -142,7 +142,7 @@ public function prepareInputForAdd($input) { } else { // Do not handle deleted users if ($user->isDeleted()) { - Session::addMessageAfterRedirect(__("The user already exists and has been deleted. You must restore or purge him first.", + Session::addMessageAfterRedirect(__('The user already exists and has been deleted. You must restore or purge him first.', 'flyvemdm'), false, INFO, true); return false; } diff --git a/install/install.class.php b/install/install.class.php index a2964f20..5f4e692e 100644 --- a/install/install.class.php +++ b/install/install.class.php @@ -272,7 +272,7 @@ protected function createGuestProfileAccess() { Config::setConfigurationValues('flyvemdm', ['guest_profiles_id' => $profileId]); $profileRight = new ProfileRight(); $profileRight->updateProfileRights($profileId, [ - PluginFlyvemdmAgent::$rightname => READ | CREATE, + PluginFlyvemdmAgent::$rightname => READ | CREATE | UPDATE, PluginFlyvemdmFile::$rightname => READ, PluginFlyvemdmPackage::$rightname => READ, ]); diff --git a/install/upgrade_to_dev.php b/install/upgrade_to_dev.php index f3b334b8..6c2edd6c 100644 --- a/install/upgrade_to_dev.php +++ b/install/upgrade_to_dev.php @@ -42,8 +42,6 @@ function upgrade(Migration $migration) { $migration->setVersion(PLUGIN_FLYVEMDM_VERSION); - $profileRight = new ProfileRight(); - $config = Config::getConfigurationValues('flyvemdm'); if (!isset($config['mqtt_broker_port_backend'])) { // Split port setting for client in one hand and backend in the other hand @@ -51,5 +49,14 @@ function upgrade(Migration $migration) { $config['mqtt_broker_port_backend'] = $config['mqtt_broker_port']; Config::setConfigurationValues('flyvemdm', $config); } + + // Merge new rights into guest profile + $profileId = $config['guest_profiles_id']; + $currentRights = ProfileRight::getProfileRights($profileId); + $newRights = array_merge($currentRights, [ + PluginFlyvemdmAgent::$rightname => CREATE| READ | UPDATE , + ]); + $profileRight = new ProfileRight(); + $profileRight->updateProfileRights($profileId, $newRights); } } diff --git a/tests/suite-integration/ProfileRight.php b/tests/suite-integration/ProfileRight.php index fee9645b..2a024cb8 100644 --- a/tests/suite-integration/ProfileRight.php +++ b/tests/suite-integration/ProfileRight.php @@ -133,7 +133,7 @@ public function testGuestProfileRights() { $profileId = $config['guest_profiles_id']; // Expected rights $rightsSet = [ - \PluginFlyvemdmAgent::$rightname => READ | CREATE, + \PluginFlyvemdmAgent::$rightname => READ | CREATE | UPDATE, \PluginFlyvemdmPackage::$rightname => READ, \PluginFlyvemdmFile::$rightname => READ, ]; diff --git a/tests/suite-unit/PluginFlyvemdmAgent.php b/tests/suite-unit/PluginFlyvemdmAgent.php index 53549371..a02d887c 100644 --- a/tests/suite-unit/PluginFlyvemdmAgent.php +++ b/tests/suite-unit/PluginFlyvemdmAgent.php @@ -348,4 +348,97 @@ public function testGetSpecificValueToDisplay() { $this->string($instance->getSpecificValueToDisplay('mdm_type', 'android'))->contains('Android'); } + + public function testCanUpdateItem() { + // Check a super admin can update the agent + $this->login('glpi', 'glpi'); + $agent = $this->createAgent(); + $output = $agent->canUpdateItem(); + $this->boolean($output)->isTrue(); + + // Check if the account of the agent can update it + $user = new \User(); + $user->getFromDB($agent->fields[\User::getForeignKeyField()]); + $this->loginWithUserToken($user->fields['api_token']); + $output = $agent->canUpdateItem(); + $this->boolean($output)->isTrue(); + + // Check if a not authorized profile can update the item + $this->login('post-only', 'postonly'); + $output = $agent->canUpdateItem(); + $this->boolean($output)->isTrue(); // The check is only on entity restriction + + // Check that canUpdate will prevent update + $output = \PluginFlyvemdmAgent::canUpdate(); + $this->boolean($output)->isFalse(); + } + + /** + * @tags testPrepareInputForUpdate + */ + public function testPrepareInputForUpdate() { + $this->login('glpi', 'glpi'); + $agent = $this->createAgent(); + list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField()); + $this->loginWithUserToken($user->fields['api_token']); + $output = $agent->prepareInputForUpdate([ + \Computer::getForeignKeyField() => '0', + \User::getForeignKeyField() => '0', + \Entity::getForeignKeyField() => '0', + \PluginFlyvemdmFleet::getForeignKeyField() => '0', + 'name' => '', + 'wipe' => '0', + 'lock' => '0', + 'enroll_status' => '', + 'last_report' => '', + 'last_contact' => '', + 'is_online' => '', + 'certificate' => '', + 'mdm_type' => '', + ]); + + $this->array($output)->notHasKeys([ + \Computer::getForeignKeyField(), + \User::getForeignKeyField(), + \Entity::getForeignKeyField(), + \PluginFlyvemdmFleet::getForeignKeyField(), + 'name', + 'wipe', + 'lock', + 'enroll_status', + 'last_report', + 'last_contact', + 'is_online', + 'certificate', + 'mdm_type', + ]); + } + + /** + * @tags testPrepareInputForAdd + */ + public function testPrepareInputForAdd() { + list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField()); + list($user2, $serial2, $guestEmail2, $invitation2) = $this->createUserInvitation(\User::getForeignKeyField()); + $invitationToken = $invitation->fields['invitation_token']; + $invitationToken2 = $invitation2->fields['invitation_token']; + $inventory = self::AgentXmlInventory($serial); + $agent = $this->agentFromInvitation($user, $guestEmail, $serial, $invitationToken, 'android', + '', $inventory, [], true); + $this->array($_SESSION)->notHasKey('plugin_flyvemdm_agents_id'); + + // Inconsistency : creating an invitation for tests should not generate a serial + // the serial should be crated in the caller instead + list($user, $serial, $guestEmail, $invitation) = $this->createUserInvitation(\User::getForeignKeyField()); + $newAgent = $this->agentFromInvitation($user2, $guestEmail2, $serial, $invitationToken2, 'android', + '', $inventory, [], false); + // Check the agent is not created + $this->boolean($newAgent->isNewItem())->isTrue(); + // Do not test the message in session: it may be filtered when + // debug mode for enrollment is disabled + // Instead, let's check if the session contains the ID of the agent to update + $this->array($_SESSION)->hasKey('plugin_flyvemdm_agents_id'); + $this->integer((int) $_SESSION['plugin_flyvemdm_agents_id']) + ->isEqualTo($agent->getID()); + } } From 63847dc094aeee12661a04ee333c5175f02c3e71 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 17 Oct 2018 21:26:38 +0200 Subject: [PATCH 8/8] refactor(travis): factorize patch arguments Also prevents patch to detect already applied patch and revert them Signed-off-by: Thierry Bugier --- tests/before_script.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/before_script.sh b/tests/before_script.sh index 4a835f1d..b3d4ea74 100755 --- a/tests/before_script.sh +++ b/tests/before_script.sh @@ -34,18 +34,20 @@ mkdir plugins/fusioninventory && git clone --depth=35 $FI_SOURCE -b $FI_BRANCH p IFS=/ read -a repo <<< $TRAVIS_REPO_SLUG mv ../${repo[1]} plugins/flyvemdm +# patch settings +PATCH_ARGS="-p1 -N --batch" + # patch Fusion Inventory when needed cd plugins/fusioninventory -if [[ $FI_BRANCH == "master" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fusioninventory/fi-raise-max-version.patch; fi -if [[ $FI_BRANCH == "master" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fusioninventory/compat-glpi-9-3-2.diff; fi -if [[ $FI_BRANCH == "glpi9.3" ]] ; then patch -p1 --batch < ../flyvemdm/tests/patches/fusioninventory/compat-glpi-9-3-2.diff; fi +if [[ $FI_BRANCH == "master" ]] ; then patch $PATCH_ARGS < ../flyvemdm/tests/patches/fusioninventory/fi-raise-max-version.patch; fi +if [[ $FI_BRANCH == "master" ]] ; then patch $PATCH_ARGS < ../flyvemdm/tests/patches/fusioninventory/compat-glpi-9-3-2.diff; fi +if [[ $FI_BRANCH == "glpi9.3" ]] ; then patch $PATCH_ARGS < ../flyvemdm/tests/patches/fusioninventory/compat-glpi-9-3-2.diff; fi cd ../.. # patch GLPI when needed -# if [[ $GLPI_BRANCH == "9.2.1" ]] ; then patch -p1 --batch < plugins/flyvemdm/tests/patches/glpi/10f8dabfc5e20bb5a4e7d4ba4b93706871156a8a.diff; fi # prepare plugin to test cd plugins/flyvemdm -if [[ $GLPI_BRANCH == "master" ]] ; then patch -p1 --batch < tests/patches/allow-test-on-master-branch.patch; fi +if [[ $GLPI_BRANCH == "master" ]] ; then patch $PATCH_ARGS < tests/patches/allow-test-on-master-branch.patch; fi composer install --no-interaction