Skip to content

Commit

Permalink
Add a Device Reset button on devicesRead to remove all non-current co…
Browse files Browse the repository at this point in the history
…mponents and change log entries.
  • Loading branch information
mark-unwin committed Jul 18, 2024
1 parent 475d11b commit eb409ac
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions app/Controllers/Devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
29 changes: 29 additions & 0 deletions app/Views/devicesRead.php
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,35 @@
$("#tags_control").css('display', 'block');
});

$('#button_reset').click(function (){
if (confirm("<?= __('Should I remove all non-current data from this device?') ?>")) {
$.ajax({
type: "PATCH",
url: "<?= base_url() ?>devices/<?= $resource->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());
}
});
}
})

}
</script>
2 changes: 2 additions & 0 deletions app/Views/lang/en.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
12 changes: 12 additions & 0 deletions app/Views/shared/read_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<button id=\"button_reset\" type=\"button\" class=\"btn btn-light mb-2\" title=\"" . __("Reset") . "\"><span style=\"margin-right:6px;\" class=\"fa-solid fa-rotate-right text-warning\"></span>" . __("Reset") . "</button>";
} elseif ($style === 'icon') {
$device_reset_button = "<button id=\"button_reset\" type=\"button\" class=\"btn btn-light mb-2\" title=\"" . __("Reset") . "\"><span class=\"fa-solid fa-rotate-right text-warning\"></span></button>";
} else {
$device_reset_button = "<button id=\"button_reset\" type=\"button\" class=\"btn btn-light mb-2\" title=\"" . __("Reset") . "\">" . __("Reset") . "</button>";
}
}

$export_csv_button = '';
if ($collection === 'queries' and !empty($id)) {
if ($style === 'icontext') {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eb409ac

Please sign in to comment.