From 59d8ac829b3cb951bcc72fef34b876d3b7158f36 Mon Sep 17 00:00:00 2001 From: David Durieux Date: Sun, 3 Apr 2022 23:25:56 +0200 Subject: [PATCH] Add code for manage remotework computers and not import external devices in this case. --- front/inventoryruleremotework.form.php | 61 ++ front/inventoryruleremotework.php | 63 ++ inc/inventorycomputerlib.class.php | 621 +++++++++--------- inc/inventoryruleremotework.class.php | 320 +++++++++ ...nventoryruleremoteworkcollection.class.php | 111 ++++ inc/menu.class.php | 41 +- setup.php | 3 + tests/Integration/RuleRemoteworkTest.php | 205 ++++++ 8 files changed, 1112 insertions(+), 313 deletions(-) create mode 100644 front/inventoryruleremotework.form.php create mode 100644 front/inventoryruleremotework.php create mode 100644 inc/inventoryruleremotework.class.php create mode 100644 inc/inventoryruleremoteworkcollection.class.php create mode 100644 tests/Integration/RuleRemoteworkTest.php diff --git a/front/inventoryruleremotework.form.php b/front/inventoryruleremotework.form.php new file mode 100644 index 0000000000..d83d04148e --- /dev/null +++ b/front/inventoryruleremotework.form.php @@ -0,0 +1,61 @@ +. + * + * ------------------------------------------------------------------------ + * + * This file is used to manage the rule remote work form. + * + * ------------------------------------------------------------------------ + * + * @package FusionInventory + * @author David Durieux + * @copyright Copyright (c) 2010-2022 FusionInventory team + * @license AGPL License 3.0 or (at your option) any later version + * http://www.gnu.org/licenses/agpl-3.0-standalone.html + * @link http://www.fusioninventory.org/ + * @link https://github.com/fusioninventory/fusioninventory-for-glpi + * + */ + +include ("../../../inc/includes.php"); + +Html::header(__('FusionInventory', 'fusioninventory'), + $_SERVER["PHP_SELF"], + "admin", + "pluginfusioninventorymenu", + "inventoryruleremotework"); +Session::checkLoginUser(); + +PluginFusioninventoryMenu::displayMenu("mini"); + +$rulecollection = new PluginFusioninventoryInventoryRuleRemoteworkCollection(); + +include (GLPI_ROOT . "/front/rule.common.form.php"); + diff --git a/front/inventoryruleremotework.php b/front/inventoryruleremotework.php new file mode 100644 index 0000000000..7d1c19f89a --- /dev/null +++ b/front/inventoryruleremotework.php @@ -0,0 +1,63 @@ +. + * + * ------------------------------------------------------------------------ + * + * This file is used to manage the rule remote work search list. + * + * ------------------------------------------------------------------------ + * + * @package FusionInventory + * @author David Durieux + * @copyright Copyright (c) 2010-2022 FusionInventory team + * @license AGPL License 3.0 or (at your option) any later version + * http://www.gnu.org/licenses/agpl-3.0-standalone.html + * @link http://www.fusioninventory.org/ + * @link https://github.com/fusioninventory/fusioninventory-for-glpi + * + */ + +include ("../../../inc/includes.php"); + +Session::checkLoginUser(); +Html::header(__('FusionInventory', 'fusioninventory'), + $_SERVER["PHP_SELF"], + "admin", + "pluginfusioninventorymenu", + "inventoryruleremotework"); + +PluginFusioninventoryMenu::displayMenu("mini"); + +RuleCollection::titleBackup(); + +$rulecollection = new PluginFusioninventoryInventoryRuleRemoteworkCollection(); + +include (GLPI_ROOT . "/front/rule.common.php"); + diff --git a/inc/inventorycomputerlib.class.php b/inc/inventorycomputerlib.class.php index 698723b922..fa45b3fa59 100644 --- a/inc/inventorycomputerlib.class.php +++ b/inc/inventorycomputerlib.class.php @@ -146,6 +146,27 @@ function updateComputer($a_computerinventory, $computers_id, $no_history, $setdy $computer->getFromDB($computers_id); $a_lockable = PluginFusioninventoryLock::getLockFields('glpi_computers', $computers_id); + $importExternalDevices = true; + + // Pass rule to know if it's a computer in remote work + $inputRuleRemotework = []; + + foreach ($a_computerinventory['networkport'] as $network) { + foreach ($network['ipaddress'] as $ip) { + if ($ip != '127.0.0.1' && $ip != '::1') { + $inputRuleRemotework['ip'][] = $ip; + } + } + } + $ruleRemotework = new PluginFusioninventoryInventoryRuleRemoteworkCollection(); + + // * Reload rules (required for unit tests) + $ruleRemotework->getCollectionPart(); + + $dataRemotework = $ruleRemotework->processAllRules($inputRuleRemotework, []); + if (isset($dataRemotework['_ignore_external_devices'])) { + $importExternalDevices = false; + } // Manage operating system if (isset($a_computerinventory['fusioninventorycomputer']['items_operatingsystems_id'])) { @@ -1532,348 +1553,354 @@ function updateComputer($a_computerinventory, $computers_id, $no_history, $setdy $entities_id = $_SESSION["plugin_fusioninventory_entity"]; // * Monitors - $rule = new PluginFusioninventoryInventoryRuleImportCollection(); - $a_monitors = []; - foreach ($a_computerinventory['monitor'] as $key => $arrays) { - $input = []; - $input['itemtype'] = "Monitor"; - $input['name'] = $arrays['name']; - $input['serial'] = isset($arrays['serial']) - ? $arrays['serial'] - : ""; - $data = $rule->processAllRules($input, [], ['class'=>$this, 'return' => true]); - - if (isset($data['found_equipment'])) { - if ($data['found_equipment'][0] == 0) { - // add monitor - $arrays['entities_id'] = $entities_id; - $arrays['otherserial'] = PluginFusioninventoryToolbox::setInventoryNumber( - 'Monitor', '', $entities_id); - $a_monitors[] = $monitor->add($arrays); - } else { - $a_monitors[] = $data['found_equipment'][0]; - // Check monitor information to update if not good (in case of modification in agent for example) - $monitor->getFromDB($data['found_equipment'][0]); - $input = []; - $input['id'] = $data['found_equipment'][0]; - foreach (['manufacturers_id', 'name', 'monitormodels_id'] as $key) { - if ($monitor->fields[$key] !== $arrays[$key] && !empty($arrays[$key])) { - $input[$key] = $arrays[$key]; + if ($importExternalDevices) { + $rule = new PluginFusioninventoryInventoryRuleImportCollection(); + $a_monitors = []; + foreach ($a_computerinventory['monitor'] as $key => $arrays) { + $input = []; + $input['itemtype'] = "Monitor"; + $input['name'] = $arrays['name']; + $input['serial'] = isset($arrays['serial']) + ? $arrays['serial'] + : ""; + $data = $rule->processAllRules($input, [], ['class'=>$this, 'return' => true]); + + if (isset($data['found_equipment'])) { + if ($data['found_equipment'][0] == 0) { + // add monitor + $arrays['entities_id'] = $entities_id; + $arrays['otherserial'] = PluginFusioninventoryToolbox::setInventoryNumber( + 'Monitor', '', $entities_id); + $a_monitors[] = $monitor->add($arrays); + } else { + $a_monitors[] = $data['found_equipment'][0]; + // Check monitor information to update if not good (in case of modification in agent for example) + $monitor->getFromDB($data['found_equipment'][0]); + $input = []; + $input['id'] = $data['found_equipment'][0]; + foreach (['manufacturers_id', 'name', 'monitormodels_id'] as $key) { + if ($monitor->fields[$key] !== $arrays[$key] && !empty($arrays[$key])) { + $input[$key] = $arrays[$key]; + } + } + if (count($input) > 1) { + $monitor->update($input); } } - if (count($input) > 1) { - $monitor->update($input); - } - } - if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { - $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); - $inputrulelog = []; - $inputrulelog['date'] = date('Y-m-d H:i:s'); - $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; - if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { - $inputrulelog['plugin_fusioninventory_agents_id'] = - $_SESSION['plugin_fusioninventory_agents_id']; + if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { + $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); + $inputrulelog = []; + $inputrulelog['date'] = date('Y-m-d H:i:s'); + $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; + if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { + $inputrulelog['plugin_fusioninventory_agents_id'] = + $_SESSION['plugin_fusioninventory_agents_id']; + } + $inputrulelog['items_id'] = end($a_monitors); + $inputrulelog['itemtype'] = "Monitor"; + $inputrulelog['method'] = 'inventory'; + $pfRulematchedlog->add($inputrulelog, [], false); + $pfRulematchedlog->cleanOlddata(end($a_monitors), "Monitor"); + unset($_SESSION['plugin_fusioninventory_rules_id']); } - $inputrulelog['items_id'] = end($a_monitors); - $inputrulelog['itemtype'] = "Monitor"; - $inputrulelog['method'] = 'inventory'; - $pfRulematchedlog->add($inputrulelog, [], false); - $pfRulematchedlog->cleanOlddata(end($a_monitors), "Monitor"); - unset($_SESSION['plugin_fusioninventory_rules_id']); } } - } - $db_monitors = []; - $iterator = $DB->request([ - 'SELECT' => [ - 'glpi_monitors.id', - 'glpi_computers_items.id AS link_id' - ], - 'FROM' => 'glpi_computers_items', - 'LEFT JOIN' => [ - 'glpi_monitors' => [ - 'FKEY' => [ - 'glpi_monitors' => 'id', - 'glpi_computers_items' => 'items_id' + $db_monitors = []; + $iterator = $DB->request([ + 'SELECT' => [ + 'glpi_monitors.id', + 'glpi_computers_items.id AS link_id' + ], + 'FROM' => 'glpi_computers_items', + 'LEFT JOIN' => [ + 'glpi_monitors' => [ + 'FKEY' => [ + 'glpi_monitors' => 'id', + 'glpi_computers_items' => 'items_id' + ] ] + ], + 'WHERE' => [ + 'itemtype' => 'Monitor', + 'computers_id' => $computers_id, + 'entities_id' => $entities_id, + 'glpi_computers_items.is_dynamic' => 1, + 'glpi_monitors.is_global' => 0 ] - ], - 'WHERE' => [ - 'itemtype' => 'Monitor', - 'computers_id' => $computers_id, - 'entities_id' => $entities_id, - 'glpi_computers_items.is_dynamic' => 1, - 'glpi_monitors.is_global' => 0 - ] - ]); - while ($data = $iterator->next()) { - $idtmp = $data['link_id']; - unset($data['link_id']); - $db_monitors[$idtmp] = $data['id']; - } - if (count($db_monitors) == 0) { - foreach ($a_monitors as $monitors_id) { - $input = [ - 'computers_id' => $computers_id, - 'itemtype' => 'Monitor', - 'items_id' => $monitors_id, - 'is_dynamic' => 1, - '_no_history' => $no_history - ]; - $this->computerItemAdd($input, $no_history); + ]); + while ($data = $iterator->next()) { + $idtmp = $data['link_id']; + unset($data['link_id']); + $db_monitors[$idtmp] = $data['id']; } - } else { - // Check all fields from source: - foreach ($a_monitors as $key => $monitors_id) { - foreach ($db_monitors as $keydb => $monits_id) { - if ($monitors_id == $monits_id) { - unset($a_monitors[$key]); - unset($db_monitors[$keydb]); - break; + if (count($db_monitors) == 0) { + foreach ($a_monitors as $monitors_id) { + $input = [ + 'computers_id' => $computers_id, + 'itemtype' => 'Monitor', + 'items_id' => $monitors_id, + 'is_dynamic' => 1, + '_no_history' => $no_history + ]; + $this->computerItemAdd($input, $no_history); + } + } else { + // Check all fields from source: + foreach ($a_monitors as $key => $monitors_id) { + foreach ($db_monitors as $keydb => $monits_id) { + if ($monitors_id == $monits_id) { + unset($a_monitors[$key]); + unset($db_monitors[$keydb]); + break; + } } } - } - if (count($a_monitors) || count($db_monitors)) { - if (count($db_monitors) != 0) { - // Delete monitors links in DB - foreach ($db_monitors as $idtmp => $monits_id) { - $computer_Item->delete(['id'=>$idtmp], 1); + if (count($a_monitors) || count($db_monitors)) { + if (count($db_monitors) != 0) { + // Delete monitors links in DB + foreach ($db_monitors as $idtmp => $monits_id) { + $computer_Item->delete(['id'=>$idtmp], 1); + } } - } - if (count($a_monitors) != 0) { - foreach ($a_monitors as $key => $monitors_id) { - $input = []; - $input['computers_id'] = $computers_id; - $input['itemtype'] = 'Monitor'; - $input['items_id'] = $monitors_id; - $input['is_dynamic'] = 1; - $input['_no_history'] = $no_history; - $this->computerItemAdd($input, $no_history); + if (count($a_monitors) != 0) { + foreach ($a_monitors as $key => $monitors_id) { + $input = []; + $input['computers_id'] = $computers_id; + $input['itemtype'] = 'Monitor'; + $input['items_id'] = $monitors_id; + $input['is_dynamic'] = 1; + $input['_no_history'] = $no_history; + $this->computerItemAdd($input, $no_history); + } } } } } // * Printers - $rule = new PluginFusioninventoryInventoryRuleImportCollection(); - $a_printers = []; - foreach ($a_computerinventory['printer'] as $key => $arrays) { - $input = []; - $input['itemtype'] = "Printer"; - $input['name'] = $arrays['name']; - $input['serial'] = isset($arrays['serial']) - ? $arrays['serial'] - : ""; - $data = $rule->processAllRules($input, [], ['class'=>$this, 'return' => true]); - if (isset($data['found_equipment'])) { - if ($data['found_equipment'][0] == 0) { - // add printer - $arrays['entities_id'] = $entities_id; - $arrays['otherserial'] = PluginFusioninventoryToolbox::setInventoryNumber( - 'Printer', '', $entities_id); - $a_printers[] = $printer->add($arrays); - } else { - $a_printers[] = $data['found_equipment'][0]; - } - if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { - $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); - $inputrulelog = []; - $inputrulelog['date'] = date('Y-m-d H:i:s'); - $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; - if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { - $inputrulelog['plugin_fusioninventory_agents_id'] = - $_SESSION['plugin_fusioninventory_agents_id']; + if ($importExternalDevices) { + $rule = new PluginFusioninventoryInventoryRuleImportCollection(); + $a_printers = []; + foreach ($a_computerinventory['printer'] as $key => $arrays) { + $input = []; + $input['itemtype'] = "Printer"; + $input['name'] = $arrays['name']; + $input['serial'] = isset($arrays['serial']) + ? $arrays['serial'] + : ""; + $data = $rule->processAllRules($input, [], ['class'=>$this, 'return' => true]); + if (isset($data['found_equipment'])) { + if ($data['found_equipment'][0] == 0) { + // add printer + $arrays['entities_id'] = $entities_id; + $arrays['otherserial'] = PluginFusioninventoryToolbox::setInventoryNumber( + 'Printer', '', $entities_id); + $a_printers[] = $printer->add($arrays); + } else { + $a_printers[] = $data['found_equipment'][0]; + } + if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { + $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); + $inputrulelog = []; + $inputrulelog['date'] = date('Y-m-d H:i:s'); + $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; + if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { + $inputrulelog['plugin_fusioninventory_agents_id'] = + $_SESSION['plugin_fusioninventory_agents_id']; + } + $inputrulelog['items_id'] = end($a_printers); + $inputrulelog['itemtype'] = "Printer"; + $inputrulelog['method'] = 'inventory'; + $pfRulematchedlog->add($inputrulelog, [], false); + $pfRulematchedlog->cleanOlddata(end($a_printers), "Printer"); + unset($_SESSION['plugin_fusioninventory_rules_id']); } - $inputrulelog['items_id'] = end($a_printers); - $inputrulelog['itemtype'] = "Printer"; - $inputrulelog['method'] = 'inventory'; - $pfRulematchedlog->add($inputrulelog, [], false); - $pfRulematchedlog->cleanOlddata(end($a_printers), "Printer"); - unset($_SESSION['plugin_fusioninventory_rules_id']); - } + } } - } - $db_printers = []; - $iterator = $DB->request([ - 'SELECT' => [ - 'glpi_printers.id', - 'glpi_computers_items.id AS link_id' - ], - 'FROM' => 'glpi_computers_items', - 'LEFT JOIN' => [ - 'glpi_printers' => [ - 'FKEY' => [ - 'glpi_printers' => 'id', - 'glpi_computers_items' => 'items_id' + $db_printers = []; + $iterator = $DB->request([ + 'SELECT' => [ + 'glpi_printers.id', + 'glpi_computers_items.id AS link_id' + ], + 'FROM' => 'glpi_computers_items', + 'LEFT JOIN' => [ + 'glpi_printers' => [ + 'FKEY' => [ + 'glpi_printers' => 'id', + 'glpi_computers_items' => 'items_id' + ] ] + ], + 'WHERE' => [ + 'itemtype' => 'Printer', + 'computers_id' => $computers_id, + 'entities_id' => $entities_id, + 'glpi_computers_items.is_dynamic' => 1, + 'glpi_printers.is_global' => 0 ] - ], - 'WHERE' => [ - 'itemtype' => 'Printer', - 'computers_id' => $computers_id, - 'entities_id' => $entities_id, - 'glpi_computers_items.is_dynamic' => 1, - 'glpi_printers.is_global' => 0 - ] - ]); + ]); - while ($data = $iterator->next()) { - $idtmp = $data['link_id']; - unset($data['link_id']); - $db_printers[$idtmp] = $data['id']; - } - if (count($db_printers) == 0) { - foreach ($a_printers as $printers_id) { - $input['entities_id'] = $entities_id; - $input['computers_id'] = $computers_id; - $input['itemtype'] = 'Printer'; - $input['items_id'] = $printers_id; - $input['is_dynamic'] = 1; - $input['_no_history'] = $no_history; - $this->computerItemAdd($input, $no_history); - } - } else { - // Check all fields from source: - foreach ($a_printers as $key => $printers_id) { - foreach ($db_printers as $keydb => $prints_id) { - if ($printers_id == $prints_id) { - unset($a_printers[$key]); - unset($db_printers[$keydb]); - break; - } + while ($data = $iterator->next()) { + $idtmp = $data['link_id']; + unset($data['link_id']); + $db_printers[$idtmp] = $data['id']; + } + if (count($db_printers) == 0) { + foreach ($a_printers as $printers_id) { + $input['entities_id'] = $entities_id; + $input['computers_id'] = $computers_id; + $input['itemtype'] = 'Printer'; + $input['items_id'] = $printers_id; + $input['is_dynamic'] = 1; + $input['_no_history'] = $no_history; + $this->computerItemAdd($input, $no_history); } - } - if (count($a_printers) || count($db_printers)) { - if (count($db_printers) != 0) { - // Delete printers links in DB - foreach ($db_printers as $idtmp => $data) { - $computer_Item->delete(['id'=>$idtmp], 1); + } else { + // Check all fields from source: + foreach ($a_printers as $key => $printers_id) { + foreach ($db_printers as $keydb => $prints_id) { + if ($printers_id == $prints_id) { + unset($a_printers[$key]); + unset($db_printers[$keydb]); + break; + } } } - if (count($a_printers) != 0) { - foreach ($a_printers as $printers_id) { - $input['entities_id'] = $entities_id; - $input['computers_id'] = $computers_id; - $input['itemtype'] = 'Printer'; - $input['items_id'] = $printers_id; - $input['is_dynamic'] = 1; - $input['_no_history'] = $no_history; - $this->computerItemAdd($input, $no_history); + if (count($a_printers) || count($db_printers)) { + if (count($db_printers) != 0) { + // Delete printers links in DB + foreach ($db_printers as $idtmp => $data) { + $computer_Item->delete(['id'=>$idtmp], 1); + } + } + if (count($a_printers) != 0) { + foreach ($a_printers as $printers_id) { + $input['entities_id'] = $entities_id; + $input['computers_id'] = $computers_id; + $input['itemtype'] = 'Printer'; + $input['items_id'] = $printers_id; + $input['is_dynamic'] = 1; + $input['_no_history'] = $no_history; + $this->computerItemAdd($input, $no_history); + } } } } } // * Peripheral - $rule = new PluginFusioninventoryInventoryRuleImportCollection(); - $a_peripherals = []; - foreach ($a_computerinventory['peripheral'] as $key => $arrays) { - $input = []; - $input['itemtype'] = "Peripheral"; - $input['name'] = $arrays['name']; - $input['serial'] = isset($arrays['serial']) - ? $arrays['serial'] - : ""; - $data = $rule->processAllRules($input, [], ['class'=>$this, 'return' => true]); - if (isset($data['found_equipment'])) { - if ($data['found_equipment'][0] == 0) { - // add peripheral - $arrays['entities_id'] = $entities_id; - $arrays['otherserial'] = PluginFusioninventoryToolbox::setInventoryNumber( - 'Peripheral', '', $entities_id); - $a_peripherals[] = $peripheral->add($arrays); - } else { - $a_peripherals[] = $data['found_equipment'][0]; - } - if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { - $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); - $inputrulelog = []; - $inputrulelog['date'] = date('Y-m-d H:i:s'); - $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; - if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { - $inputrulelog['plugin_fusioninventory_agents_id'] = - $_SESSION['plugin_fusioninventory_agents_id']; + if ($importExternalDevices) { + $rule = new PluginFusioninventoryInventoryRuleImportCollection(); + $a_peripherals = []; + foreach ($a_computerinventory['peripheral'] as $key => $arrays) { + $input = []; + $input['itemtype'] = "Peripheral"; + $input['name'] = $arrays['name']; + $input['serial'] = isset($arrays['serial']) + ? $arrays['serial'] + : ""; + $data = $rule->processAllRules($input, [], ['class'=>$this, 'return' => true]); + if (isset($data['found_equipment'])) { + if ($data['found_equipment'][0] == 0) { + // add peripheral + $arrays['entities_id'] = $entities_id; + $arrays['otherserial'] = PluginFusioninventoryToolbox::setInventoryNumber( + 'Peripheral', '', $entities_id); + $a_peripherals[] = $peripheral->add($arrays); + } else { + $a_peripherals[] = $data['found_equipment'][0]; + } + if (isset($_SESSION['plugin_fusioninventory_rules_id'])) { + $pfRulematchedlog = new PluginFusioninventoryRulematchedlog(); + $inputrulelog = []; + $inputrulelog['date'] = date('Y-m-d H:i:s'); + $inputrulelog['rules_id'] = $_SESSION['plugin_fusioninventory_rules_id']; + if (isset($_SESSION['plugin_fusioninventory_agents_id'])) { + $inputrulelog['plugin_fusioninventory_agents_id'] = + $_SESSION['plugin_fusioninventory_agents_id']; + } + $inputrulelog['items_id'] = end($a_peripherals); + $inputrulelog['itemtype'] = "Peripheral"; + $inputrulelog['method'] = 'inventory'; + $pfRulematchedlog->add($inputrulelog, [], false); + $pfRulematchedlog->cleanOlddata(end($a_peripherals), "Peripheral"); + unset($_SESSION['plugin_fusioninventory_rules_id']); } - $inputrulelog['items_id'] = end($a_peripherals); - $inputrulelog['itemtype'] = "Peripheral"; - $inputrulelog['method'] = 'inventory'; - $pfRulematchedlog->add($inputrulelog, [], false); - $pfRulematchedlog->cleanOlddata(end($a_peripherals), "Peripheral"); - unset($_SESSION['plugin_fusioninventory_rules_id']); } } - } - $db_peripherals = []; - $iterator = $DB->request([ - 'SELECT' => [ - 'glpi_peripherals.id', - 'glpi_computers_items.id AS link_id' - ], - 'FROM' => 'glpi_computers_items', - 'LEFT JOIN' => [ - 'glpi_peripherals' => [ - 'FKEY' => [ - 'glpi_peripherals' => 'id', - 'glpi_computers_items' => 'items_id' + $db_peripherals = []; + $iterator = $DB->request([ + 'SELECT' => [ + 'glpi_peripherals.id', + 'glpi_computers_items.id AS link_id' + ], + 'FROM' => 'glpi_computers_items', + 'LEFT JOIN' => [ + 'glpi_peripherals' => [ + 'FKEY' => [ + 'glpi_peripherals' => 'id', + 'glpi_computers_items' => 'items_id' + ] ] + ], + 'WHERE' => [ + 'itemtype' => 'Peripheral', + 'computers_id' => $computers_id, + 'entities_id' => $entities_id, + 'glpi_computers_items.is_dynamic' => 1, + 'glpi_peripherals.is_global' => 0 ] - ], - 'WHERE' => [ - 'itemtype' => 'Peripheral', - 'computers_id' => $computers_id, - 'entities_id' => $entities_id, - 'glpi_computers_items.is_dynamic' => 1, - 'glpi_peripherals.is_global' => 0 - ] - ]); - - while ($data = $iterator->next()) { - $idtmp = $data['link_id']; - unset($data['link_id']); - $db_peripherals[$idtmp] = $data['id']; - } + ]); - if (count($db_peripherals) == 0) { - foreach ($a_peripherals as $peripherals_id) { - $input = []; - $input['computers_id'] = $computers_id; - $input['itemtype'] = 'Peripheral'; - $input['items_id'] = $peripherals_id; - $input['is_dynamic'] = 1; - $input['_no_history'] = $no_history; - $this->computerItemAdd($input, $no_history); + while ($data = $iterator->next()) { + $idtmp = $data['link_id']; + unset($data['link_id']); + $db_peripherals[$idtmp] = $data['id']; } - } else { - // Check all fields from source: - foreach ($a_peripherals as $key => $peripherals_id) { - foreach ($db_peripherals as $keydb => $periphs_id) { - if ($peripherals_id == $periphs_id) { - unset($a_peripherals[$key]); - unset($db_peripherals[$keydb]); - break; + + if (count($db_peripherals) == 0) { + foreach ($a_peripherals as $peripherals_id) { + $input = []; + $input['computers_id'] = $computers_id; + $input['itemtype'] = 'Peripheral'; + $input['items_id'] = $peripherals_id; + $input['is_dynamic'] = 1; + $input['_no_history'] = $no_history; + $this->computerItemAdd($input, $no_history); + } + } else { + // Check all fields from source: + foreach ($a_peripherals as $key => $peripherals_id) { + foreach ($db_peripherals as $keydb => $periphs_id) { + if ($peripherals_id == $periphs_id) { + unset($a_peripherals[$key]); + unset($db_peripherals[$keydb]); + break; + } } } - } - if (count($a_peripherals) || count($db_peripherals)) { - if (count($db_peripherals) != 0) { - // Delete peripherals links in DB - foreach ($db_peripherals as $idtmp => $data) { - $computer_Item->delete(['id'=>$idtmp], 1); + if (count($a_peripherals) || count($db_peripherals)) { + if (count($db_peripherals) != 0) { + // Delete peripherals links in DB + foreach ($db_peripherals as $idtmp => $data) { + $computer_Item->delete(['id'=>$idtmp], 1); + } } - } - if (count($a_peripherals) != 0) { - foreach ($a_peripherals as $peripherals_id) { - $input = []; - $input['computers_id'] = $computers_id; - $input['itemtype'] = 'Peripheral'; - $input['items_id'] = $peripherals_id; - $input['is_dynamic'] = 1; - $input['_no_history'] = $no_history; - $this->computerItemAdd($input, $no_history); + if (count($a_peripherals) != 0) { + foreach ($a_peripherals as $peripherals_id) { + $input = []; + $input['computers_id'] = $computers_id; + $input['itemtype'] = 'Peripheral'; + $input['items_id'] = $peripherals_id; + $input['is_dynamic'] = 1; + $input['_no_history'] = $no_history; + $this->computerItemAdd($input, $no_history); + } } } } diff --git a/inc/inventoryruleremotework.class.php b/inc/inventoryruleremotework.class.php new file mode 100644 index 0000000000..1d141217c9 --- /dev/null +++ b/inc/inventoryruleremotework.class.php @@ -0,0 +1,320 @@ +. + * + * ------------------------------------------------------------------------ + * + * This file is used to manage remote work rules for computer. + * + * ------------------------------------------------------------------------ + * + * @package FusionInventory + * @author David Durieux + * @copyright Copyright (c) 2010-2022 FusionInventory team + * @license AGPL License 3.0 or (at your option) any later version + * http://www.gnu.org/licenses/agpl-3.0-standalone.html + * @link http://www.fusioninventory.org/ + * @link https://github.com/fusioninventory/fusioninventory-for-glpi + * + */ + +if (!defined('GLPI_ROOT')) { + die("Sorry. You can't access directly to this file"); +} + +/** + * Manage remote work rules for computer. + */ +class PluginFusioninventoryInventoryRuleRemotework extends Rule { + + /** + * Set these rules can be sorted + * + * @var boolean + */ + public $can_sort=true; + + /** + * Set these rules don't have specific parameters + * + * @var boolean + */ + public $specific_parameters = false; + + /** + * The right name for this class + * + * @var string + */ + static $rightname = 'rule_import'; + + const PATTERN_CIDR = 333; + const PATTERN_NOT_CIDR = 334; + + + /** + * Get name of this type by language of the user connected + * + * @return string name of this type + */ + function getTitle() { + return __('Remote work rules', 'fusioninventory'); + } + + + /** + * Make some changes before process review result + * + * @param array $output + * @return array + */ + function preProcessPreviewResults($output) { + return $output; + } + + + /** + * Define maximum number of actions possible in a rule + * + * @return integer + */ + function maxActionsCount() { + return 1; + } + + + /** + * Code execution of actions of the rule + * + * @param array $output + * @param array $params + * @return array + */ + function executeActions($output, $params, array $input = []) { + + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-remotework", + "execute actions, data:\n". print_r($output, true). "\n" . print_r($params, true) + ); + + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-remotework", + "execute actions: ". count($this->actions) ."\n" + ); + + if (count($this->actions)) { + foreach ($this->actions as $action) { + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-remotework", + "- action: ". $action->fields["action_type"] ." for: ". $action->fields["field"] ."\n" + ); + + switch ($action->fields["action_type"]) { + case "assign" : + PluginFusioninventoryToolbox::logIfExtradebug( + "pluginFusioninventory-rules-remotework", + "- value ".$action->fields["value"]."\n" + ); + $output[$action->fields["field"]] = $action->fields["value"]; + break; + + } + } + } + return $output; + } + + + /** + * Get the criteria available for the rule + * + * @return array + */ + function getCriterias() { + + $criterias = [ + 'ip' => [ + 'name' => __('IP address', 'fusioninventory'), + 'field' => 'name', + 'allow_condition' => [ + self::PATTERN_CIDR, + self::PATTERN_NOT_CIDR + ] + ] + ]; + return $criterias; + } + + + /** + * Get the actions available for the rule + * + * @return array + */ + function getActions() { + + $actions = []; + $actions['_ignore_external_devices']['name'] = + __('Ignore external devices import', 'fusioninventory'); + $actions['_ignore_external_devices']['type'] = 'yesonly'; + + return $actions; + } + + + /** + * Add more criteria + * + * @param string $criterion + * @return array + */ + static function addMoreCriteria($criterion = '') { + if ($criterion == 'ip') { + return [ + self::PATTERN_CIDR => __('is CIDR', 'fusioninventory'), + self::PATTERN_NOT_CIDR => __('is not CIDR', 'fusioninventory') + ]; + } + return []; + } + + + /** + * Check the criteria + * + * @param object $criteria + * @param array $input + * @return boolean + */ + function checkCriteria(&$criteria, &$input) { + + $res = parent::checkCriteria($criteria, $input); + + if (in_array($criteria->fields["condition"], [self::PATTERN_CIDR])) { + $pattern = $criteria->fields['pattern']; + $value = $this->getCriteriaValue($criteria->fields["criteria"], + $criteria->fields["condition"], + $input[$criteria->fields["criteria"]]); + + list ($subnet, $bits) = explode('/', $pattern); + $subnet = ip2long($subnet); + $mask = -1 << (32 - $bits); + $subnet &= $mask; // nb: in case the supplied subnet wasn't correctly aligned + + if (is_array($value)) { + foreach ($value as $ip) { + if (isset($ip) && $ip != '') { + $ip = ip2long($ip); + if (($ip & $mask) == $subnet) { + $res = true; + break 1; + } + } + } + } else { + if (isset($value) && $value != '') { + $ip = ip2long($value); + if (($ip & $mask) == $subnet) { + $res = true; + } + } + } + } else if (in_array($criteria->fields["condition"], [self::PATTERN_NOT_CIDR])) { + $pattern = $criteria->fields['pattern']; + $value = $this->getCriteriaValue($criteria->fields["criteria"], + $criteria->fields["condition"], + $input[$criteria->fields["criteria"]]); + + list ($subnet, $bits) = explode('/', $pattern); + $subnet = ip2long($subnet); + $mask = -1 << (32 - $bits); + $subnet &= $mask; // nb: in case the supplied subnet wasn't correctly aligned + + if (is_array($value)) { + $resarray = true; + foreach ($value as $ip) { + if (isset($ip) && $ip != '') { + $ip = ip2long($ip); + if (($ip & $mask) == $subnet) { + $resarray = false; + } + } + } + $res = $resarray; + } else { + if (isset($value) && $value != '') { + $ip = ip2long($value); + if (($ip & $mask) != $subnet) { + $res = true; + } + } + } + } + + return $res; + } + + + /** + * Process the rule + * + * @param array &$input the input data used to check criterias + * @param array &$output the initial ouput array used to be manipulate by actions + * @param array &$params parameters for all internal functions + * @param array &options array options: + * - only_criteria : only react on specific criteria + * + * @return array the output updated by actions. + * If rule matched add field _rule_process to return value + */ + function process(&$input, &$output, &$params, &$options = []) { + + if ($this->validateCriterias($options)) { + $this->regex_results = []; + $this->criterias_results = []; + $input = $this->prepareInputDataForProcess($input, $params); + + if ($this->checkCriterias($input)) { + unset($output["_no_rule_matches"]); + $refoutput = $output; + $output = $this->executeActions($output, $params); + if (!isset($output['pass_rule'])) { + $this->updateOnlyCriteria($options, $refoutput, $output); + //Hook + $hook_params["sub_type"] = $this->getType(); + $hook_params["ruleid"] = $this->fields["id"]; + $hook_params["input"] = $input; + $hook_params["output"] = $output; + Plugin::doHook("rule_matched", $hook_params); + $output["_rule_process"] = true; + } + } + } + } +} diff --git a/inc/inventoryruleremoteworkcollection.class.php b/inc/inventoryruleremoteworkcollection.class.php new file mode 100644 index 0000000000..aaf08087cf --- /dev/null +++ b/inc/inventoryruleremoteworkcollection.class.php @@ -0,0 +1,111 @@ +. + * + * ------------------------------------------------------------------------ + * + * This file is used to manage the remote work rules collection. + * + * ------------------------------------------------------------------------ + * + * @package FusionInventory + * @author David Durieux + * @copyright Copyright (c) 2010-2022 FusionInventory team + * @license AGPL License 3.0 or (at your option) any later version + * http://www.gnu.org/licenses/agpl-3.0-standalone.html + * @link http://www.fusioninventory.org/ + * @link https://github.com/fusioninventory/fusioninventory-for-glpi + * + */ + +if (!defined('GLPI_ROOT')) { + die("Sorry. You can't access directly to this file"); +} + +/** + * Manage the remote work rules collection. + */ +class PluginFusioninventoryInventoryRuleRemoteworkCollection extends RuleCollection { + + + /** + * Set stop play rules when have the first rule of list match + * + * @var boolean + */ + public $stop_on_first_match=true; + + /** + * Define the name of menu option + * + * @var string + */ + public $menu_option='test'; + + /** + * The right name for this class + * + * @var string + */ + static $rightname = 'plugin_fusioninventory_ruleimport'; + + + /** + * Get name of this type by language of the user connected + * + * @return string name of this type + */ + function getTitle() { + return __('Remote work rules', 'fusioninventory'); + } + + + /** + * Prepare input data for process the rule + * + * @param array $input + * @param array $params + * @return array + */ + function prepareInputDataForProcess($input, $params) { + return $input; + } + + /** + * Print a title above list of rules + * + * @return void + **/ + function title() { + echo ""; + echo __('These rules can ignore import external devices of computer when computer is in remotework.', 'fusioninventory'); + echo ""; + echo "

