Feature: Ability to create multiple interfaces/ports in one swoop with interface name and port prefix along with network assignment and notes

This commit is contained in:
johnnyq 2025-02-12 15:27:31 -05:00
parent a2e16f918c
commit 8d8b0f4a48
4 changed files with 167 additions and 6 deletions

View File

@ -365,9 +365,25 @@ if (isset($_GET['asset_id'])) {
<div class="card-header py-2">
<h3 class="card-title mt-2"><i class="fa fa-fw fa-ethernet mr-2"></i><?php echo $asset_name; ?> Network Interfaces</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addAssetInterfaceModal">
<i class="fas fa-plus mr-2"></i>New Interface
</button>
<div class="btn-group">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addAssetInterfaceModal">
<i class="fas fa-plus mr-2"></i>New Interface
</button>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown"></button>
<div class="dropdown-menu">
<a class="dropdown-item text-dark" href="#" data-toggle="modal" data-target="#addMultipleAssetInterfacesModal">
<i class="fa fa-fw fa-check-double mr-2"></i>Add Multiple
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-dark" href="#" data-toggle="modal" data-target="#importAssetInterfacesModal">
<i class="fa fa-fw fa-upload mr-2"></i>Import
</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item text-dark" href="#" data-toggle="modal" data-target="#exportAssetInterfacesModal">
<i class="fa fa-fw fa-download mr-2"></i>Export
</a>
</div>
</div>
</div>
</div>
<div class="card-body">
@ -947,6 +963,7 @@ if (isset($_GET['asset_id'])) {
<?php
require_once "modals/client_asset_interface_add_modal.php";
require_once "modals/client_asset_interface_multiple_add_modal.php";
require_once "modals/ticket_add_modal.php";
require_once "modals/recurring_ticket_add_modal.php";
require_once "modals/recurring_ticket_edit_modal.php";

View File

@ -122,7 +122,7 @@ if (mysqli_num_rows($os_sql) > 0) {
<h3 class="card-title mt-2"><i class="fa fa-fw fa-desktop mr-2"></i>Assets</h3>
<div class="card-tools">
<?php if (lookupUserPermission("module_support") >= 2) { ?>
<div class="btn-group">
<div class="btn-group">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addAssetModal">
<i class="fas fa-plus mr-2"></i>New <?php if (!empty($_GET['type'])) { echo ucwords(strip_tags(nullable_htmlentities($_GET['type']))); } else { echo "Asset"; } ?>
</button>

View File

@ -0,0 +1,102 @@
<div class="modal" id="addMultipleAssetInterfacesModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content bg-dark">
<div class="modal-header">
<h5 class="modal-title"><i class="fa fa-fw fa-ethernet mr-2"></i>Creating Multiple Interfaces: <strong><?php echo $asset_name; ?></strong></h5>
<button type="button" class="close text-white" data-dismiss="modal">
<span>&times;</span>
</button>
</div>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="asset_id" value="<?php echo $asset_id; ?>">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token'] ?>">
<div class="modal-body bg-white">
<!-- Starting Interface Number -->
<div class="form-group">
<label for="interface_start">Starting Interface Number</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="number" id="interface_start" class="form-control" name="interface_start" placeholder="e.g., 1" min="1" required>
</div>
</div>
<!-- Number of Interfaces -->
<div class="form-group">
<label for="interfaces">Number of Interfaces</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-ethernet"></i></span>
</div>
<input type="number" id="interfaces" class="form-control" name="interfaces" placeholder="How many interfaces to create?" min="1" required>
</div>
</div>
<!-- Interface Name -->
<div class="form-group">
<label for="name_prefix">Interface Name Prefix (Optional)</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-tag"></i></span>
</div>
<input type="text" id="name_prefix" class="form-control" name="name_prefix" placeholder="e.g., eth-" maxlength="200">
</div>
</div>
<!-- Port Prefix -->
<div class="form-group">
<label for="port_prefix">Port Prefix (Optional)</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-plug"></i></span>
</div>
<input type="text" id="port_prefix" class="form-control" name="port_prefix" placeholder="e.g., fe" maxlength="200">
</div>
</div>
<!-- Network -->
<div class="form-group">
<label for="network">Network Assignment</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-fw fa-network-wired"></i></span>
</div>
<select id="network" class="form-control select2" name="network">
<option value="">- Select Network -</option>
<?php
$sql_network_select = mysqli_query($mysqli, "SELECT network_id, network_name, network FROM networks WHERE network_archived_at IS NULL AND network_client_id = $client_id ORDER BY network_name ASC");
while ($row = mysqli_fetch_array($sql_network_select)) {
$network_id = $row['network_id'];
$network_name = nullable_htmlentities($row['network_name']);
$network = nullable_htmlentities($row['network']);
?>
<option value="<?php echo $network_id; ?>">
<?php echo "$network_name - $network"; ?>
</option>
<?php } ?>
</select>
</div>
<small class="form-text text-muted">Choose the network for these interfaces or leave as None.</small>
</div>
<!-- Notes -->
<div class="form-group">
<label for="notes">Additional Notes</label>
<textarea id="notes" class="form-control" rows="5" placeholder="Enter any additional details or notes" name="notes"></textarea>
</div>
</div>
<div class="modal-footer bg-white">
<button type="submit" name="add_asset_multiple_interfaces" class="btn btn-primary text-bold">
<i class="fas fa-check mr-2"></i>Create Interfaces
</button>
<button type="button" class="btn btn-light" data-dismiss="modal"><i class="fas fa-times mr-2"></i>Cancel</button>
</div>
</form>
</div>
</div>
</div>

View File

@ -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 <strong>$interfaces</strong> Interface(s) for asset <strong>$asset_name</strong>";
header("Location: " . $_SERVER["HTTP_REFERER"]);
exit;
}
if (isset($_POST['edit_asset_interface'])) {
enforceUserPermission('module_support', 2);