From 8d8b0f4a48f4aec85e0b5114e96d83bf25d1605e Mon Sep 17 00:00:00 2001 From: johnnyq Date: Wed, 12 Feb 2025 15:27:31 -0500 Subject: [PATCH] Feature: Ability to create multiple interfaces/ports in one swoop with interface name and port prefix along with network assignment and notes --- client_asset_details.php | 23 +++- client_assets.php | 2 +- ...ent_asset_interface_multiple_add_modal.php | 102 ++++++++++++++++++ post/user/asset.php | 46 +++++++- 4 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 modals/client_asset_interface_multiple_add_modal.php diff --git a/client_asset_details.php b/client_asset_details.php index dcc23861..6d030666 100644 --- a/client_asset_details.php +++ b/client_asset_details.php @@ -365,9 +365,25 @@ if (isset($_GET['asset_id'])) {

Network Interfaces

- +
+ + + +
@@ -947,6 +963,7 @@ if (isset($_GET['asset_id'])) { 0) {

Assets

= 2) { ?> -
+
diff --git a/modals/client_asset_interface_multiple_add_modal.php b/modals/client_asset_interface_multiple_add_modal.php new file mode 100644 index 00000000..2d87ea30 --- /dev/null +++ b/modals/client_asset_interface_multiple_add_modal.php @@ -0,0 +1,102 @@ + diff --git a/post/user/asset.php b/post/user/asset.php index e5080ddb..9cd2501f 100644 --- a/post/user/asset.php +++ b/post/user/asset.php @@ -39,7 +39,7 @@ if (isset($_POST['add_asset'])) { } // Add Primary Interface - mysqli_query($mysqli,"INSERT INTO asset_interfaces SET interface_name = 'Primary', interface_mac = '$mac', interface_ip = '$ip', interface_nat_ip = '$nat_ip', interface_ipv6 = '$ipv6', interface_port = 'eth0', interface_primary = 1, interface_network_id = $network, interface_asset_id = $asset_id"); + mysqli_query($mysqli,"INSERT INTO asset_interfaces SET interface_name = '1', interface_mac = '$mac', interface_ip = '$ip', interface_nat_ip = '$nat_ip', interface_ipv6 = '$ipv6', interface_port = '1', interface_primary = 1, interface_network_id = $network, interface_asset_id = $asset_id"); if (!empty($_POST['username'])) { @@ -89,7 +89,7 @@ if (isset($_POST['edit_asset'])) { if(mysqli_num_rows($sql_interfaces) == 0 ) { // Add Primary Interface - mysqli_query($mysqli,"INSERT INTO asset_interfaces SET interface_name = 'Primary', interface_mac = '$mac', interface_ip = '$ip', interface_nat_ip = '$nat_ip', interface_ipv6 = '$ipv6', interface_port = 'eth0', interface_primary = 1, interface_network_id = $network, interface_asset_id = $asset_id"); + mysqli_query($mysqli,"INSERT INTO asset_interfaces SET interface_name = '1', interface_mac = '$mac', interface_ip = '$ip', interface_nat_ip = '$nat_ip', interface_ipv6 = '$ipv6', interface_port = '1', interface_primary = 1, interface_network_id = $network, interface_asset_id = $asset_id"); } else { // Update Primary Interface mysqli_query($mysqli,"UPDATE asset_interfaces SET interface_mac = '$mac', interface_ip = '$ip', interface_nat_ip = '$nat_ip', interface_ipv6 = '$ipv6', interface_network_id = $network WHERE interface_asset_id = $asset_id AND interface_primary = 1"); @@ -769,6 +769,48 @@ if (isset($_POST['add_asset_interface'])) { exit; } +if (isset($_POST['add_asset_multiple_interfaces'])) { + + enforceUserPermission('module_support', 2); + validateCSRFToken($_POST['csrf_token']); + + $asset_id = intval($_POST['asset_id']); + $interface_start = intval($_POST['interface_start']); + $interfaces = intval($_POST['interfaces']); + $name_prefix = sanitizeInput($_POST['name_prefix']); + $port_prefix = sanitizeInput($_POST['port_prefix']); + $network = intval($_POST['network']); + $notes = sanitizeInput($_POST['notes']); + + $sql = mysqli_query($mysqli, "SELECT asset_name, asset_client_id FROM assets WHERE asset_id = $asset_id"); + $row = mysqli_fetch_array($sql); + $asset_name = sanitizeInput($row['asset_name']); + $client_id = intval($row['asset_client_id']); + + for ($interface_number = $interface_start; $interface_number < $interface_start + $interfaces; $interface_number++) { + + // Format $interface_number as a 2-digit number + $formatted_interface_number = str_pad($interface_number, 2, '0', STR_PAD_LEFT); + + $sql_insert = " + INSERT INTO asset_interfaces SET + interface_name = '$name_prefix$formatted_interface_number', + interface_port = '$port_prefix$formatted_interface_number', + interface_notes = '$notes', + interface_network_id = $network, + interface_asset_id = $asset_id + "; + mysqli_query($mysqli, $sql_insert); + + logAction("Asset Interface", "Create", "$session_name created interface $name for asset $asset_name", $client_id, $asset_id); + } + + logAction("Asset Interface", "Bulk Create", "$session_name created $interfaces for asset $asset_name", $client_id, $asset_id); + $_SESSION['alert_message'] = "Created $interfaces Interface(s) for asset $asset_name"; + header("Location: " . $_SERVER["HTTP_REFERER"]); + exit; +} + if (isset($_POST['edit_asset_interface'])) { enforceUserPermission('module_support', 2);