connessione con hardware in assets
This commit is contained in:
+122
-58
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user