From adb74fc04b7f2a3d5f2d64f7e9a5edd1df62cb17 Mon Sep 17 00:00:00 2001 From: johnnyq Date: Sat, 21 Nov 2026 18:32:43 -0500 Subject: [PATCH] QoL 74: unlinking an RMM client now removes its device records so its assets no longer show as in the RMM or count as managed devices, existing unlinked clients are cleaned up at the next sync, and relinking matches the devices back to the same assets --- CHANGELOG.md | 14 ++++++++++++++ admin/post/rmm.php | 12 +++--------- functions/rmm.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6157f32d3..8b29291e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,20 @@ This file documents all notable changes made to ITFlow. +## [26.12.1] + +### Upgrading + +Update from Maintenance > Update — Queue Update hands the job to cron and it applies on its own. There is no database change in this release, so nothing else is required. + +### Bug Fixes + +- RMM Providers: unlinking a client left its assets showing as still in the RMM - the dot on the assets list, the card on the asset, Take Control and the RMM filter, all with what the RMM said when it was unlinked - and they went on counting as managed devices on recurring invoices. Now they stay as they are but are no longer in the RMM, and each asset's history says its client was unlinked. A client you unlinked before this update is put right at the next sync. Link it again and its devices are matched back to the same assets. + +### Developer Updates + +- `rmmForgetClientDevices($rmm_client_id, $name, $why)` in `functions/rmm.php` deletes an RMM client's `rmm_devices` rows and adds an asset history line for each asset that is still there. Unlinking a client and linking it to another one both use it, and `rmmSync()` runs it for any of the provider's clients with `rmm_client_client_id = 0` that still have rows. + ## [26.12] ### Upgrading to 26.12 diff --git a/admin/post/rmm.php b/admin/post/rmm.php index 67230022d..bc6a102b1 100644 --- a/admin/post/rmm.php +++ b/admin/post/rmm.php @@ -172,14 +172,7 @@ $rmm_link_client = function (array $rmm_client, int $client_id) use ($mysqli, &$ $name = rmmProviderName($rmm_client); $left = 0; if ($before && $before !== $client_id) { - $sql = mysqli_query($mysqli, "SELECT rmm_device_asset_id FROM rmm_devices WHERE rmm_device_rmm_client_id = $rmm_client_id AND rmm_device_asset_id > 0"); - while ($device = mysqli_fetch_row($sql)) { - if (rmmAssetCurrent(intval($device[0]))) { - rmmAssetHistory(intval($device[0]), $name, 'no longer keeps it up - its client there was linked to another one'); - $left++; - } - } - mysqli_query($mysqli, "DELETE FROM rmm_devices WHERE rmm_device_rmm_client_id = $rmm_client_id"); + $left = rmmForgetClientDevices($rmm_client_id, $name, 'its client there was linked to another one'); } mysqli_query($mysqli, "UPDATE rmm_clients SET rmm_client_client_id = $client_id, rmm_client_synced_at = NULL WHERE rmm_client_id = $rmm_client_id"); $client_name = (string) getFieldById('clients', $client_id, 'client_name'); @@ -248,7 +241,7 @@ if (isset($_GET['link_rmm_suggested'])) { } -// Unlink - its devices stop coming in; their assets stay +// Unlink - its devices stop coming in; their assets stay, no longer in the RMM if (isset($_GET['unlink_rmm_client'])) { validateCSRFToken(); @@ -259,6 +252,7 @@ if (isset($_GET['unlink_rmm_client'])) { redirect(); } $rmm_client_id = intval($rmm_client['rmm_client_id']); + rmmForgetClientDevices($rmm_client_id, rmmProviderName($rmm_client), 'its client there was unlinked'); mysqli_query($mysqli, "UPDATE rmm_clients SET rmm_client_client_id = 0, rmm_client_synced_at = NULL WHERE rmm_client_id = $rmm_client_id"); logAudit("RMM Provider", "Unlink", "$session_name unlinked " . escapeSql(rmmProviderName($rmm_client) . " client {$rmm_client['rmm_client_name']} from " . $rmm_client['client_name']), intval($rmm_client['rmm_client_client_id']), intval($rmm_client['rmm_client_provider_id'])); flashAlert('' . escapeHtml($rmm_client['rmm_client_name']) . ' unlinked - its devices no longer come in. Their assets stay.', 'error'); diff --git a/functions/rmm.php b/functions/rmm.php index a0742f35a..ca396d3f5 100644 --- a/functions/rmm.php +++ b/functions/rmm.php @@ -724,6 +724,26 @@ function rmmDeviceSave(array $provider, array $rmm_client, int $client_id, array return $remote_id; } +/** + * Forget what the sync knew about one of an RMM's clients' devices - it was unlinked, or linked to + * another client. Their assets stay as they are, no longer in the RMM, and their history says why; + * linked again, its devices come in afresh and are matched to them. Returns how many assets stay. + */ +function rmmForgetClientDevices(int $rmm_client_id, string $name, string $why): int +{ + global $mysqli; + $left = 0; + $sql = mysqli_query($mysqli, "SELECT rmm_device_asset_id FROM rmm_devices WHERE rmm_device_rmm_client_id = $rmm_client_id AND rmm_device_asset_id > 0"); + while ($device = mysqli_fetch_row($sql)) { + if (rmmAssetCurrent(intval($device[0]))) { + rmmAssetHistory(intval($device[0]), $name, "no longer keeps it up - $why"); + $left++; + } + } + mysqli_query($mysqli, "DELETE FROM rmm_devices WHERE rmm_device_rmm_client_id = $rmm_client_id"); + return $left; +} + /* * ------------------------------------------------------------------ Syncing */ @@ -811,6 +831,14 @@ function rmmSync(array $provider, int $only = 0, int $max_seconds = 120): array } } + // Devices still kept for a client that was unlinked - before 26.12.1 unlinking left them + $unlinked = mysqli_query($mysqli, "SELECT DISTINCT rmm_device_rmm_client_id FROM rmm_devices + JOIN rmm_clients ON rmm_clients.rmm_client_id = rmm_device_rmm_client_id AND rmm_client_client_id = 0 + WHERE rmm_device_provider_id = $provider_id"); + while ($unlinked_row = mysqli_fetch_row($unlinked)) { + rmmForgetClientDevices(intval($unlinked_row[0]), $name, 'its client there was unlinked'); + } + foreach (rmmLinkedClients($provider_id, $only) as $rmm_client) { if (microtime(true) - $clock > $max_seconds) { $stopped = true;