"; + } +} diff --git a/inc/menu.class.php b/inc/menu.class.php index a0f0836c24..6d0f1d46ab 100644 --- a/inc/menu.class.php +++ b/inc/menu.class.php @@ -121,6 +121,7 @@ static function getAdditionalMenuOptions() { 'unmanaged' => 'PluginFusioninventoryUnmanaged', 'inventoryruleimport' => 'PluginFusioninventoryInventoryRuleImport', 'inventoryruleentity' => 'PluginFusioninventoryInventoryRuleEntity', + 'inventoryruleremotework' => 'PluginFusioninventoryInventoryRuleRemotework', 'inventoryrulelocation' => 'PluginFusioninventoryInventoryRuleLocation', 'collectrule' => 'PluginFusioninventoryCollectRule', 'inventorycomputerblacklist' => 'PluginFusioninventoryInventoryComputerBlacklist', @@ -326,43 +327,51 @@ static function displayMenu($type = "big") { $a_menu = []; if (Session::haveRight('plugin_fusioninventory_ruleimport', READ)) { - $a_menu[1]['name'] = __('Equipment import and link rules', 'fusioninventory'); + $a_menu[1]['name'] = __('Remotework', 'fusioninventory'); $a_menu[1]['pic'] = $fi_path."/pics/menu_rules.png"; $a_menu[1]['link'] = Toolbox::getItemTypeSearchURL( - 'PluginFusioninventoryInventoryRuleImport' + 'PluginFusioninventoryInventoryRuleRemotework' ); } - if (Session::haveRight('plugin_fusioninventory_ignoredimportdevice', READ)) { - $a_menu[2]['name'] = __('Asset skipped during import', 'fusioninventory'); + if (Session::haveRight('plugin_fusioninventory_ruleimport', READ)) { + $a_menu[2]['name'] = __('Equipment import and link rules', 'fusioninventory'); $a_menu[2]['pic'] = $fi_path."/pics/menu_rules.png"; $a_menu[2]['link'] = Toolbox::getItemTypeSearchURL( + 'PluginFusioninventoryInventoryRuleImport' + ); + } + + if (Session::haveRight('plugin_fusioninventory_ignoredimportdevice', READ)) { + $a_menu[3]['name'] = __('Asset skipped during import', 'fusioninventory'); + $a_menu[3]['pic'] = $fi_path."/pics/menu_rules.png"; + $a_menu[3]['link'] = Toolbox::getItemTypeSearchURL( 'PluginFusioninventoryIgnoredimportdevice' ); } if (Session::haveRight('plugin_fusioninventory_ruleentity', READ)) { - $a_menu[3]['name'] = __('Computer entity rules', 'fusioninventory'); - $a_menu[3]['pic'] = $fi_path."/pics/menu_rules.png"; - $a_menu[3]['link'] = $fi_path."/front/inventoryruleentity.php"; + $a_menu[4]['name'] = __('Computer entity rules', 'fusioninventory'); + $a_menu[4]['pic'] = $fi_path."/pics/menu_rules.png"; + $a_menu[4]['link'] = $fi_path."/front/inventoryruleentity.php"; } if (Session::haveRight('plugin_fusioninventory_rulelocation', READ)) { - $a_menu[4]['name'] = __('Location rules', 'fusioninventory'); - $a_menu[4]['pic'] = $fi_path."/pics/menu_rules.png"; - $a_menu[4]['link'] = $fi_path."/front/inventoryrulelocation.php"; + $a_menu[5]['name'] = __('Location rules', 'fusioninventory'); + $a_menu[5]['pic'] = $fi_path."/pics/menu_rules.png"; + $a_menu[5]['link'] = $fi_path."/front/inventoryrulelocation.php"; } if (Session::haveRight("plugin_fusioninventory_rulecollect", READ)) { - $a_menu[5]['name'] = __('Computer information rules', 'fusioninventory'); - $a_menu[5]['pic'] = $fi_path."/pics/menu_rules.png"; - $a_menu[5]['link'] = $fi_path."/front/collectrule.php"; + $a_menu[6]['name'] = __('Computer information rules', 'fusioninventory'); + $a_menu[6]['pic'] = $fi_path."/pics/menu_rules.png"; + $a_menu[6]['link'] = $fi_path."/front/collectrule.php"; } if (Session::haveRight('plugin_fusioninventory_blacklist', READ)) { - $a_menu[6]['name'] = _n('Blacklist', 'Blacklists', 1); - $a_menu[6]['pic'] = $fi_path."/pics/menu_blacklist.png"; - $a_menu[6]['link'] = $fi_path."/front/inventorycomputerblacklist.php"; + $a_menu[7]['name'] = _n('Blacklist', 'Blacklists', 1); + $a_menu[7]['pic'] = $fi_path."/pics/menu_blacklist.png"; + $a_menu[7]['link'] = $fi_path."/front/inventorycomputerblacklist.php"; } if (!empty($a_menu)) { diff --git a/setup.php b/setup.php index 3bfcca97c2..7226476b8a 100644 --- a/setup.php +++ b/setup.php @@ -180,6 +180,9 @@ function plugin_init_fusioninventory() { $Plugin->registerClass('PluginFusioninventoryInventoryRuleEntity'); $Plugin->registerClass('PluginFusioninventoryInventoryRuleEntityCollection', ['rulecollections_types'=>true]); + $Plugin->registerClass('PluginFusioninventoryInventoryRuleRemotework'); + $Plugin->registerClass('PluginFusioninventoryInventoryRuleRemoteworkCollection', + ['rulecollections_types'=>true]); $Plugin->registerClass('PluginFusioninventoryRulematchedlog', ['addtabon' => ['Computer', 'Monitor', diff --git a/tests/Integration/RuleRemoteworkTest.php b/tests/Integration/RuleRemoteworkTest.php new file mode 100644 index 0000000000..073c0bb5c0 --- /dev/null +++ b/tests/Integration/RuleRemoteworkTest.php @@ -0,0 +1,205 @@ +. + + ------------------------------------------------------------------------ + + @package FusionInventory + @author David Durieux + @co-author + @copyright Copyright (C) 2010-2022 FusionInventory team + @license AGPL License 3.0 or (at your option) any later version + http://www.gnu.org/licenses/agpl-3.0-standalone.html + @link http://www.fusioninventory.org/ + @link http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/ + @since 2013 + + ------------------------------------------------------------------------ + */ + +use PHPUnit\Framework\TestCase; + +class RuleRemoteworkTest extends TestCase { + + + public static function setUpBeforeClass(): void { + + // Delete all remoteworkrules + $rule = new Rule(); + $items = $rule->find(['sub_type' => "PluginFusioninventoryInventoryRemoteworkImport"]); + foreach ($items as $item) { + $rule->delete(['id' => $item['id']], true); + } + } + + public static function tearDownAfterClass(): void { + // Delete all remoteworkrules + $rule = new Rule(); + $items = $rule->find(['sub_type' => "PluginFusioninventoryInventoryRemoteworkImport"]); + foreach ($items as $item) { + $rule->delete(['id' => $item['id']], true); + } + } + + function setUp(): void { + + } + + + /** + * @test + */ + function addRemoteworkRule() { + + $rule = new Rule(); + // Add a rule test check model + $input = [ + 'is_active' => 1, + 'name' => 'remote work rangeIP', + 'match' => 'AND', + 'sub_type' => 'PluginFusioninventoryInventoryRuleRemotework', + 'ranking' => 198, + ]; + $rule_id = $rule->add($input); + $this->assertNotFalse($rule_id); + + // Add criteria + $rulecriteria = new RuleCriteria(); + $input = [ + 'rules_id' => $rule_id, + 'criteria' => 'ip', + 'pattern' => '192.168.0.1/24', + 'condition' => 333 + ]; + $ret = $rulecriteria->add($input); + $this->assertNotFalse($ret); + + // Add action + $ruleaction = new RuleAction(); + $input = [ + 'rules_id' => $rule_id, + 'action_type' => 'assign', + 'field' => '_ignore_external_devices', + 'value' => '1' + ]; + $ret = $ruleaction->add($input); + $this->assertNotFalse($ret); + } + + + /** + * @test + */ + public function computerImport() { + $computerData = [ + 'Computer' => [ + 'name' => 'pc010', + 'serial' => 'ggheb7hy6' + ], + 'fusioninventorycomputer' => [ + 'last_fusioninventory_update' => date('Y-m-d H:i:s'), + 'serialized_inventory' => 'something' + ], + 'soundcard' => [], + 'graphiccard' => [], + 'controller' => [], + 'processor' => [], + 'computerdisk' => [], + 'memory' => [], + 'monitor' => [ + [ + 'name' => 'DELL E1911', + 'manufacturers_id' => 2, + 'serial' => 'W6VPJ18475H', + 'is_dynamic' => 1 + ] + ], + 'printer' => [], + 'peripheral' => [], + 'networkport' => [ + 'em0-cc:f9:54:a1:03:45' => [ + 'name' => 'em0', + 'netmask' => '255.255.255.0', + 'subnet' => '192.168.0.0', + 'mac' => 'cc:f9:54:a1:03:45', + 'instantiation_type' => 'NetworkPortEthernet', + 'virtualdev' => 0, + 'ssid' => '', + 'gateway' => '', + 'dhcpserver' => '', + 'logical_number' => 1, + 'ipaddress' => ['192.168.0.198'] + ] + ], + 'software' => [], + 'harddrive' => [], + 'virtualmachine' => [], + 'antivirus' => [], + 'storage' => [], + 'licenseinfo' => [], + 'networkcard' => [], + 'drive' => [], + 'batteries' => [], + 'remote_mgmt' => [], + 'bios' => [], + 'itemtype' => 'Computer' + ]; + + $_SESSION["plugin_fusioninventory_entity"] = 0; + $_SESSION["glpiname"] = 'Plugin_FusionInventory'; + + $pfiComputerLib = new PluginFusioninventoryInventoryComputerLib(); + $computer = new Computer(); + $monitor = new Monitor(); + + $a_computerinventory = $computerData; + $a_computer = $a_computerinventory['Computer']; + $a_computer["entities_id"] = 0; + $computers_id = $computer->add($a_computer); + + $pfiComputerLib->updateComputer( + $a_computerinventory, + $computers_id, + false, + 1 + ); + + // Check if no monitor added + $this->assertEquals(0, countElementsInTable('glpi_monitors', ['serial' => 'W6VPJ18475H']), 'Monitor must not be added'); + + $computerData['networkport']['em0-cc:f9:54:a1:03:45']['ipaddress'] = ['10.0.43.55']; + $a_computerinventory = $computerData; + $pfiComputerLib->updateComputer( + $a_computerinventory, + $computers_id, + false, + 1 + ); + // Check if the monitor is right added + $this->assertEquals(1, countElementsInTable('glpi_monitors', ['serial' => 'W6VPJ18475H']), 'Monitor must be present'); + + } +}