From eb409ac8a63ed4448db1b48a9a5049a043f1f004 Mon Sep 17 00:00:00 2001 From: Mark Unwin Date: Thu, 18 Jul 2024 12:37:14 +1000 Subject: [PATCH] Add a Device Reset button on devicesRead to remove all non-current components and change log entries. --- app/Config/Routes.php | 2 ++ app/Controllers/Devices.php | 17 +++++++++++++++++ app/Views/devicesRead.php | 29 +++++++++++++++++++++++++++++ app/Views/lang/en.inc | 2 ++ app/Views/shared/read_functions.php | 12 ++++++++++++ 5 files changed, 62 insertions(+) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 58b9b3421..4fd72fab5 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -130,6 +130,8 @@ $routes->cli('clouds/(:num)/execute', 'Cli::executeCloud/$1', ['as' => 'executeCloud']); $routes->cli('devices/(:num)/cloudDevice', 'Cli::cloudDevice/$1', ['as' => 'cloudDevice']); +$routes->patch('devices/(:num)/reset', 'Devices::reset/$1', ['filter' => \App\Filters\Session::class, 'as' => 'DeviceReset']); + foreach ($collections as $collection) { // Account for users editing the config and including a space character $collection = trim((string)$collection); diff --git a/app/Controllers/Devices.php b/app/Controllers/Devices.php index 8fd61e5c0..6a8b43bcd 100644 --- a/app/Controllers/Devices.php +++ b/app/Controllers/Devices.php @@ -468,4 +468,21 @@ public function importNMIS() \Config\Services::session()->setFlashdata('success', $this->resp->meta->total . ' devices imported (' . $this->resp->meta->inserted . ' inserted and ' . $this->resp->meta->updated . ' updated).'); return redirect()->to(site_url() . '/devices?devices.last_seen=' . $this->config->timestamp . '&devices.last_seen_by=nmis&properties=devices.id,devices.icon,devices.type,devices.name,nmis_name,devices.ip,devices.nmis_business_service,devices.nmis_group,devices.nmis_role,devices.nmis_notes'); } + + public function reset($id = null) + { + $id = (int)$id; + $db = db_connect(); + $tables = array('bios', 'certificate', 'disk', 'dns', 'executable', 'file', 'ip', 'log', 'memory', 'module', 'monitor', 'motherboard', 'netstat', 'network', 'nmap', 'optical', 'pagefile', 'partition', 'policy', 'print_queue', 'processor', 'radio', 'route', 'san', 'scsi', 'server', 'server_item', 'service', 'share', 'software', 'software_key', 'sound', 'task', 'usb', 'user', 'user_group', 'variable', 'video', 'vm', 'warranty', 'windows'); + foreach ($tables as $table) { + $sql = "DELETE FROM `$table` WHERE device_id = ? and current = 'n'"; + $db->query($sql, [$id]); + log_message('debug', str_replace("\n", " ", (string)$db->getLastQuery())); + $sql = "DELETE FROM `change_log` WHERE device_id = ? and db_table = ?"; + $db->query($sql, [$id, $table]); + log_message('debug', str_replace("\n", " ", (string)$db->getLastQuery())); + } + echo json_encode($this->resp); + return; + } } diff --git a/app/Views/devicesRead.php b/app/Views/devicesRead.php index 3d87dd588..b1968ebf6 100644 --- a/app/Views/devicesRead.php +++ b/app/Views/devicesRead.php @@ -2581,6 +2581,35 @@ $("#tags_control").css('display', 'block'); }); + $('#button_reset').click(function (){ + if (confirm("")) { + $.ajax({ + type: "PATCH", + url: "devices/id ?>/reset", + contentType: "application/json", + data: {}, + success: function (data) { + $("#liveToastSuccess-header").text("Reset Succeeded"); + $("#liveToastSuccess-body").text("Reload to view updated device."); + var toastElList = [].slice.call(document.querySelectorAll('.toast-success')); + var toastList = toastElList.map(function(toastEl) { + return new bootstrap.Toast(toastEl) + }); + toastList.forEach(toast => toast.show()); + }, + error: function (data) { + // data = JSON.parse(data.responseText); + $("#liveToastFailure-header").text("Update Failed"); + $("#liveToastFailure-body").text("Please check the logfile."); + var toastElList = [].slice.call(document.querySelectorAll('.toast-failure')); + var toastList = toastElList.map(function(toastEl) { + return new bootstrap.Toast(toastEl) + }); + toastList.forEach(toast => toast.show()); + } + }); + } + }) } diff --git a/app/Views/lang/en.inc b/app/Views/lang/en.inc index 122868cf0..8b64b97b5 100644 --- a/app/Views/lang/en.inc +++ b/app/Views/lang/en.inc @@ -42,6 +42,7 @@ $GLOBALS["lang"]["Access Control"]="Access Control"; $GLOBALS["lang"]["Access Point"]="Access Point"; $GLOBALS["lang"]["Access Server"]="Access Server"; $GLOBALS["lang"]["Access Token"]="Access Token"; +$GLOBALS["lang"]["Ack By"]="Ack By"; $GLOBALS["lang"]["Ack Time"]="Ack Time"; $GLOBALS["lang"]["Acrobat"]="Acrobat"; $GLOBALS["lang"]["Action"]="Action"; @@ -1492,6 +1493,7 @@ $GLOBALS["lang"]["Shares"]="Shares"; $GLOBALS["lang"]["Shelf"]="Shelf"; $GLOBALS["lang"]["Shell"]="Shell"; $GLOBALS["lang"]["Ship Date"]="Ship Date"; +$GLOBALS["lang"]["Should I remove all non-current data from this device?"]="Should I remove all non-current data from this device?"; $GLOBALS["lang"]["Show All"]="Show All"; $GLOBALS["lang"]["SID"]="SID"; $GLOBALS["lang"]["Sid"]="Sid"; diff --git a/app/Views/shared/read_functions.php b/app/Views/shared/read_functions.php index 8b1599574..cf4eb9176 100644 --- a/app/Views/shared/read_functions.php +++ b/app/Views/shared/read_functions.php @@ -40,6 +40,17 @@ function read_card_header(string $collection = '', string $id = '', string $icon } } + $device_reset_button = "\n"; + if ($collection === 'devices' and !empty($id) and strpos($user->permissions[$collection], 'u') !== false) { + if ($style === 'icontext') { + $device_reset_button = ""; + } elseif ($style === 'icon') { + $device_reset_button = ""; + } else { + $device_reset_button = ""; + } + } + $export_csv_button = ''; if ($collection === 'queries' and !empty($id)) { if ($style === 'icontext') { @@ -115,6 +126,7 @@ function read_card_header(string $collection = '', string $id = '', string $icon $collection_button $download_button $create_button + $device_reset_button $delete_button $export_button $export_csv_button