connessione con hardware in assets

This commit is contained in:
test
2026-08-07 14:27:33 +02:00
parent 7c6e244155
commit f575d8cb05
33 changed files with 3489 additions and 1608 deletions
+122 -58
View File
@@ -8,10 +8,9 @@ declare(strict_types=1);
* -------------------------------------------------------------------------
*/
use Glpi\Asset\AssetDefinitionManager;
use GlpiPlugin\Urbackup\Capacity\UrBackupCapacity;
use GlpiPlugin\Urbackup\Config;
use GlpiPlugin\Urbackup\Profile;
use GlpiPlugin\Urbackup\Server;
if (!defined('GLPI_ROOT')) {
die(__('Sorry. You cannot access this file directly.', 'urbackup'));
@@ -38,12 +37,15 @@ function plugin_urbackup_install_process(): bool
plugin_urbackup_install_update_servers_table($migration);
plugin_urbackup_install_update_serverassets_table($migration);
plugin_urbackup_install_encrypt_api_passwords();
plugin_urbackup_install_add_enable_computer_config();
// Drop tables that are no longer used (replaced by Profile::registerRights() and Capacity system)
plugin_urbackup_install_drop_legacy_tables($migration);
$migration->executeMigration();
Config::ensureDefaultConfiguration();
Profile::installRights();
return true;
@@ -131,40 +133,6 @@ function plugin_urbackup_install_update_configs_table(Migration $migration): voi
$migration->addKey($table, 'name');
}
/**
* Update assettypes table.
*
* @param Migration $migration Migration instance
*
* @return void
*/
function plugin_urbackup_install_update_assettypes_table(Migration $migration): void
{
global $DB;
$table = 'glpi_plugin_urbackup_assettypes';
if (!$DB->tableExists($table)) {
return;
}
$migration->addField($table, 'itemtype', 'string', [
'value' => '',
'after' => 'id',
]);
$migration->addField($table, 'is_active', 'bool', [
'value' => 0,
'after' => 'itemtype',
]);
$migration->addField($table, 'date_creation', 'timestamp');
$migration->addField($table, 'date_mod', 'timestamp');
$migration->addKey($table, 'itemtype');
$migration->addKey($table, 'is_active');
}
/**
* Update servers table.
*
@@ -261,6 +229,15 @@ function plugin_urbackup_install_update_servers_table(Migration $migration): voi
'after' => 'last_api_check',
]);
$migration->addField($table, 'host_itemtype', 'string', [
'after' => 'comment',
]);
$migration->addField($table, 'host_items_id', 'integer', [
'value' => 0,
'after' => 'host_itemtype',
]);
$migration->addField($table, 'date_creation', 'timestamp');
$migration->addField($table, 'date_mod', 'timestamp');
@@ -270,6 +247,7 @@ function plugin_urbackup_install_update_servers_table(Migration $migration): voi
$migration->addKey($table, 'users_id');
$migration->addKey($table, 'is_active');
$migration->addKey($table, ['locations_id', 'is_active'], 'location_active');
$migration->addKey($table, ['host_itemtype', 'host_items_id'], 'host_asset');
}
/**
@@ -311,6 +289,106 @@ function plugin_urbackup_install_update_serverassets_table(Migration $migration)
$migration->addKey($table, ['itemtype', 'items_id'], 'item');
}
/**
* Encrypt api_password values still stored in plaintext.
*
* Runs on every install/update, but skips values that already use the
* GLPIKey encrypted format (idempotent). If the GLPI key is unavailable,
* the plaintext value is kept to avoid data loss.
*
* @return void
*/
function plugin_urbackup_install_encrypt_api_passwords(): void
{
global $DB;
$table = 'glpi_plugin_urbackup_servers';
if (!$DB->tableExists($table)) {
return;
}
$iterator = $DB->request([
'FROM' => $table,
'WHERE' => ['api_password' => ['<>', '']],
]);
foreach ($iterator as $row) {
$password = (string) $row['api_password'];
if ($password === '' || Server::isApiPasswordEncrypted($password)) {
continue;
}
$encrypted = (new GLPIKey())->encrypt($password);
if ($encrypted === '') {
continue;
}
$DB->update(
$table,
['api_password' => $encrypted],
['id' => (int) $row['id']]
);
}
}
/**
* Ensure the `enable_computer` config row exists (default: enabled).
*
* Computer is registered as a plain tab (not a capacity), so its default
* state must be stored in the config table to keep it backward compatible
* with previous versions where Computer was always enabled. Idempotent.
*
* @return void
*/
function plugin_urbackup_install_add_enable_computer_config(): void
{
global $DB;
$table = 'glpi_plugin_urbackup_configs';
if (!$DB->tableExists($table)) {
return;
}
$iterator = $DB->request([
'FROM' => $table,
'WHERE' => ['name' => 'enable_computer'],
]);
if (count($iterator) > 0) {
return;
}
$DB->insert($table, [
'name' => 'enable_computer',
'value' => '1',
'date_creation' => $_SESSION['glpi_currenttime'] ?? date('Y-m-d H:i:s'),
]);
}
/**
* Drop a plugin table through Migration.
*
* Shared helper used by both install and uninstall processes
* (uninstall.php requires install.php to get this function).
*
* @param Migration $migration Migration instance
* @param string $table Table name
*
* @return void
*/
function plugin_urbackup_migration_drop_table(Migration $migration, string $table): void
{
global $DB;
if (!$DB->tableExists($table)) {
return;
}
$migration->dropTable($table);
}
/**
* Drop legacy tables that are no longer used.
*
@@ -330,11 +408,14 @@ function plugin_urbackup_install_drop_legacy_tables(Migration $migration): void
}
/**
* Convert old glpi_plugin_urbackup_assettypes to GLPI 11 capacity system
* and auto-enable UrBackup capacity on all existing Asset Definitions.
* Convert old glpi_plugin_urbackup_assettypes to GLPI 11 capacity system.
*
* After this migration, admins can disable UrBackup per Asset Definition
* via the native Capacities UI (Config > Asset definitions > Capacities).
* The legacy table is only kept to drop the obsolete `is_default` column.
* Enabling/disabling the UrBackup capacity on Asset Definitions is done by
* the admin via the native Capacities UI (Config > Asset definitions >
* Capacities). GLPI 11.0.x provides no public API to programmatically enable
* a capacity (it is processed from the `capacities` form input in
* AssetDefinition::post_updateItem()).
*
* @param Migration $migration Migration instance
*
@@ -358,23 +439,6 @@ function plugin_urbackup_install_convert_assettypes_to_capacities(Migration $mig
}
}
// Auto-enable UrBackup capacity on ALL Asset Definitions
// so the tab appears out of the box for any asset type.
if (class_exists(AssetDefinitionManager::class)) {
try {
$manager = AssetDefinitionManager::getInstance();
foreach ($manager->getDefinitions() as $definition) {
if (!$definition->hasCapacity(UrBackupCapacity::class)) {
$definition->enableCapacity(UrBackupCapacity::class);
}
}
} catch (\Throwable $e) {
$migration->displayMessage(
__('Error enabling UrBackup capacity on definitions: ', 'urbackup') . $e->getMessage()
);
}
}
// Drop is_default column (no longer needed in capacity system)
if ($has_is_default) {
$migration->dropField($table, 'is_default');
+8 -1
View File
@@ -8,6 +8,10 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_urbackup_configs` (
KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
-- Default configuration: UrBackup tab on Computer is enabled by default.
-- Can be changed from the plugin configuration page.
INSERT INTO `glpi_plugin_urbackup_configs` (`name`, `value`) VALUES ('enable_computer', '1');
CREATE TABLE IF NOT EXISTS `glpi_plugin_urbackup_servers` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`entities_id` INT UNSIGNED NOT NULL DEFAULT 0,
@@ -27,6 +31,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_urbackup_servers` (
`last_api_message` TEXT DEFAULT NULL,
`last_api_check` TIMESTAMP NULL DEFAULT NULL,
`comment` TEXT DEFAULT NULL,
`host_itemtype` VARCHAR(255) DEFAULT NULL,
`host_items_id` INT UNSIGNED NOT NULL DEFAULT 0,
`date_creation` TIMESTAMP NULL DEFAULT NULL,
`date_mod` TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (`id`),
@@ -35,7 +41,8 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_urbackup_servers` (
KEY `locations_id` (`locations_id`),
KEY `users_id` (`users_id`),
KEY `is_active` (`is_active`),
KEY `location_active` (`locations_id`, `is_active`)
KEY `location_active` (`locations_id`, `is_active`),
KEY `host_asset` (`host_itemtype`, `host_items_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;
CREATE TABLE IF NOT EXISTS `glpi_plugin_urbackup_serverassets` (
+2 -19
View File
@@ -14,6 +14,8 @@ if (!defined('GLPI_ROOT')) {
die(__('Sorry. You cannot access this file directly.', 'urbackup'));
}
require_once __DIR__ . '/install.php';
/**
* Uninstall plugin.
*
@@ -37,22 +39,3 @@ function plugin_urbackup_uninstall_process(): bool
return true;
}
/**
* Drop a plugin table through Migration.
*
* @param Migration $migration Migration instance
* @param string $table Table name
*
* @return void
*/
function plugin_urbackup_migration_drop_table(Migration $migration, $table): void
{
global $DB;
if (!$DB->tableExists($table)) {
return;
}
$migration->dropTable($table);
}