Update ipsec.widget.php - Count user in "Overview" Tab and improve "Mobile Users" Tab (#6912)

* Update ipsec.widget.php

This change solves the problem of users having multiple IP addresses as leases and being counted per leased IP.
- Only "user" are counted now
- Each "user" can have several "lease"
- Each "lease" can have an individual online or offline status
- A user is online when at least one "lease" is "online = true"

* Update ipsec.widget.php

- Replaced multiple "array()" with "[]"
- Access keys "user", "address" and "online" directly from the $lease array without storping them first
- Merged two seperate foreach loops into one
This commit is contained in:
Cedrik Pischem 2023-10-05 11:05:45 +02:00 committed by GitHub
parent 2bac4f10a5
commit e5e8d003bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,6 +68,29 @@ foreach ($ipsec_status as $status_key => $status_value) {
}
}
}
// Initialize variable aggregated_data and loop through the ipsec_leases array to fetch the data for user, address and online status. Used later for div ipsec-mobile. Additionally count the unique_users in the same foreach loop, used for mobile_users count.
$aggregated_data = [];
$unique_users = [];
foreach ($ipsec_leases as $lease) {
// For each unique user, initialize an empty array
if (!isset($aggregated_data[$lease['user']])) {
$aggregated_data[$lease['user']] = [];
}
// Add the lease data to this user's array of leases
$aggregated_data[$lease['user']][] = [
'address' => $lease['address'],
'online' => $lease['online']
];
// Count unique users in ipsec_leases array if lease is online
if ($lease['online']) {
$unique_users[$lease['user']] = true;
}
}
// Return the number of unique_users as mobile_users
$mobile_users = count($unique_users);
?>
<script>
@ -133,16 +156,8 @@ foreach ($ipsec_status as $status_key => $status_value) {
</td>
<td><?= (count($ipsec_tunnels) - $activetunnels); ?></td>
<td>
<?php
// count active mobile users
$mobile_users = 0;
foreach ($ipsec_leases as $lease) {
if ($lease['online']) {
++$mobile_users;
}
}
?>
<?=$mobile_users;?>
<!-- mobile_users were counted in the earlier loop where data was aggregated -->
<?=$mobile_users;?>
</td>
</tr>
</tbody>
@ -176,28 +191,45 @@ foreach ($ipsec_status as $status_key => $status_value) {
</tbody>
</table>
</div>
<div id="ipsec-mobile" class="ipsec-tab-content" style="display:none;">
<table class="table table-striped">
<thead>
<tr>
<th><?= gettext('User');?></th>
<th><?= gettext('IP');?></th>
<th><?= gettext('Status');?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($ipsec_leases as $lease):?>
<tr>
<td><?=htmlspecialchars($lease['user']);?></td>
<td><?=htmlspecialchars($lease['address']);?></td>
<td>
<i class="fa fa-exchange fa-fw text-<?= $lease['online'] ? "success" : 'danger' ?>"></i>
</td>
</tr>
<?php
endforeach;?>
</tbody>
</table>
<div id="ipsec-mobile" class="ipsec-tab-content" style="display:none;">
<table class="table table-striped">
<thead>
<tr>
<th><?=gettext('User');?></th>
<th><?=gettext('IP');?></th>
<th><?=gettext('Status');?></th>
</tr>
</thead>
<tbody>
<?php
// Generate the user and IP addresses table rows using the aggregated_data variable that was populated earlier
foreach ($aggregated_data as $user => $user_data):?>
<tr>
<td><?=htmlspecialchars($user);?></td>
<td>
<table>
<?php foreach($user_data as $lease): ?>
<tr>
<td><?=htmlspecialchars($lease['address']);?></td>
</tr>
<?php endforeach; ?>
</table>
</td>
<td>
<table>
<?php foreach($user_data as $lease):?>
<tr>
<td>
<!-- Show the online and offline status of each lease a user has. -->
<i class="fa fa-exchange fa-fw text-<?=$lease['online'] ? 'success' : 'danger' ?>"></i>
</td>
</tr>
<?php endforeach; ?>
</table>
</td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>