diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 0f40e9f..7c5fcbd 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -20,7 +20,7 @@ jobs: name: "Generate CI matrix" uses: "glpi-project/plugin-ci-workflows/.github/workflows/generate-ci-matrix.yml@v1" with: - glpi-version: "11.0.x" + glpi-version: "12.0.x" ci: name: "GLPI ${{ matrix.glpi-version }} - php:${{ matrix.php-version }} - ${{ matrix.db-image }}" needs: "generate-ci-matrix" diff --git a/.gitignore b/.gitignore index 044ece9..03a6388 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vendor/ .gh_token *.min.* var/ +.phpunit.result.cache diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f5214b..d09e439 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added + +- GLPI 12 compatibility + ### Fixed - CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix diff --git a/ajax/agent.php b/ajax/agent.php index 9375ec4..0dcad73 100644 --- a/ajax/agent.php +++ b/ajax/agent.php @@ -29,6 +29,7 @@ */ use Glpi\Exception\Http\NotFoundHttpException; +use Glpi\Inventory\Conf; use function Safe\json_encode; @@ -38,8 +39,8 @@ Html::header_nocache(); Session::checkLoginUser(); -Session::checkRight("inventory", READ); -Session::checkRight("database_inventory", PluginDatabaseinventoryProfile::RUN_DATABSE_INVENTORY); +Session::checkRight(Conf::class, READ); +Session::checkRight(PluginDatabaseinventoryDatabaseParam::class, PluginDatabaseinventoryProfile::RUN_DATABSE_INVENTORY); if (isset($_POST['action']) && isset($_POST['id'])) { $agent = new Agent(); diff --git a/composer.json b/composer.json index f3811f7..b1d84aa 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,11 @@ }, "sort-packages": true }, + "autoload": { + "psr-4": { + "GlpiPlugin\\DatabaseInventory\\Tests\\": "tests" + } + }, "autoload-dev": { "psr-4": { "Glpi\\Tools\\": "../../tools/src/" diff --git a/front/computergroup.form.php b/front/computergroup.form.php index 1dad2c8..d1e880d 100644 --- a/front/computergroup.form.php +++ b/front/computergroup.form.php @@ -30,15 +30,11 @@ use Glpi\Event; -Session::checkRight('config', READ); +Session::checkRight(Config::$rightname, READ); -if (!isset($_GET['id'])) { - $_GET['id'] = ''; -} +$_GET['id'] ??= ''; -if (!isset($_GET['withtemplate'])) { - $_GET['withtemplate'] = ''; -} +$_GET['withtemplate'] ??= ''; $computergroup = new PluginDatabaseinventoryComputerGroup(); $computergroupstatic = new PluginDatabaseinventoryComputerGroupStatic(); diff --git a/front/computergroup.php b/front/computergroup.php index a2b516c..fc7cc6f 100644 --- a/front/computergroup.php +++ b/front/computergroup.php @@ -28,7 +28,7 @@ * ------------------------------------------------------------------------- */ -Session::checkRight('config', UPDATE); +Session::checkRight(Config::$rightname, UPDATE); Html::header( PluginDatabaseinventoryComputerGroup::getTypeName(Session::getPluralNumber()), diff --git a/front/credential.form.php b/front/credential.form.php index c495186..5e36f42 100644 --- a/front/credential.form.php +++ b/front/credential.form.php @@ -30,15 +30,11 @@ use Glpi\Event; -Session::checkRight('config', READ); +Session::checkRight(Config::$rightname, READ); -if (!isset($_GET['id'])) { - $_GET['id'] = ''; -} +$_GET['id'] ??= ''; -if (!isset($_GET['withtemplate'])) { - $_GET['withtemplate'] = ''; -} +$_GET['withtemplate'] ??= ''; $credential = new PluginDatabaseinventoryCredential(); diff --git a/front/credential.php b/front/credential.php index d5e976c..d5cc3d3 100644 --- a/front/credential.php +++ b/front/credential.php @@ -28,7 +28,7 @@ * ------------------------------------------------------------------------- */ -Session::checkRight('config', UPDATE); +Session::checkRight(Config::$rightname, UPDATE); Html::header( PluginDatabaseinventoryCredential::getTypeName(Session::getPluralNumber()), diff --git a/front/databaseparam.form.php b/front/databaseparam.form.php index ccbd75e..44c2b82 100644 --- a/front/databaseparam.form.php +++ b/front/databaseparam.form.php @@ -30,15 +30,11 @@ use Glpi\Event; -Session::checkRight('config', READ); +Session::checkRight(Config::$rightname, READ); -if (!isset($_GET['id'])) { - $_GET['id'] = ''; -} +$_GET['id'] ??= ''; -if (!isset($_GET['withtemplate'])) { - $_GET['withtemplate'] = ''; -} +$_GET['withtemplate'] ??= ''; $databaseparam = new PluginDatabaseinventoryDatabaseParam(); $databaseparam_credential = new PluginDatabaseinventoryDatabaseParam_Credential(); diff --git a/front/databaseparam.php b/front/databaseparam.php index bd7966e..f4d456d 100644 --- a/front/databaseparam.php +++ b/front/databaseparam.php @@ -28,7 +28,7 @@ * ------------------------------------------------------------------------- */ -Session::checkRight('config', UPDATE); +Session::checkRight(Config::$rightname, UPDATE); Html::header( PluginDatabaseinventoryDatabaseParam::getTypeName(Session::getPluralNumber()), diff --git a/hook.php b/hook.php index 0e420b8..fbf1637 100644 --- a/hook.php +++ b/hook.php @@ -89,14 +89,14 @@ function plugin_databaseinventory_uninstall() function plugin_databaseinventory_MassiveActions($type) { // Must be super-admin - if (!Session::haveRight('database_inventory', UPDATE)) { + if (!Session::haveRight(PluginDatabaseinventoryDatabaseParam::class, UPDATE)) { return []; } switch ($type) { case 'Computer': case 'Agent': - $class = PluginDatabaseinventoryInventoryAction::getType(); + $class = PluginDatabaseinventoryInventoryAction::class; $key = PluginDatabaseinventoryInventoryAction::MA_PARTIAL; $label = __s('Run partial databases inventory', 'databaseinventory'); @@ -113,7 +113,7 @@ function postItemForm(CommonDBTM $item) function cleanComputerFromStaticGroup(CommonDBTM $item) { - if ($item::getType() === Computer::getType()) { + if ($item::class === Computer::class) { $c_static = new PluginDatabaseinventoryComputerGroupStatic(); $c_static->deleteByCriteria(['computers_id' => $item->fields['id']]); } @@ -121,7 +121,7 @@ function cleanComputerFromStaticGroup(CommonDBTM $item) function cleanAgentFromContactLog(CommonDBTM $item) { - if ($item::getType() === Agent::getType()) { + if ($item::class === Agent::class) { $contactlog = new PluginDatabaseinventoryContactLog(); $contactlog->deleteByCriteria(['agents_id' => $item->fields['id']]); } diff --git a/inc/computergroup.class.php b/inc/computergroup.class.php index f655a0d..da29f22 100644 --- a/inc/computergroup.class.php +++ b/inc/computergroup.class.php @@ -60,9 +60,9 @@ class PluginDatabaseinventoryComputerGroup extends CommonDBTM { - public $dohistory = true; + public bool $dohistory = true; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function getTypeName($nb = 0) { diff --git a/inc/computergroupdynamic.class.php b/inc/computergroupdynamic.class.php index 88b08b3..6935c31 100644 --- a/inc/computergroupdynamic.class.php +++ b/inc/computergroupdynamic.class.php @@ -39,7 +39,7 @@ class PluginDatabaseinventoryComputerGroupDynamic extends CommonDBTM { - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function getTypeName($nb = 0) { @@ -192,9 +192,7 @@ public function isDynamicSearchMatchComputer(Computer $computer) 'value' => $computer->fields['id'], ]; - if (!isset($_SESSION['glpiname'])) { - $_SESSION['glpiname'] = 'databaseinventory_plugin'; - } + $_SESSION['glpiname'] ??= 'databaseinventory_plugin'; $search_params = Search::manageParams('Computer', $search); $data = Search::prepareDatasForSearch('Computer', $search_params); @@ -250,7 +248,7 @@ private static function showForItem(PluginDatabaseinventoryComputerGroup $comput //hack because submit button is not a submit... See https://github.com/glpi-project/glpi/pull/20731 ob_start(); - Search::showGenericSearch(Computer::getType(), $p); + Search::showGenericSearch(Computer::class, $p); $generic_search = ob_get_clean(); echo preg_replace('/type="button" name="save"/', 'type="submit" name="save"', $generic_search); diff --git a/inc/computergroupstatic.class.php b/inc/computergroupstatic.class.php index f3c953b..15af522 100644 --- a/inc/computergroupstatic.class.php +++ b/inc/computergroupstatic.class.php @@ -61,21 +61,21 @@ class PluginDatabaseinventoryComputerGroupStatic extends CommonDBRelation { // From CommonDBRelation - public static $itemtype_1 = 'PluginDatabaseinventoryComputerGroup'; + public static ?string $itemtype_1 = 'PluginDatabaseinventoryComputerGroup'; - public static $items_id_1 = 'plugin_databaseinventory_computergroups_id'; + public static ?string $items_id_1 = 'plugin_databaseinventory_computergroups_id'; - public static $itemtype_2 = 'Computer'; + public static ?string $itemtype_2 = 'Computer'; - public static $items_id_2 = 'computers_id'; + public static ?string $items_id_2 = 'computers_id'; - public static $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS; + public static int $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS; - public static $logs_for_item_2 = false; + public static bool $logs_for_item_2 = false; - public $auto_message_on_action = false; + public bool $auto_message_on_action = false; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function getTypeName($nb = 0) { diff --git a/inc/contactlog.class.php b/inc/contactlog.class.php index aafeca7..661cdce 100644 --- a/inc/contactlog.class.php +++ b/inc/contactlog.class.php @@ -60,9 +60,9 @@ class PluginDatabaseinventoryContactLog extends CommonDBTM { - public $dohistory = true; + public bool $dohistory = true; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function canCreate(): bool { @@ -155,7 +155,7 @@ private static function showForDatabaseParams(PluginDatabaseinventoryDatabasePar TemplateRenderer::getInstance()->display( '@databaseinventory/contactlog.html.twig', [ - 'itemtype' => PluginDatabaseinventoryDatabaseParam::getType(), + 'itemtype' => PluginDatabaseinventoryDatabaseParam::class, 'contactlogs' => $listofctlog, 'canread' => $databaseparams->can($ID, READ), ], @@ -205,7 +205,7 @@ private static function showForAgent(Agent $agent) TemplateRenderer::getInstance()->display( '@databaseinventory/contactlog.html.twig', [ - 'itemtype' => Agent::getType(), + 'itemtype' => Agent::class, 'contactlogs' => $listofctlog, 'canread' => $agent->can($ID, READ), ], diff --git a/inc/credential.class.php b/inc/credential.class.php index c6c852e..f3ccef0 100644 --- a/inc/credential.class.php +++ b/inc/credential.class.php @@ -60,9 +60,9 @@ class PluginDatabaseinventoryCredential extends CommonDBTM { - public $dohistory = true; + public bool $dohistory = true; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function canCreate(): bool { diff --git a/inc/credentialtype.class.php b/inc/credentialtype.class.php index 337eb85..d98d1fc 100644 --- a/inc/credentialtype.class.php +++ b/inc/credentialtype.class.php @@ -32,7 +32,7 @@ class PluginDatabaseinventoryCredentialType extends CommonDropdown { - public static $rightname = 'dropdown'; + public static string $rightname = 'dropdown'; private const MYSQL = 1; diff --git a/inc/databaseparam.class.php b/inc/databaseparam.class.php index 5da76f2..7e52f67 100644 --- a/inc/databaseparam.class.php +++ b/inc/databaseparam.class.php @@ -60,9 +60,9 @@ class PluginDatabaseinventoryDatabaseParam extends CommonDBTM { - public $dohistory = true; + public bool $dohistory = true; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function getTypeName($nb = 0) { diff --git a/inc/databaseparam_computergroup.class.php b/inc/databaseparam_computergroup.class.php index 8e4b781..b946c62 100644 --- a/inc/databaseparam_computergroup.class.php +++ b/inc/databaseparam_computergroup.class.php @@ -61,21 +61,21 @@ class PluginDatabaseinventoryDatabaseParam_ComputerGroup extends CommonDBRelation { // From CommonDBRelation - public static $itemtype_1 = 'PluginDatabaseinventoryDatabaseParam'; + public static ?string $itemtype_1 = 'PluginDatabaseinventoryDatabaseParam'; - public static $items_id_1 = 'plugin_databaseinventory_databaseparams_id'; + public static ?string $items_id_1 = 'plugin_databaseinventory_databaseparams_id'; - public static $itemtype_2 = 'PluginDatabaseinventoryComputerGroup'; + public static ?string $itemtype_2 = 'PluginDatabaseinventoryComputerGroup'; - public static $items_id_2 = 'plugin_databaseinventory_computergroups_id'; + public static ?string $items_id_2 = 'plugin_databaseinventory_computergroups_id'; - public static $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS; + public static int $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS; - public static $logs_for_item_2 = false; + public static bool $logs_for_item_2 = false; - public $auto_message_on_action = false; + public bool $auto_message_on_action = false; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function canCreate(): bool { diff --git a/inc/databaseparam_credential.class.php b/inc/databaseparam_credential.class.php index 9de9615..791f1a4 100644 --- a/inc/databaseparam_credential.class.php +++ b/inc/databaseparam_credential.class.php @@ -61,21 +61,21 @@ class PluginDatabaseinventoryDatabaseParam_Credential extends CommonDBRelation { // From CommonDBRelation - public static $itemtype_1 = 'PluginDatabaseinventoryDatabaseParam'; + public static ?string $itemtype_1 = 'PluginDatabaseinventoryDatabaseParam'; - public static $items_id_1 = 'plugin_databaseinventory_databaseparams_id'; + public static ?string $items_id_1 = 'plugin_databaseinventory_databaseparams_id'; - public static $itemtype_2 = 'PluginDatabaseinventoryCredential'; + public static ?string $itemtype_2 = 'PluginDatabaseinventoryCredential'; - public static $items_id_2 = 'plugin_databaseinventory_credentials_id'; + public static ?string $items_id_2 = 'plugin_databaseinventory_credentials_id'; - public static $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS; + public static int $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS; - public static $logs_for_item_2 = false; + public static bool $logs_for_item_2 = false; - public $auto_message_on_action = false; + public bool $auto_message_on_action = false; - public static $rightname = 'database_inventory'; + public static string $rightname = 'database_inventory'; public static function canCreate(): bool { diff --git a/inc/inventoryaction.class.php b/inc/inventoryaction.class.php index f75fb39..89cd8d3 100644 --- a/inc/inventoryaction.class.php +++ b/inc/inventoryaction.class.php @@ -36,7 +36,7 @@ class PluginDatabaseinventoryInventoryAction extends CommonDBTM private const ENDPOINT_PARTIAL = 'now?'; - public static $rightname = 'database_inventory_run_inventory'; + public static string $rightname = 'database_inventory_run_inventory'; public static function showMassiveActionsSubForm(MassiveAction $ma) { @@ -57,49 +57,49 @@ public static function processMassiveActionsForOneItemtype(MassiveAction $ma, Co return; } - if (!Session::haveRight('database_inventory', PluginDatabaseinventoryProfile::RUN_DATABSE_INVENTORY)) { + if (!Session::haveRight(PluginDatabaseinventoryDatabaseParam::class, PluginDatabaseinventoryProfile::RUN_DATABSE_INVENTORY)) { foreach ($ids as $id) { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_NORIGHT); } return; } - switch ($item->getType()) { - case Computer::getType(): + switch ($item::class) { + case Computer::class: foreach ($ids as $id) { $computer = new Computer(); if (!$computer->getFromDB($id) || !$computer->can($id, READ)) { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_NORIGHT); continue; } if ($agent = self::findAgent($computer)) { if (PluginDatabaseinventoryInventoryAction::runPartialInventory($agent, true)) { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_OK); } else { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION)); } } else { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_KO); $ma->addMessage(__s('Agent not found for computer', 'databaseinventory') . "" . $computer->getFriendlyName() . ''); } } break; - case Agent::getType(): + case Agent::class: foreach ($ids as $id) { $agent = new Agent(); if (!$agent->getFromDB($id) || !$agent->can($id, READ)) { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_NORIGHT); continue; } if (PluginDatabaseinventoryInventoryAction::runPartialInventory($agent, true)) { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_OK); } else { - $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO); + $ma->itemDone($item::class, $id, MassiveAction::ACTION_KO); $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION)); } } @@ -125,17 +125,17 @@ public static function runPartialInventory(Agent $agent, $fromMA = false) $response = $agent->requestAgent($endpoint); if ($fromMA) { return true; - } else { - // not authorized - return self::handleAgentResponse($response, $endpoint); } + + // not authorized + return self::handleAgentResponse($response, $endpoint); } catch (Exception $exception) { if ($fromMA) { return false; - } else { - // not authorized - return ['answer' => $exception->getMessage()]; } + + // not authorized + return ['answer' => $exception->getMessage()]; } } @@ -151,20 +151,20 @@ private static function findAgent(Computer $item) { $agent = new Agent(); $has_agent = $agent->getFromDBByCrit([ - 'itemtype' => $item->getType(), + 'itemtype' => $item::class, 'items_id' => $item->fields['id'], ]); // if no agent has been found, check if there is a linked item, and find its agent - if (!$has_agent && $item->getType() == 'Computer') { + if (!$has_agent && $item::class == 'Computer') { $citem = new Asset_PeripheralAsset(); $has_relation = $citem->getFromDBByCrit([ - 'itemtype_peripheral' => $item->getType(), + 'itemtype_peripheral' => $item::class, 'items_id_peripheral' => $item->fields['id'], ]); if ($has_relation) { $has_agent = $agent->getFromDBByCrit([ - 'itemtype' => Computer::getType(), + 'itemtype' => Computer::class, 'items_id' => $citem->fields['computers_id'], ]); } @@ -172,9 +172,9 @@ private static function findAgent(Computer $item) if ($has_agent) { return $agent; - } else { - return false; } + + return false; } public static function postItemForm($item) @@ -183,14 +183,14 @@ public static function postItemForm($item) return; } - if (!Session::haveRight("database_inventory", PluginDatabaseinventoryProfile::RUN_DATABSE_INVENTORY)) { + if (!Session::haveRight(PluginDatabaseinventoryDatabaseParam::class, PluginDatabaseinventoryProfile::RUN_DATABSE_INVENTORY)) { return; } /** @var array $CFG_GLPI */ global $CFG_GLPI; - if ($item::getType() == Computer::getType() && $agent = self::findAgent($item)) { + if ($item::getType() == Computer::class && $agent = self::findAgent($item)) { $out = '