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

This commit is contained in:
johnnyq
2026-11-21 18:32:43 -05:00
parent 536b5352f2
commit adb74fc04b
3 changed files with 45 additions and 9 deletions

View File

@@ -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

View File

@@ -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('<strong>' . escapeHtml($rmm_client['rmm_client_name']) . '</strong> unlinked - its devices no longer come in. Their assets stay.', 'error');

View File

@@ -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;