mirror of
https://github.com/itflow-org/itflow
synced 2026-11-23 21:07:22 +00:00
QoL 76: RMM devices can be linked to another asset from their card with the old one archived, assets get their contact from the signed-in user one at a time or in bulk, each provider picks the asset types its devices come in as with database 4.1.2, the client overview shows a card of device states with new assets list filters and overdue devices under Needs attention, Bring back is offered as soon as an asset is deleted, and the API returns asset_rmm with each asset
This commit is contained in:
19
CHANGELOG.md
19
CHANGELOG.md
@@ -6,18 +6,33 @@ This file documents all notable changes made to ITFlow.
|
||||
|
||||
### 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.
|
||||
Update from Maintenance > Update — Queue Update hands the job to cron, which updates the files and applies this release's one database change (4.1.1 to 4.1.2) in the same pass. It adds a column to RMM providers for the asset type each kind of device comes in as, and changes nothing that is there. Nothing else is required.
|
||||
|
||||
### New Features & Updates
|
||||
|
||||
- RMM Providers: Link to another asset, in the ⋮ menu on an asset's RMM card, links the device to the asset it really is - when it was matched to the wrong one, or came in as a new asset beside the one you'd entered for it. Pick the asset: it keeps its name and what you've entered, and gets what it's missing from the RMM straight away. Archive, ticked when the RMM made the asset the device had, archives that one, with Undo. Assets another device already has can't be picked.
|
||||
- RMM Providers: when an asset has nobody assigned and the person signed in to it is one of the client's contacts, its RMM card offers Assign with their name. The username is matched to the contact's email or name - RDG\mwhitfield, marla.whitfield and AzureAD\MarlaWhitfield are all Marla Whitfield - and nothing is offered when no contact matches or more than one could. Bulk Action > Assign Signed-in Users on the assets list does it for every asset ticked that has nobody assigned.
|
||||
- RMM Providers: a provider's Edit has Asset types - what desktops, laptops, servers and virtual machines come in as, so they can be your own types, such as a Workstation, instead of ITFlow's. Assets whose type the RMM set follow a change at the next sync; one whose type you changed keeps yours. A type of yours that is renamed is renamed here too.
|
||||
- RMM Providers: a client with devices in an RMM has a card for it on its overview, beside Assets - how many are online, offline and overdue, and how many need a reboot, have patches waiting or have checks failing. Each line opens those devices on the assets list, whose RMM filter now also shows the ones that need a reboot, have patches waiting or have checks failing. Devices overdue are counted under Needs attention too.
|
||||
- RMM Providers: Bring back is on the client's row on the RMM Clients page as soon as you delete an asset that came from the RMM, rather than after the next sync, and the message for deleting it says so.
|
||||
- API: assets read through the API come with asset_rmm - each of the asset's devices in an RMM, with its status, when it was last seen, who is signed in, its IPs, and whether it needs a reboot, has patches waiting or has checks failing.
|
||||
|
||||
### 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.
|
||||
- RMM Providers: reinstalling a device's agent in Tactical - which gives it a new id there - brought it in as a second asset, even with the same serial number and name, and marked the first one as no longer in Tactical. Deleting the extra asset and using Bring back made it again. A reinstalled agent is now matched to the asset its old one had. If you already have an extra asset from this, delete it, click Sync Now, then use Bring back on its client's row on the RMM Clients page.
|
||||
- RMM Providers: reinstalling a device's agent in Tactical - which gives it a new id there - brought it in as a second asset, even with the same serial number and name, and marked the first one as no longer in Tactical. Deleting the extra asset and using Bring back made it again. A reinstalled agent is now matched to the asset its old one had. If you already have an extra asset from this, open it and use Link to another asset in the ⋮ menu on its RMM card to link the device to the first one - it archives the extra asset.
|
||||
- RMM Providers: a device moved to another linked client in Tactical stayed with its asset at the first client, and that asset took the new client's location. It is now matched or made at the client it moved to. The first client's asset stays there, no longer kept up, and its history says where the device went. Devices that moved before this update are put right at the next sync.
|
||||
|
||||
### 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.
|
||||
- `rmmSync()` marks a client's unlisted devices removed before it saves the listed ones, and `rmmAssetMatch()` no longer counts a removed device as holding its asset, so a reinstalled agent (a new `remote_id`) is matched to the asset its old one had, and the old removed row is deleted. `rmmDeviceSave()` treats a device whose asset belongs to another client as new at this one, leaving that asset with a history line.
|
||||
- Database 4.1.2: `rmm_providers.rmm_provider_asset_types`, JSON of the type the sync guesses (`Desktop`, `Laptop`, `Server`, `Virtual Machine`) to the asset type to make it - NULL, or a kind it doesn't name, keeps the guessed type. Re-runnable. `rmmProviderAssetTypes($provider)` gives the four, falling back to the guessed type for one that no longer exists, and `rmmDeviceSave()` applies them before matching and following. `rmmProviderCheck()`, `rmmProviderWrite()` and `rmmProviderChangeLines()` take them from the window's `asset_types[...]`, and `assetTypeRenameEverywhere()` calls `rmmProviderAssetTypesRename()`.
|
||||
- `rmmDeviceLinkAsset($device, $asset_id)` relinks a device by hand: it refuses an asset of another client or one another live device of the provider has, deletes the provider's removed rows on the asset picked, resets `rmm_device_synced` to `[]` so the follow rule only fills what is empty, and writes both assets' history. The `rmm_link_asset` handler in `agent/post/asset.php` then runs `rmmSync()` for the device's RMM client and archives the old asset with `undoArchiveBefore()` when asked. `rmmDeviceById()` and `rmmDeviceLinkTargets()` feed `agent/modals/asset/asset_rmm_link.php`.
|
||||
- `rmmUserContact($client_id, $logged_user)` matches a signed-in user to exactly one unarchived contact by `rmmUserKey()` - the username after the last backslash and before any @, letters and digits only - against the contact's email local part, full name, first initial and last name, and first name and last initial. Generic accounts such as administrator are never matched. `rmmAssignUserContacts()` uses it through `assetQuickSet()` for the `bulk_rmm_assign_contacts` handler, and the card's Assign posts `rmm_assign_contact`.
|
||||
- `rmmClientSummary($client_id)` counts a client's unarchived assets with a live device by state, cached per request, for the client overview card and the Devices overdue item in `clientOverviewAttention()`. `agent/assets.php` has `rmm=reboot`, `rmm=patches` and `rmm=checks`.
|
||||
- `rmmAssetsDeleted($asset_ids)` sets `rmm_device_asset_id = 0` for deleted assets - single, bulk and API delete - so Bring back counts them at once.
|
||||
- `api/v1/assets/read.php` sets `$api_asset_rmm`, and `api/v1/read_output.php` adds `asset_rmm` through `rmmApiAttach()`. It picks device columns only - never the provider's key or secret.
|
||||
|
||||
## [26.12]
|
||||
|
||||
|
||||
23
admin/database_updates/4.1.2.php
Normal file
23
admin/database_updates/4.1.2.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* ITFlow - Database update to version 4.1.2 (from 4.1.1)
|
||||
* Included by admin/database_updates.php - do not access directly
|
||||
*
|
||||
* The asset type each kind of device an RMM brings in comes in as, picked in the provider's Edit:
|
||||
*
|
||||
* rmm_providers.rmm_provider_asset_types JSON of the type the sync guesses (Desktop, Laptop,
|
||||
* Server, Virtual Machine) => the asset type to make it.
|
||||
* NULL, or a kind it doesn't name, keeps the guessed type.
|
||||
*
|
||||
* Re-runnable: it only adds the column where it isn't.
|
||||
*/
|
||||
|
||||
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
|
||||
|
||||
$itflow_column = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'rmm_providers' AND COLUMN_NAME = 'rmm_provider_asset_types' LIMIT 1");
|
||||
|
||||
if ($itflow_column && mysqli_num_rows($itflow_column) === 0) {
|
||||
mysqli_query($mysqli, "ALTER TABLE `rmm_providers` ADD `rmm_provider_asset_types` varchar(1000) DEFAULT NULL AFTER `rmm_provider_alerts_note`");
|
||||
}
|
||||
@@ -5,7 +5,8 @@
|
||||
* picked when it is added and stays; each kind brings its own fields (rmmProviderKinds() in
|
||||
* functions/rmm.php) - addresses, a key, a secret. A key or secret is never put back into the
|
||||
* page, so left empty it stays. A kind that has alerts can have them open tickets, from the
|
||||
* level picked, in a category. Save asks first (check_rmm_provider, admin/post/rmm.php): what
|
||||
* level picked, in a category. Desktops, laptops, servers and virtual machines each come in as
|
||||
* the asset type picked for them (rmmProviderAssetTypes()). Save asks first (check_rmm_provider, admin/post/rmm.php): what
|
||||
* is wrong shows beside each field and the window stays open; once all is right it saves, and
|
||||
* tries the connection.
|
||||
*/
|
||||
@@ -150,6 +151,31 @@ ob_start();
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if (rmmAssetTypesReady()) {
|
||||
$device_types = rmmProviderAssetTypes($row); ?>
|
||||
<div class="mb-3" data-rmm-asset-types>
|
||||
<label class="form-label">Asset types</label>
|
||||
<div class="row g-2">
|
||||
<?php foreach (rmmDeviceKinds() as $guessed => $kind_label) {
|
||||
$type_id = 'rmmProviderType' . str_replace(' ', '', $guessed); ?>
|
||||
<div class="col-sm-6">
|
||||
<label class="form-label small text-body-secondary mb-1" for="<?= $type_id ?>"><?= escapeHtml($kind_label) ?></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw <?= escapeHtml(assetTypeInfo($guessed)['icon']) ?>"></i></span>
|
||||
<select class="form-select select2" name="asset_types[<?= escapeHtml($guessed) ?>]" id="<?= $type_id ?>">
|
||||
<?php foreach (assetTypePicker($device_types[$guessed]) as $type_name => $type_icon) { ?>
|
||||
<option <?= $type_name === $device_types[$guessed] ? 'selected' : '' ?><?= selectOptionData(['icon' => "fas $type_icon"]) ?>><?= escapeHtml($type_name) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<?= $error_for('asset_types') ?>
|
||||
<div class="form-text">What its devices come in as. A device the sync typed follows a change here at the next sync; one whose type you changed on the asset keeps yours. Recurring invoice lines that count managed devices by type count these.</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="<?= $row ? 'edit_rmm_provider' : 'add_rmm_provider' ?>" class="btn btn-primary fw-bold" data-rmm-provider-save>
|
||||
|
||||
@@ -415,13 +415,27 @@ if (mysqli_num_rows($sql) == 0) {
|
||||
$rmm_each_name = rmmProviderName($rmm_each);
|
||||
$rmm_each_kind = rmmKind($rmm_each);
|
||||
$rmm_each_urls = $rmm_each !== $rmm_device && $rmm_state['key'] !== 'removed' ? rmmDeviceUrls($rmm_each) : [];
|
||||
$rmm_elsewhere = intval($rmm_each['rmm_client_client_id'] ?? 0) && intval($rmm_each['rmm_client_client_id']) !== $client_id; ?>
|
||||
$rmm_elsewhere = intval($rmm_each['rmm_client_client_id'] ?? 0) && intval($rmm_each['rmm_client_client_id']) !== $client_id;
|
||||
// Who is signed in, when they are one of the client's contacts and the asset has nobody yet - offered once
|
||||
$rmm_user_contact = $can_edit && !$asset_contact_id && empty($rmm_suggested) && $rmm_state['key'] !== 'removed'
|
||||
? rmmUserContact($client_id, (string) $rmm_each['rmm_device_logged_user']) : null;
|
||||
$rmm_suggested = !empty($rmm_suggested) || $rmm_user_contact; ?>
|
||||
<div class="card mb-3" data-rmm-card="<?= escapeHtml($rmm_state['key']) ?>">
|
||||
<div class="card-header d-flex align-items-center">
|
||||
<h3 class="card-title mb-0"><i class="<?= escapeHtml($rmm_each_kind['icon'] ?? 'fas fa-laptop-medical') ?> fa-fw text-secondary me-2"></i><?= escapeHtml($rmm_each_name) ?></h3>
|
||||
<?php if (isset($rmm_each_urls['open'])) { ?>
|
||||
<a class="ms-auto ps-2 text-secondary" href="<?= escapeHtml($rmm_each_urls['open']) ?>" target="_blank" rel="noopener noreferrer" title="<?= escapeHtml(($rmm_each_kind['links']['open'] ?? 'Open') . " - $rmm_each_name") ?>"><i class="fas fa-fw fa-external-link-alt"></i></a>
|
||||
<?php } ?>
|
||||
<?php if ($can_edit && $rmm_state['key'] !== 'removed') { ?>
|
||||
<div class="dropdown <?= isset($rmm_each_urls['open']) ? 'ps-1' : 'ms-auto' ?>">
|
||||
<button class="btn btn-sm btn-link text-secondary p-0 px-1" type="button" data-bs-toggle="dropdown" title="More" aria-label="More"><i class="fas fa-fw fa-ellipsis-v"></i></button>
|
||||
<div class="dropdown-menu dropdown-menu-end">
|
||||
<a class="dropdown-item ajax-modal" href="#" data-modal-url="modals/asset/asset_rmm_link.php?device_id=<?= intval($rmm_each['rmm_device_id']) ?>">
|
||||
<i class="fas fa-fw fa-link me-2"></i>Link to another asset
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<?php if ($rmm_state['key'] === 'removed') { ?>
|
||||
@@ -436,7 +450,16 @@ if (mysqli_num_rows($sql) == 0) {
|
||||
</dd>
|
||||
<?php if ((string) $rmm_each['rmm_device_logged_user'] !== '') { ?>
|
||||
<dt><?= $rmm_state['key'] === 'online' ? 'Signed in' : 'Last user' ?></dt>
|
||||
<dd><?= escapeHtml($rmm_each['rmm_device_logged_user']) ?></dd>
|
||||
<dd><?= escapeHtml($rmm_each['rmm_device_logged_user']) ?>
|
||||
<?php if ($rmm_user_contact) { ?>
|
||||
<form class="d-inline" action="post.php" method="post">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="asset_id" value="<?= $asset_id ?>">
|
||||
<input type="hidden" name="contact_id" value="<?= intval($rmm_user_contact['contact_id']) ?>">
|
||||
<button type="submit" name="rmm_assign_contact" class="btn btn-link btn-sm p-0 ms-1 align-baseline" data-rmm-assign-contact title="Make <?= escapeHtml($rmm_user_contact['contact_name']) ?> this asset's contact"><i class="fas fa-user-check me-1"></i>Assign <?= escapeHtml($rmm_user_contact['contact_name']) ?></button>
|
||||
</form>
|
||||
<?php } ?>
|
||||
</dd>
|
||||
<?php } ?>
|
||||
<?php if ($rmm_each['rmm_device_boot_at'] && $rmm_state['key'] === 'online') { ?>
|
||||
<dt>Up since</dt>
|
||||
|
||||
@@ -83,12 +83,13 @@ if (isset($_GET['tags']) && is_array($_GET['tags']) && !empty($_GET['tags'])) {
|
||||
$tag_query = '';
|
||||
}
|
||||
|
||||
// RMM Filter - only once an RMM provider is set up (functions/rmm.php): any of its devices, online, offline or overdue, or not one of them.
|
||||
// With one provider it goes by its name
|
||||
// RMM Filter - only once an RMM provider is set up (functions/rmm.php): any of its devices, online, offline or overdue, needing
|
||||
// a reboot, with patches waiting or checks failing, or not one of them. With one provider it goes by its name
|
||||
$rmm_set_up = (bool) rmmProviders();
|
||||
$rmm_name = $rmm_set_up ? rmmListLabel() : '';
|
||||
$rmm_short = $rmm_name === 'an RMM' ? 'RMM' : $rmm_name;
|
||||
$rmm_filters = ['any' => "In $rmm_name", 'online' => "$rmm_short: online", 'offline' => "$rmm_short: offline", 'overdue' => "$rmm_short: overdue", 'none' => "Not in $rmm_name"];
|
||||
$rmm_filters = ['any' => "In $rmm_name", 'online' => "$rmm_short: online", 'offline' => "$rmm_short: offline", 'overdue' => "$rmm_short: overdue",
|
||||
'reboot' => "$rmm_short: needs a reboot", 'patches' => "$rmm_short: patches waiting", 'checks' => "$rmm_short: checks failing", 'none' => "Not in $rmm_name"];
|
||||
$rmm_filter = $rmm_set_up && isset($rmm_filters[$_GET['rmm'] ?? '']) ? (string) $_GET['rmm'] : '';
|
||||
$rmm_in = "SELECT rmm_device_asset_id FROM rmm_devices WHERE rmm_device_asset_id > 0 AND rmm_device_removed_at IS NULL";
|
||||
$rmm_query = [
|
||||
@@ -97,6 +98,9 @@ $rmm_query = [
|
||||
'online' => "AND asset_id IN ($rmm_in AND rmm_device_status = 'online')",
|
||||
'offline' => "AND asset_id IN ($rmm_in AND rmm_device_status = 'offline')",
|
||||
'overdue' => "AND asset_id IN ($rmm_in AND rmm_device_status = 'overdue')",
|
||||
'reboot' => "AND asset_id IN ($rmm_in AND rmm_device_needs_reboot = 1)",
|
||||
'patches' => "AND asset_id IN ($rmm_in AND rmm_device_patches_pending = 1)",
|
||||
'checks' => "AND asset_id IN ($rmm_in AND rmm_device_checks_failing > 0)",
|
||||
'none' => "AND asset_id NOT IN ($rmm_in)",
|
||||
][$rmm_filter];
|
||||
|
||||
@@ -417,6 +421,12 @@ $search_noun = $type_filter ? $asset_type_tabs[$type_filter]['label'] : 'assets'
|
||||
<i class="fas fa-fw fa-map-marker-alt me-2"></i>Assign Location
|
||||
</a>
|
||||
<?php } ?>
|
||||
<?php if ($rmm_set_up) { ?>
|
||||
<button class="dropdown-item" title="The ones with nobody assigned get the contact signed in to them in <?= escapeHtml($rmm_short) ?>"
|
||||
type="submit" form="bulkActions" name="bulk_rmm_assign_contacts">
|
||||
<i class="fas fa-fw fa-user-check me-2"></i>Assign Signed-in Users
|
||||
</button>
|
||||
<?php } ?>
|
||||
<a class="dropdown-item ajax-modal" href="#"
|
||||
data-modal-url="modals/asset/asset_bulk_assign_physical_location.php"
|
||||
data-bulk="true">
|
||||
|
||||
@@ -30,6 +30,7 @@ $overview_contacts = clientOverviewContacts($client_id);
|
||||
$favorites = clientOverviewFavorites($client_id, $access);
|
||||
$upcoming = clientOverviewUpcoming($client_id, $access);
|
||||
$asset_summary = $access['docs'] ? clientOverviewAssets($client_id) : null;
|
||||
$rmm_summary = $access['docs'] ? rmmClientSummary($client_id) : null;
|
||||
$shared_items = clientOverviewSharedItems($client_id);
|
||||
$activity = clientOverviewActivity($client_id);
|
||||
$money = $access['financial'] ? clientOverviewMoney($client_id, $balance, $recurring_monthly) : null;
|
||||
@@ -468,6 +469,37 @@ $money_format = function ($amount) use ($currency_format, $client_currency_code,
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($rmm_summary) {
|
||||
$rmm_title = rmmListLabel() === 'an RMM' ? 'RMM' : rmmListLabel();
|
||||
$rmm_rows = [
|
||||
'online' => ['Online', 'fa-circle', 'success'], 'offline' => ['Offline', 'fa-circle', 'secondary'], 'overdue' => ['Overdue', 'fa-circle', 'danger'],
|
||||
'reboot' => ['Need a reboot', 'fa-redo-alt', 'warning'], 'patches' => ['Patches waiting', 'fa-download', 'warning'], 'checks' => ['Checks failing', 'fa-exclamation-triangle', 'danger'],
|
||||
]; ?>
|
||||
<div class="col-md-6 col-xl-4" data-rmm-summary>
|
||||
<div class="card h-100 mb-0">
|
||||
<div class="card-header d-flex align-items-center">
|
||||
<h3 class="card-title mb-0 me-auto"><?= escapeHtml($rmm_title) ?></h3>
|
||||
<a class="small" href="assets.php?client_id=<?= $client_id ?>&rmm=any">View all <?= $rmm_summary['total'] ?></a>
|
||||
</div>
|
||||
<div class="list-group list-group-flush">
|
||||
<?php foreach ($rmm_rows as $rmm_key => [$rmm_label, $rmm_icon, $rmm_tone]) {
|
||||
if (!$rmm_summary[$rmm_key]) {
|
||||
continue;
|
||||
} ?>
|
||||
<a href="assets.php?client_id=<?= $client_id ?>&rmm=<?= $rmm_key ?>" class="list-group-item list-group-item-action d-flex align-items-center gap-2 py-2" data-rmm-summary-row="<?= $rmm_key ?>">
|
||||
<i class="fas fa-fw <?= $rmm_icon ?> text-<?= $rmm_tone ?>" aria-hidden="true"></i>
|
||||
<span class="flex-grow-1"><?= $rmm_label ?></span>
|
||||
<span class="fw-semibold"><?= $rmm_summary[$rmm_key] ?></span>
|
||||
</a>
|
||||
<?php } ?>
|
||||
</div>
|
||||
<div class="card-footer small text-body-secondary py-2" title="<?= escapeHtml((string) $rmm_summary['synced_at']) ?>">
|
||||
As <?= escapeHtml($rmm_title) ?> said <?= escapeHtml(timeAgo($rmm_summary['synced_at'])) ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
<?php if ($shared_items) { ?>
|
||||
<div class="col-md-6 col-xl-4">
|
||||
<div class="card h-100 mb-0">
|
||||
|
||||
92
agent/modals/asset/asset_rmm_link.php
Normal file
92
agent/modals/asset/asset_rmm_link.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Link a device from an RMM to another of its client's assets, by hand (rmmDeviceLinkAsset() in
|
||||
* functions/rmm.php) - from the menu on its card on the asset page, for a device matched to the
|
||||
* wrong asset or brought in as a new one beside the asset you already had. The asset it has now
|
||||
* can be archived as it goes - ticked when the sync made it for this device.
|
||||
*/
|
||||
|
||||
require_once '../../../includes/modal_header.php';
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$device = rmmDeviceById(intval($_GET['device_id'] ?? 0));
|
||||
if (!$device || $device['rmm_device_removed_at'] !== null) {
|
||||
echo json_encode(['error' => 'That device is no longer in its RMM.']);
|
||||
exit;
|
||||
}
|
||||
$client_id = intval($device['rmm_device_client_id']);
|
||||
|
||||
enforceClientAccess();
|
||||
|
||||
$provider_name = rmmProviderName($device);
|
||||
$host = (string) $device['rmm_device_hostname'];
|
||||
$current_id = intval($device['rmm_device_asset_id']);
|
||||
$current = $current_id ? rmmAssetCurrent($current_id) : null;
|
||||
$current_serial = $current ? mb_strtolower(trim($current['serial'])) : '';
|
||||
// Made by the sync for this device - the one to archive once the device has the asset it is
|
||||
$current_made = $current_id && mysqli_num_rows(mysqli_query($mysqli, "SELECT 1 FROM asset_history WHERE asset_history_asset_id = $current_id
|
||||
AND asset_history_description LIKE '" . escapeSql(str_replace(['%', '_'], ['\%', '\_'], $provider_name)) . " brought it in from %' LIMIT 1")) > 0;
|
||||
|
||||
// The most likely ones first - the same name or serial number as the asset it has
|
||||
$targets = rmmDeviceLinkTargets($device);
|
||||
uasort($targets, function ($a, $b) use ($current, $current_serial) {
|
||||
$likely = function ($row) use ($current, $current_serial) {
|
||||
return $current && (strcasecmp(trim($row['asset_name']), trim($current['name'])) === 0
|
||||
|| ($current_serial !== '' && mb_strtolower(trim((string) $row['asset_serial'])) === $current_serial));
|
||||
};
|
||||
return [$likely($b), $a['taken'] !== null] <=> [$likely($a), $b['taken'] !== null] ?: strcasecmp($a['asset_name'], $b['asset_name']);
|
||||
});
|
||||
|
||||
ob_start();
|
||||
|
||||
?>
|
||||
|
||||
<div class="modal-header bg-dark">
|
||||
<h5 class="modal-title"><i class="fas fa-fw fa-link me-2"></i>Link <strong><?= escapeHtml($host) ?></strong> to another asset</h5>
|
||||
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
|
||||
<form action="post.php" method="post" autocomplete="off">
|
||||
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
|
||||
<input type="hidden" name="device_id" value="<?= intval($device['rmm_device_id']) ?>">
|
||||
<div class="modal-body">
|
||||
|
||||
<p class="mb-3">
|
||||
<?= escapeHtml($provider_name) ?>'s <strong><?= escapeHtml($host) ?></strong><?= $current ? ' is linked to <strong>' . escapeHtml($current['name']) . '</strong>' : '' ?>.
|
||||
Pick the asset it is - that asset keeps its name and what you've filled in, and gets what it's missing from <?= escapeHtml($provider_name) ?>.
|
||||
</p>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Asset <strong class="text-danger">*</strong></label>
|
||||
<div class="input-group">
|
||||
<span class="input-group-text"><i class="fas fa-fw fa-desktop"></i></span>
|
||||
<select class="form-select select2" name="asset_id" required>
|
||||
<option value="">- Select an asset -</option>
|
||||
<?php foreach ($targets as $target_id => $target) {
|
||||
$taken = $target['taken'] !== null;
|
||||
$about = $taken ? "Already $target[taken] in $provider_name" : trim(trim($target['asset_make'] . ' ' . $target['asset_model']) . ($target['asset_serial'] !== '' ? ' - ' . $target['asset_serial'] : ''), ' -'); ?>
|
||||
<option value="<?= $target_id ?>" <?= $taken ? 'disabled' : '' ?><?= selectOptionAsset($target['asset_type'], $about) ?>><?= escapeHtml($target['asset_name']) ?></option>
|
||||
<?php } ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if ($current) { ?>
|
||||
<div class="form-check">
|
||||
<input class="form-check-input" type="checkbox" name="archive_old" value="1" id="rmmLinkArchive" <?= $current_made ? 'checked' : '' ?>>
|
||||
<label class="form-check-label" for="rmmLinkArchive">Archive <strong><?= escapeHtml($current['name']) ?></strong><?= $current_made ? ' - ' . escapeHtml($provider_name) . ' made it for this device' : '' ?></label>
|
||||
</div>
|
||||
<?php } ?>
|
||||
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
<button type="submit" name="rmm_link_asset" class="btn btn-primary fw-bold"><i class="fas fa-link me-2"></i>Link</button>
|
||||
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fas fa-times me-2"></i>Cancel</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
require_once '../../../includes/modal_footer.php';
|
||||
@@ -279,9 +279,122 @@ if (isset($_GET['delete_asset'])) {
|
||||
// Its logins stay with the client, no longer pointing at an asset that is gone
|
||||
credentialsForget('asset', $asset_id);
|
||||
|
||||
// Its device in an RMM keeps no asset - Bring back on its client's row brings it in again (functions/rmm.php)
|
||||
$in_rmm = rmmAssetsDeleted([$asset_id]);
|
||||
|
||||
logAudit("Asset", "Delete", "$session_name deleted asset $asset_name", $client_id);
|
||||
|
||||
flashAlert("Asset <strong>" . stripslashes($asset_name) . "</strong> deleted", 'error');
|
||||
flashAlert("Asset <strong>" . stripslashes($asset_name) . "</strong> deleted" . ($in_rmm ? ' - the next sync won\'t bring it in again. Bring back on its client\'s row in Admin > RMM Providers does.' : ''), 'error');
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
// Link a device from an RMM to another of its client's assets (modals/asset/asset_rmm_link.php)
|
||||
if (isset($_POST['rmm_link_asset'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$device = rmmDeviceById(intval($_POST['device_id'] ?? 0));
|
||||
if (!$device) {
|
||||
flashAlert('That device is no longer in its RMM.', 'error');
|
||||
redirect();
|
||||
}
|
||||
$client_id = intval($device['rmm_device_client_id']);
|
||||
|
||||
enforceClientAccess();
|
||||
|
||||
$asset_id = intval($_POST['asset_id'] ?? 0);
|
||||
$had = intval($device['rmm_device_asset_id']);
|
||||
$had_row = $had ? rmmAssetCurrent($had) : null;
|
||||
$problem = rmmDeviceLinkAsset($device, $asset_id);
|
||||
if ($problem !== '') {
|
||||
flashAlert(escapeHtml($problem), 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
// What the asset picked is missing comes in now
|
||||
set_time_limit(180);
|
||||
$provider = rmmProviderRow($device['rmm_device_provider_id']);
|
||||
$sync = $provider ? rmmSync($provider, intval($device['rmm_device_rmm_client_id']), 60) : ['ok' => false, 'note' => ''];
|
||||
|
||||
$message = '<strong>' . escapeHtml($device['rmm_device_hostname']) . '</strong> in ' . escapeHtml(rmmProviderName($device)) . ' linked to <strong>' . escapeHtml((string) getFieldById('assets', $asset_id, 'asset_name')) . '</strong>';
|
||||
if (!$sync['ok'] && $sync['note'] !== '') {
|
||||
$message .= '. What it is missing comes in at the next sync - ' . escapeHtml($sync['note']);
|
||||
}
|
||||
|
||||
// And the asset it had archived, when asked - with Undo
|
||||
$undo = null;
|
||||
if (!empty($_POST['archive_old']) && $had_row && getFieldById('assets', $had, 'asset_archived_at') === null) {
|
||||
$undo = undoArchiveBefore('asset', $had);
|
||||
$had_name = escapeSql($had_row['name']);
|
||||
mysqli_query($mysqli, "UPDATE assets SET asset_archived_at = NOW() WHERE asset_id = $had");
|
||||
mysqli_query($mysqli, "INSERT INTO asset_history SET asset_history_status = 'Archived', asset_history_description = '$session_name archived $had_name', asset_history_asset_id = $had");
|
||||
logAudit("Asset", "Archive", "$session_name archived asset $had_name", $client_id, $had);
|
||||
$message .= ' - <strong>' . escapeHtml($had_row['name']) . '</strong> archived';
|
||||
}
|
||||
|
||||
flashAlert($message, $undo ? 'error' : 'success');
|
||||
if ($undo) {
|
||||
undoOfferArchive($undo);
|
||||
}
|
||||
|
||||
redirect("asset.php?client_id=$client_id&asset_id=$asset_id");
|
||||
|
||||
}
|
||||
|
||||
// An asset's contact from who is signed in to its device in an RMM (the card on the asset page)
|
||||
if (isset($_POST['rmm_assign_contact'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$asset_id = intval($_POST['asset_id'] ?? 0);
|
||||
$contact_id = intval($_POST['contact_id'] ?? 0);
|
||||
$client_id = intval(getFieldById('assets', $asset_id, 'asset_client_id'));
|
||||
if (!$client_id || !$contact_id) {
|
||||
flashAlert('That asset no longer exists.', 'error');
|
||||
redirect();
|
||||
}
|
||||
|
||||
enforceClientAccess();
|
||||
|
||||
[$ok, $message] = assetQuickSet($asset_id, 'asset_contact', (string) $contact_id);
|
||||
flashAlert(escapeHtml($message !== '' ? $message : 'Assigned'), $ok ? 'success' : 'error');
|
||||
|
||||
redirect();
|
||||
|
||||
}
|
||||
|
||||
// Assets' contacts from who is signed in to their devices - the ones with nobody assigned (the assets list's Bulk Action)
|
||||
if (isset($_POST['bulk_rmm_assign_contacts'])) {
|
||||
|
||||
validateCSRFToken();
|
||||
|
||||
enforceUserPermission('module_support', 2);
|
||||
|
||||
$asset_ids = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['asset_ids'] ?? [])))));
|
||||
foreach ($asset_ids as $asset_id) {
|
||||
$client_id = intval(getFieldById('assets', $asset_id, 'asset_client_id'));
|
||||
enforceClientAccess();
|
||||
}
|
||||
|
||||
$counts = rmmAssignUserContacts($asset_ids);
|
||||
$assigned = $counts['assigned'];
|
||||
$message = $assigned ? "Assigned <strong>$assigned</strong> asset" . ($assigned === 1 ? '' : 's') . ' to who is signed in' : 'Nobody assigned';
|
||||
if ($counts['had']) {
|
||||
$message .= ". {$counts['had']} already had a contact";
|
||||
}
|
||||
if ($counts['none']) {
|
||||
$message .= ". {$counts['none']} " . ($counts['none'] === 1 ? 'has' : 'have') . " nobody signed in who is one of the client's contacts";
|
||||
}
|
||||
if ($assigned) {
|
||||
logAudit("Asset", "Bulk Edit", "$session_name assigned $assigned assets to their signed-in users");
|
||||
}
|
||||
flashAlert($message, $assigned ? 'success' : 'info');
|
||||
|
||||
redirect();
|
||||
|
||||
|
||||
@@ -30,6 +30,9 @@ if (!empty($asset_id)) {
|
||||
// Its logins stay with the client, no longer pointing at it
|
||||
credentialsForget('asset', $asset_id);
|
||||
|
||||
// Its device in an RMM keeps no asset, for Bring back (functions/rmm.php)
|
||||
rmmAssetsDeleted([$asset_id]);
|
||||
|
||||
// Logging
|
||||
logAudit("Asset", "Delete", "$asset_name via API ($api_key_name)", $client_id);
|
||||
}
|
||||
|
||||
@@ -48,4 +48,5 @@ if (isset($_GET['asset_id'])) {
|
||||
// Output
|
||||
$api_tag_entity = 'asset'; // each one's tags
|
||||
$api_asset_specs = true; // and its details (functions/asset_type.php)
|
||||
$api_asset_rmm = true; // and its devices in an RMM (functions/rmm.php)
|
||||
require_once "../read_output.php";
|
||||
|
||||
@@ -25,6 +25,11 @@ if ($sql && mysqli_num_rows($sql) > 0) {
|
||||
$return_arr['data'] = assetSpecsApiAttach($return_arr['data']);
|
||||
}
|
||||
|
||||
// Each asset's devices in an RMM, as asset_rmm ($api_asset_rmm, set by api/v1/assets/read.php)
|
||||
if (!empty($api_asset_rmm)) {
|
||||
$return_arr['data'] = rmmApiAttach($return_arr['data']);
|
||||
}
|
||||
|
||||
echo json_encode($return_arr);
|
||||
exit();
|
||||
}
|
||||
|
||||
1
db.sql
1
db.sql
@@ -3513,6 +3513,7 @@ CREATE TABLE `rmm_providers` (
|
||||
`rmm_provider_alerts_since` datetime DEFAULT NULL,
|
||||
`rmm_provider_alerts_at` datetime DEFAULT NULL,
|
||||
`rmm_provider_alerts_note` varchar(500) DEFAULT NULL,
|
||||
`rmm_provider_asset_types` varchar(1000) DEFAULT NULL,
|
||||
`rmm_provider_created_at` datetime NOT NULL DEFAULT current_timestamp(),
|
||||
`rmm_provider_updated_at` datetime DEFAULT NULL ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`rmm_provider_id`)
|
||||
|
||||
@@ -290,6 +290,8 @@ function assetTypeRenameEverywhere($old, $new) {
|
||||
$lines++;
|
||||
}
|
||||
}
|
||||
// And the RMM providers that bring devices in as it (functions/rmm.php)
|
||||
rmmProviderAssetTypesRename((string) $old, (string) $new);
|
||||
return [$assets, $products, $lines];
|
||||
}
|
||||
|
||||
|
||||
@@ -119,6 +119,16 @@ function clientOverviewAttention($client_id, $access, $renewals) {
|
||||
}
|
||||
|
||||
if ($access['docs']) {
|
||||
// Devices the RMM says haven't checked in for longer than it allows (functions/rmm.php) - the same as assets.php?rmm=overdue
|
||||
$rmm_summary = rmmClientSummary($client_id);
|
||||
if ($rmm_summary) {
|
||||
$items[] = [
|
||||
'count' => $rmm_summary['overdue'],
|
||||
'label' => 'Devices overdue', 'one' => 'Device overdue', 'icon' => 'heartbeat', 'tone' => 'danger',
|
||||
'detail' => 'Not checking in to ' . (rmmListLabel() === 'an RMM' ? 'their RMM' : rmmListLabel()),
|
||||
'url' => "/agent/assets.php?client_id=$client_id&rmm=overdue",
|
||||
];
|
||||
}
|
||||
// More devices and users on a license than seats bought - a true-up to bill, or seats to free
|
||||
$items[] = [
|
||||
'count' => dashboardCount("SELECT COUNT(software_id) FROM software WHERE software_client_id = $client_id AND software_archived_at IS NULL
|
||||
|
||||
@@ -654,6 +654,8 @@ function rmmDeviceSave(array $provider, array $rmm_client, int $client_id, array
|
||||
}
|
||||
$facts = rmmDeviceFacts($device);
|
||||
$values = rmmDeviceAssetValues($device, $facts, $client_id);
|
||||
// The asset type the provider's Edit has this kind of device come in as
|
||||
$values['type'] = rmmProviderAssetTypes($provider)[$values['type']] ?? $values['type'];
|
||||
$row = rmmDeviceRow($provider_id, $remote_id);
|
||||
$synced_before = [];
|
||||
$asset_id = 0;
|
||||
@@ -1511,6 +1513,322 @@ function rmmClientRow($rmm_client_id): ?array
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------------ Asset types, linking by hand, contacts
|
||||
*/
|
||||
|
||||
/** Can a provider pick the asset types its devices come in as yet (database update 4.1.2)? */
|
||||
function rmmAssetTypesReady(): bool
|
||||
{
|
||||
global $mysqli;
|
||||
static $ready = null;
|
||||
if ($ready === null) {
|
||||
if (defined('CURRENT_DATABASE_VERSION')) {
|
||||
$version = CURRENT_DATABASE_VERSION;
|
||||
} else {
|
||||
$result = mysqli_query($mysqli, "SELECT config_current_database_version FROM settings WHERE company_id = 1");
|
||||
$version = $result && ($row = mysqli_fetch_row($result)) ? (string) $row[0] : '0';
|
||||
}
|
||||
$ready = version_compare($version, '4.1.2', '>=');
|
||||
}
|
||||
return $ready;
|
||||
}
|
||||
|
||||
/** The kinds of device the sync tells apart - the type it guesses (rmmDeviceType()) => what the provider window calls them. */
|
||||
function rmmDeviceKinds(): array
|
||||
{
|
||||
return ['Desktop' => 'Desktops', 'Laptop' => 'Laptops', 'Server' => 'Servers', 'Virtual Machine' => 'Virtual machines'];
|
||||
}
|
||||
|
||||
/** The asset type each kind of a provider's devices comes in as - [guessed type => type]. A type deleted since is the guessed one again. */
|
||||
function rmmProviderAssetTypes(?array $provider): array
|
||||
{
|
||||
$picked = $provider && rmmAssetTypesReady() ? json_decode((string) ($provider['rmm_provider_asset_types'] ?? ''), true) : null;
|
||||
$types = [];
|
||||
foreach (array_keys(rmmDeviceKinds()) as $guessed) {
|
||||
$type = is_array($picked) && is_string($picked[$guessed] ?? null) ? assetTypeName($picked[$guessed]) : '';
|
||||
$types[$guessed] = $type !== '' ? $type : $guessed;
|
||||
}
|
||||
return $types;
|
||||
}
|
||||
|
||||
/** One of your asset types renamed, or deleted with its assets moved to another - the providers bringing devices in as it follow. */
|
||||
function rmmProviderAssetTypesRename(string $old, string $new): void
|
||||
{
|
||||
global $mysqli;
|
||||
if (!rmmTablesReady() || !rmmAssetTypesReady() || $old === $new) {
|
||||
return;
|
||||
}
|
||||
$sql = mysqli_query($mysqli, "SELECT rmm_provider_id, rmm_provider_asset_types FROM rmm_providers WHERE rmm_provider_asset_types IS NOT NULL");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$picked = json_decode((string) $row['rmm_provider_asset_types'], true);
|
||||
if (!is_array($picked)) {
|
||||
continue;
|
||||
}
|
||||
$changed = false;
|
||||
foreach ($picked as $guessed => $type) {
|
||||
if (is_string($type) && strcasecmp($type, $old) === 0) {
|
||||
$picked[$guessed] = $new;
|
||||
$changed = true;
|
||||
}
|
||||
}
|
||||
if ($changed) {
|
||||
mysqli_query($mysqli, "UPDATE rmm_providers SET rmm_provider_asset_types = '" . escapeSql(json_encode($picked)) . "' WHERE rmm_provider_id = " . intval($row['rmm_provider_id']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** One device with its provider's columns and its RMM client's name - raw, or null. */
|
||||
function rmmDeviceById(int $device_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
if (!$device_id || !rmmTablesReady()) {
|
||||
return null;
|
||||
}
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT rmm_devices.*, rmm_providers.*, rmm_client_name FROM rmm_devices
|
||||
JOIN rmm_providers ON rmm_provider_id = rmm_device_provider_id
|
||||
LEFT JOIN rmm_clients ON rmm_clients.rmm_client_id = rmm_device_rmm_client_id
|
||||
WHERE rmm_device_id = $device_id LIMIT 1"));
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
/** The assets a device can be linked to by hand - its client's, not archived, but the one it has: [asset_id => row, with 'taken' - the hostname of another device of its provider that has it]. */
|
||||
function rmmDeviceLinkTargets(array $device): array
|
||||
{
|
||||
global $mysqli;
|
||||
$client_id = intval($device['rmm_device_client_id']);
|
||||
$provider_id = intval($device['rmm_device_provider_id']);
|
||||
$device_id = intval($device['rmm_device_id']);
|
||||
$targets = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT asset_id, asset_name, asset_type, asset_make, asset_model, asset_serial,
|
||||
(SELECT rmm_device_hostname FROM rmm_devices WHERE rmm_device_provider_id = $provider_id AND rmm_device_asset_id = asset_id
|
||||
AND rmm_device_removed_at IS NULL AND rmm_device_id <> $device_id LIMIT 1) AS taken
|
||||
FROM assets WHERE asset_client_id = $client_id AND asset_archived_at IS NULL AND asset_id <> " . intval($device['rmm_device_asset_id']) . "
|
||||
ORDER BY asset_name ASC");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$targets[intval($row['asset_id'])] = $row;
|
||||
}
|
||||
return $targets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Link a device to another of its client's assets, by hand - it was matched to the wrong one,
|
||||
* or brought in as a new one beside the asset you already had. The asset it had stays, no
|
||||
* longer kept up. The one picked fills in only what it is missing, as a matched asset does.
|
||||
* '' when done, or what is wrong.
|
||||
*/
|
||||
function rmmDeviceLinkAsset(array $device, int $asset_id): string
|
||||
{
|
||||
global $mysqli, $session_name;
|
||||
$device_id = intval($device['rmm_device_id']);
|
||||
$provider_id = intval($device['rmm_device_provider_id']);
|
||||
$client_id = intval($device['rmm_device_client_id']);
|
||||
$name = rmmProviderName($device);
|
||||
$host = (string) $device['rmm_device_hostname'];
|
||||
$had = intval($device['rmm_device_asset_id']);
|
||||
if ($device['rmm_device_removed_at'] !== null) {
|
||||
return "$name no longer has it.";
|
||||
}
|
||||
$target = rmmAssetCurrent($asset_id);
|
||||
if (!$target || $target['_client_id'] !== $client_id) {
|
||||
return 'Pick one of this client\'s assets.';
|
||||
}
|
||||
if ($asset_id === $had) {
|
||||
return 'It is linked to that asset already.';
|
||||
}
|
||||
$taken = rmmDeviceLinkTargets($device)[$asset_id]['taken'] ?? null;
|
||||
if ($taken !== null) {
|
||||
return "That asset is $taken in $name already.";
|
||||
}
|
||||
|
||||
// A device of this provider that had the asset picked and is gone there no longer holds it
|
||||
mysqli_query($mysqli, "DELETE FROM rmm_devices WHERE rmm_device_provider_id = $provider_id AND rmm_device_asset_id = $asset_id
|
||||
AND rmm_device_removed_at IS NOT NULL AND rmm_device_id <> $device_id");
|
||||
mysqli_query($mysqli, "UPDATE rmm_devices SET rmm_device_asset_id = $asset_id, rmm_device_synced = '[]' WHERE rmm_device_id = $device_id");
|
||||
|
||||
$who = stripslashes((string) $session_name);
|
||||
$had_row = $had ? rmmAssetCurrent($had) : null;
|
||||
if ($had_row) {
|
||||
rmmAssetHistory($had, $name, "no longer keeps it up - $who linked its device $host to " . $target['name']);
|
||||
}
|
||||
rmmAssetHistory($asset_id, $who, "linked it to its device in $name, $host" . ($had_row ? ', which was ' . $had_row['name'] : ''));
|
||||
logAudit('Asset', 'Edit', "$session_name linked " . escapeSql("$name device $host to asset {$target['name']}" . ($had_row ? " - it was {$had_row['name']}" : '')), $client_id, $asset_id);
|
||||
return '';
|
||||
}
|
||||
|
||||
/** A username as it is compared with contacts - DOMAIN\ and @domain taken off, then letters and digits only, in lower case. */
|
||||
function rmmUserKey($user): string
|
||||
{
|
||||
$user = preg_replace('/^.*\\\\/u', '', (string) $user);
|
||||
$user = preg_replace('/@.*$/u', '', $user);
|
||||
return preg_replace('/[^\p{L}\p{N}]+/u', '', mb_strtolower($user));
|
||||
}
|
||||
|
||||
/**
|
||||
* The contact a device's signed-in user is - the only one of the client's contacts whose email
|
||||
* starts with that username, or whose name it is: Marla Whitfield for mwhitfield, marlaw,
|
||||
* marla.whitfield or AzureAD\MarlaWhitfield. Null when none is, or when more than one could be.
|
||||
*/
|
||||
function rmmUserContact(int $client_id, string $logged_user): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
static $contacts = [];
|
||||
$key = rmmUserKey($logged_user);
|
||||
if (mb_strlen($key) < 3 || in_array($key, ['administrator', 'admin', 'localadmin', 'user', 'guest', 'owner', 'system', 'defaultuser0', 'kiosk'], true)) {
|
||||
return null;
|
||||
}
|
||||
if (!isset($contacts[$client_id])) {
|
||||
$contacts[$client_id] = [];
|
||||
$sql = mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_email FROM contacts WHERE contact_client_id = $client_id AND contact_archived_at IS NULL");
|
||||
while ($row = mysqli_fetch_assoc($sql)) {
|
||||
$keys = [];
|
||||
$email = (string) $row['contact_email'];
|
||||
if (str_contains($email, '@')) {
|
||||
$keys[] = rmmUserKey($email);
|
||||
}
|
||||
$words = preg_split('/\s+/u', trim((string) $row['contact_name']), -1, PREG_SPLIT_NO_EMPTY);
|
||||
if (count($words) >= 2) {
|
||||
$first = rmmUserKey($words[0]);
|
||||
$last = rmmUserKey(end($words));
|
||||
$keys[] = rmmUserKey(implode('', $words));
|
||||
$keys[] = mb_substr($first, 0, 1) . $last;
|
||||
$keys[] = $first . mb_substr($last, 0, 1);
|
||||
}
|
||||
$row['keys'] = array_values(array_unique(array_filter($keys, function ($k) {
|
||||
return mb_strlen($k) >= 3;
|
||||
})));
|
||||
$contacts[$client_id][] = $row;
|
||||
}
|
||||
}
|
||||
$found = [];
|
||||
foreach ($contacts[$client_id] as $row) {
|
||||
if (in_array($key, $row['keys'], true)) {
|
||||
$found[intval($row['contact_id'])] = $row;
|
||||
}
|
||||
}
|
||||
return count($found) === 1 ? reset($found) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Some assets' contacts from who is signed in to their devices - only assets with nobody
|
||||
* assigned. ['assigned', 'had', 'none'] counts. The caller checks the agent may edit them.
|
||||
*/
|
||||
function rmmAssignUserContacts(array $asset_ids): array
|
||||
{
|
||||
global $mysqli;
|
||||
$counts = ['assigned' => 0, 'had' => 0, 'none' => 0];
|
||||
$asset_ids = array_values(array_filter(array_map('intval', $asset_ids)));
|
||||
$devices = rmmDevicesForAssets($asset_ids);
|
||||
foreach ($asset_ids as $asset_id) {
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT asset_client_id, asset_contact_id FROM assets WHERE asset_id = $asset_id"));
|
||||
if (!$row) {
|
||||
continue;
|
||||
}
|
||||
if (intval($row['asset_contact_id'])) {
|
||||
$counts['had']++;
|
||||
continue;
|
||||
}
|
||||
$contact = null;
|
||||
foreach ($devices[$asset_id] ?? [] as $device) {
|
||||
if ($device['rmm_device_removed_at'] === null && (string) $device['rmm_device_logged_user'] !== '') {
|
||||
$contact = rmmUserContact(intval($row['asset_client_id']), (string) $device['rmm_device_logged_user']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$contact) {
|
||||
$counts['none']++;
|
||||
continue;
|
||||
}
|
||||
[$ok] = assetQuickSet($asset_id, 'asset_contact', (string) intval($contact['contact_id']));
|
||||
$counts[$ok ? 'assigned' : 'none']++;
|
||||
}
|
||||
return $counts;
|
||||
}
|
||||
|
||||
/** Assets deleted - their devices keep no asset, so Bring back on their client's row has them at once. How many were in an RMM. */
|
||||
function rmmAssetsDeleted(array $asset_ids): int
|
||||
{
|
||||
global $mysqli;
|
||||
$asset_ids = array_values(array_filter(array_map('intval', $asset_ids)));
|
||||
if (!$asset_ids || !rmmTablesReady()) {
|
||||
return 0;
|
||||
}
|
||||
$in = implode(', ', $asset_ids);
|
||||
$live = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(DISTINCT rmm_device_asset_id) FROM rmm_devices WHERE rmm_device_asset_id IN ($in) AND rmm_device_removed_at IS NULL"))[0]);
|
||||
mysqli_query($mysqli, "UPDATE rmm_devices SET rmm_device_asset_id = 0 WHERE rmm_device_asset_id IN ($in)");
|
||||
return $live;
|
||||
}
|
||||
|
||||
/**
|
||||
* How a client's devices in its RMMs are - how many of its assets are in one, and how many are
|
||||
* online, offline, overdue, need a reboot, have patches waiting or checks failing, with when it
|
||||
* was last brought in. Null when none of its assets are in an RMM.
|
||||
*/
|
||||
function rmmClientSummary(int $client_id): ?array
|
||||
{
|
||||
global $mysqli;
|
||||
static $summaries = [];
|
||||
if (array_key_exists($client_id, $summaries)) {
|
||||
return $summaries[$client_id];
|
||||
}
|
||||
if (!rmmTablesReady() || !rmmProviders()) {
|
||||
return $summaries[$client_id] = null;
|
||||
}
|
||||
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT COUNT(DISTINCT asset_id) AS total,
|
||||
COUNT(DISTINCT IF(rmm_device_status = 'online', asset_id, NULL)) AS online,
|
||||
COUNT(DISTINCT IF(rmm_device_status = 'offline', asset_id, NULL)) AS offline,
|
||||
COUNT(DISTINCT IF(rmm_device_status = 'overdue', asset_id, NULL)) AS overdue,
|
||||
COUNT(DISTINCT IF(rmm_device_needs_reboot = 1, asset_id, NULL)) AS reboot,
|
||||
COUNT(DISTINCT IF(rmm_device_patches_pending = 1, asset_id, NULL)) AS patches,
|
||||
COUNT(DISTINCT IF(rmm_device_checks_failing > 0, asset_id, NULL)) AS checks,
|
||||
MAX(rmm_device_synced_at) AS synced_at
|
||||
FROM rmm_devices JOIN assets ON asset_id = rmm_device_asset_id AND asset_client_id = $client_id AND asset_archived_at IS NULL
|
||||
WHERE rmm_device_asset_id > 0 AND rmm_device_removed_at IS NULL"));
|
||||
if (!$row || !intval($row['total'])) {
|
||||
return $summaries[$client_id] = null;
|
||||
}
|
||||
$summary = ['synced_at' => $row['synced_at']];
|
||||
foreach (['total', 'online', 'offline', 'overdue', 'reboot', 'patches', 'checks'] as $key) {
|
||||
$summary[$key] = intval($row[$key]);
|
||||
}
|
||||
return $summaries[$client_id] = $summary;
|
||||
}
|
||||
|
||||
/** Each asset's devices in its RMMs, for the API - as asset_rmm, [] for an asset in none. */
|
||||
function rmmApiAttach(array $rows): array
|
||||
{
|
||||
$devices = rmmTablesReady() ? rmmDevicesForAssets(array_map(function ($row) {
|
||||
return intval($row['asset_id'] ?? 0);
|
||||
}, $rows)) : [];
|
||||
foreach ($rows as &$row) {
|
||||
$row['asset_rmm'] = array_map(function ($device) {
|
||||
return [
|
||||
'provider_id' => intval($device['rmm_device_provider_id']),
|
||||
'provider' => rmmProviderName($device),
|
||||
'device_id' => (string) $device['rmm_device_remote_id'],
|
||||
'hostname' => (string) $device['rmm_device_hostname'],
|
||||
'site' => (string) $device['rmm_device_site'],
|
||||
'status' => $device['rmm_device_removed_at'] !== null ? 'removed' : (string) $device['rmm_device_status'],
|
||||
'last_seen_at' => $device['rmm_device_last_seen_at'],
|
||||
'boot_at' => $device['rmm_device_boot_at'],
|
||||
'logged_user' => (string) $device['rmm_device_logged_user'],
|
||||
'public_ip' => (string) $device['rmm_device_public_ip'],
|
||||
'local_ips' => (string) $device['rmm_device_local_ips'],
|
||||
'needs_reboot' => (bool) intval($device['rmm_device_needs_reboot']),
|
||||
'patches_pending' => (bool) intval($device['rmm_device_patches_pending']),
|
||||
'checks_total' => intval($device['rmm_device_checks_total']),
|
||||
'checks_failing' => intval($device['rmm_device_checks_failing']),
|
||||
'maintenance' => (bool) intval($device['rmm_device_maintenance']),
|
||||
'agent_version' => (string) $device['rmm_device_agent_version'],
|
||||
'synced_at' => $device['rmm_device_synced_at'],
|
||||
'removed_at' => $device['rmm_device_removed_at'],
|
||||
];
|
||||
}, $devices[intval($row['asset_id'] ?? 0)] ?? []);
|
||||
}
|
||||
unset($row);
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/*
|
||||
* ------------------------------------------------------------------ The provider window
|
||||
*/
|
||||
@@ -1585,6 +1903,24 @@ function rmmProviderCheck(array $in, ?array $before): array
|
||||
$after['rmm_provider_alerts_restart'] = $level !== '' && ($level !== (string) ($before['rmm_provider_alert_level'] ?? '')
|
||||
|| ($before && !intval($before['rmm_provider_active']) && $after['rmm_provider_active']));
|
||||
}
|
||||
|
||||
// The asset type each kind of device comes in as - kept as they were for a kind the window didn't send
|
||||
if (rmmAssetTypesReady()) {
|
||||
$now = rmmProviderAssetTypes($before);
|
||||
$sent = is_array($in['asset_types'] ?? null) ? $in['asset_types'] : [];
|
||||
$picked = [];
|
||||
foreach (array_keys(rmmDeviceKinds()) as $guessed) {
|
||||
$type = isset($sent[$guessed]) && is_string($sent[$guessed]) ? assetTypeName($sent[$guessed]) : $now[$guessed];
|
||||
if ($type === '') {
|
||||
$errors['asset_types'] = 'Pick an asset type for each kind of device.';
|
||||
$type = $now[$guessed];
|
||||
}
|
||||
if ($type !== $guessed) {
|
||||
$picked[$guessed] = $type;
|
||||
}
|
||||
}
|
||||
$after['rmm_provider_asset_types'] = $picked ? json_encode($picked) : null;
|
||||
}
|
||||
return [$after, $errors];
|
||||
}
|
||||
|
||||
@@ -1610,6 +1946,9 @@ function rmmProviderWrite(array $after, int $provider_id = 0): int
|
||||
$set[] = 'rmm_provider_alerts_since = NULL, rmm_provider_alerts_note = NULL';
|
||||
}
|
||||
}
|
||||
if (array_key_exists('rmm_provider_asset_types', $after)) {
|
||||
$set[] = 'rmm_provider_asset_types = ' . ($after['rmm_provider_asset_types'] === null ? 'NULL' : "'" . escapeSql($after['rmm_provider_asset_types']) . "'");
|
||||
}
|
||||
if ($provider_id) {
|
||||
mysqli_query($mysqli, "UPDATE rmm_providers SET " . implode(', ', $set) . " WHERE rmm_provider_id = $provider_id");
|
||||
return $provider_id;
|
||||
@@ -1649,5 +1988,14 @@ function rmmProviderChangeLines(array $before, array $after): array
|
||||
$category = intval($after['rmm_provider_alert_category_id']) ? (string) getFieldById('categories', intval($after['rmm_provider_alert_category_id']), 'category_name') : '';
|
||||
$lines[] = $category === '' ? 'alert tickets not categorized' : "alert tickets in $category";
|
||||
}
|
||||
if (array_key_exists('rmm_provider_asset_types', $after)) {
|
||||
$had = rmmProviderAssetTypes($before);
|
||||
$now = rmmProviderAssetTypes(['rmm_provider_asset_types' => $after['rmm_provider_asset_types']]);
|
||||
foreach (rmmDeviceKinds() as $guessed => $label) {
|
||||
if ($had[$guessed] !== $now[$guessed]) {
|
||||
$lines[] = mb_strtolower($label) . ' come in as ' . $now[$guessed];
|
||||
}
|
||||
}
|
||||
}
|
||||
return $lines;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user