From f4bd50c1b14c7d175c4434db528bacb3493a270f Mon Sep 17 00:00:00 2001 From: mariano Date: Wed, 20 May 2026 11:16:14 +0200 Subject: [PATCH] pulsanti e sistemazione create client --- front/asset.form.php | 32 ++++++++++++++ front/server.form.php | 13 ++++++ src/AssetTab.php | 12 ----- src/Server.php | 68 ++++++++++++++++++++++++++++- templates/asset/asset_tab.html.twig | 8 ---- 5 files changed, 112 insertions(+), 21 deletions(-) diff --git a/front/asset.form.php b/front/asset.form.php index 42dfeff..fe666f5 100644 --- a/front/asset.form.php +++ b/front/asset.form.php @@ -152,6 +152,38 @@ if (isset($_POST['execute'])) { } } break; + case 'create_client': + if (Profile::canCurrentUser(CREATE)) { + $link = ServerAsset::getLinkForAsset($itemtype, $items_id, false); + if ($link !== null) { + $server = new \GlpiPlugin\Urbackup\Server(); + $server->getFromDB((int) $link['plugin_urbackup_servers_id']); + $client_name = ServerAsset::getAssetName($itemtype, $items_id); + try { + $api = new \GlpiPlugin\Urbackup\UrbackupApiClient($server); + $api->addClient($client_name); + } catch (\Throwable $e) { + // ignore + } + } + } + break; + case 'delete_client': + if (Profile::canCurrentUser(PURGE)) { + $link = ServerAsset::getLinkForAsset($itemtype, $items_id, false); + if ($link !== null) { + $server = new \GlpiPlugin\Urbackup\Server(); + $server->getFromDB((int) $link['plugin_urbackup_servers_id']); + $client_name = ServerAsset::getAssetName($itemtype, $items_id); + try { + $api = new \GlpiPlugin\Urbackup\UrbackupApiClient($server); + $api->removeClient($client_name); + } catch (\Throwable $e) { + // ignore + } + } + } + break; case 'set_internet_mode': if (Profile::canCurrentUser(UPDATE)) { $enabled = (int) ($_POST['internet_mode'] ?? 0) === 1; diff --git a/front/server.form.php b/front/server.form.php index e6bb5de..41e2cc1 100644 --- a/front/server.form.php +++ b/front/server.form.php @@ -2,6 +2,7 @@ use GlpiPlugin\Urbackup\Profile; use GlpiPlugin\Urbackup\Server; +use GlpiPlugin\Urbackup\ServerAsset; use Html; if (!defined('GLPI_ROOT')) { @@ -17,6 +18,18 @@ if (!Profile::canCurrentUser(UPDATE)) { $server = new Server(); if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_POST['link_asset'])) { + $itemtype = (string) ($_POST['itemtype'] ?? ''); + $items_id = (int) ($_POST['items_id'] ?? 0); + $server_id = (int) ($_POST['id'] ?? 0); + + if ($itemtype !== '' && $items_id > 0 && $server_id > 0) { + ServerAsset::connectAssetToServer($itemtype, $items_id, $server_id); + } + + Html::redirect(PLUGIN_URBACKUP_WEB_DIR . '/front/server.php?id=' . $server_id); + } + $id = $_POST['id'] ?? 0; if ($id > 0) { diff --git a/src/AssetTab.php b/src/AssetTab.php index b9e4a6d..0a21d67 100644 --- a/src/AssetTab.php +++ b/src/AssetTab.php @@ -116,8 +116,6 @@ class AssetTab extends CommonDBTM */ private static function showNoServerLinkedBlock(CommonDBTM $item): void { - $asset_location_id = LocationHelper::getAssetLocationId($item); - $root_location_id = LocationHelper::getRootLocationIdForAsset($item); $is_sub_location = LocationHelper::assetIsInSubLocation($item); $servers = LocationHelper::getAvailableServersForAsset($item); @@ -128,16 +126,6 @@ class AssetTab extends CommonDBTM echo ""; echo ""; - echo ""; - echo ""; - echo ""; - echo ""; - - echo ""; - echo ""; - echo ""; - echo ""; - if ($is_sub_location) { echo ""; echo "
" . htmlspecialchars(__('UrBackup server selection', 'urbackup')) . "
" . htmlspecialchars(__('Asset location ID', 'urbackup')) . "" . htmlspecialchars((string) $asset_location_id) . "
" . htmlspecialchars(__('Root location ID', 'urbackup')) . "" . htmlspecialchars((string) $root_location_id) . "
"; diff --git a/src/Server.php b/src/Server.php index d154409..bf18816 100644 --- a/src/Server.php +++ b/src/Server.php @@ -904,6 +904,51 @@ $apiStatus = (int) ($this->fields['last_api_status'] ?? 0); return; } + $serverLocationId = (int) ($server->fields['locations_id'] ?? 0); + $linkableAssets = []; + if ($serverLocationId > 0) { + $itemtypes = Config::getEnabledItemtypes(); + foreach ($unlinkedClients as $uc) { + $clientName = (string) ($uc['name'] ?? ''); + if ($clientName === '') { + continue; + } + $clientNameLower = strtolower($clientName); + foreach ($itemtypes as $itemtype) { + if (!class_exists($itemtype)) { + continue; + } + $assetItem = new $itemtype(); + if (!$assetItem instanceof CommonDBTM) { + continue; + } + $table = $assetItem->getTable(); + if (!$DB->tableExists($table)) { + continue; + } + $assetIterator = $DB->request([ + 'FROM' => $table, + 'WHERE' => [ + 'name' => $clientName, + 'is_deleted' => 0, + ], + 'LIMIT' => 1, + ]); + foreach ($assetIterator as $assetRow) { + $assetLocationId = (int) ($assetRow['locations_id'] ?? 0); + $rootLocationId = LocationHelper::getRootLocationId($assetLocationId); + if ($rootLocationId > 0 && $rootLocationId === $serverLocationId) { + $linkableAssets[$clientNameLower] = [ + 'itemtype' => $itemtype, + 'items_id' => (int) $assetRow['id'], + ]; + } + break; + } + } + } + } + echo ''; echo ''; echo ''; @@ -912,11 +957,15 @@ $apiStatus = (int) ($this->fields['last_api_status'] ?? 0); echo ''; echo ''; echo ''; + echo ''; echo ''; echo ''; echo ''; foreach ($unlinkedClients as $uc) { + $clientName = (string) ($uc['name'] ?? 'Unknown'); + $clientNameLower = strtolower($clientName); + $online = $uc['online'] ?? null; $apiStatusString = $uc['status'] ?? ''; $statusHtml = self::renderOnlineBadge($online, $apiStatusString); @@ -928,11 +977,28 @@ $apiStatus = (int) ($this->fields['last_api_status'] ?? 0); $clientVersion = $uc['client_version_string'] ?? $uc['client_version'] ?? '-'; echo ''; - echo ''; + echo ''; echo ''; echo ''; echo ''; echo ''; + echo ''; echo ''; } diff --git a/templates/asset/asset_tab.html.twig b/templates/asset/asset_tab.html.twig index c3bc354..909971e 100644 --- a/templates/asset/asset_tab.html.twig +++ b/templates/asset/asset_tab.html.twig @@ -7,14 +7,6 @@ - - - - - - - - {% if is_sub_location %}
' . htmlspecialchars(__('Status', 'urbackup')) . '' . htmlspecialchars(__('Last backup', 'urbackup')) . '' . htmlspecialchars(__('IP address', 'urbackup')) . '' . htmlspecialchars(__('Actions', 'urbackup')) . '
' . htmlspecialchars((string) ($uc['name'] ?? 'Unknown')) . '' . htmlspecialchars($clientName) . '' . htmlspecialchars($clientVersion) . '' . $statusHtml . '' . htmlspecialchars($lastBackup ?: '-') . '' . htmlspecialchars($clientIp ?: '-') . ''; + if (isset($linkableAssets[$clientNameLower])) { + $match = $linkableAssets[$clientNameLower]; + $formAction = PLUGIN_URBACKUP_WEB_DIR . '/front/server.form.php'; + echo '
'; + echo Html::hidden('_glpi_csrf_token', ['value' => Session::getNewCSRFToken()]); + echo Html::hidden('itemtype', ['value' => $match['itemtype']]); + echo Html::hidden('items_id', ['value' => $match['items_id']]); + echo Html::hidden('id', ['value' => (int) $server->fields['id']]); + echo ''; + Html::closeForm(); + } else { + echo ''; + } + echo '
{{ __('UrBackup server selection', 'urbackup') }}
{{ __('Asset location ID', 'urbackup') }}{{ asset_location_id }}
{{ __('Root location ID', 'urbackup') }}{{ root_location_id }}