Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to display audit logs #157

Merged
merged 20 commits into from
Aug 22, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#128: Added functionality to sort audit logs by date as well as displ…
…ay newest log entries first. Added documentation for the reverse sort ordering feature.
  • Loading branch information
abpai94 committed Jul 30, 2024
commit 951cd86039cd191bf33d3b11d2087ed7ea9e5fc1
1 change: 1 addition & 0 deletions conf/config.inc.php
Original file line number Diff line number Diff line change
@@ -183,6 +183,7 @@
$audit_log_days = 5;
$audit_log_items = array('date','ip','dn','done_by','action','result','comment');
$audit_log_sortby = "date";
$audit_log_reverse = true;
$audit_log_linkto = array("dn");
$audit_log_truncate_value_after = 10;
#$header_name_audit_admin = "AUTH_USER";
20 changes: 15 additions & 5 deletions docs/audit.rst
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ Display Audit logs
==================

Enabling audit logs display
--------------------------
---------------------------

When the audit logs are enabled, they can be displayed in a table by setting the following variable:

@@ -94,25 +94,35 @@ The number of days that can be displayed in the table can be configured as follo
$audit_log_days = 5;

abpai94 marked this conversation as resolved.
Show resolved Hide resolved
Display table columns
------------------
---------------------

The table columns to be displayed can be configured with the following variable:

.. code-block:: php

$audit_log_items = array('date','ip','dn','done_by','action','result','comment');

Audit table sorting
------------------
Audit table sorting
-------------------

The table can be sorted by default by the setting:

.. code-block:: php

$audit_log_sortby = "date";

Audit table sorting order
-------------------------

Audit logs are usually display with the oldest first as they are being parsed from a file.
In order to have the newest audit log entries first the following configuration can reverse the order:

.. code-block:: php

$audit_log_reverse = true;

Audit user link
------------------
---------------

Users present in the audit table can be linked by setting:

4 changes: 1 addition & 3 deletions htdocs/auditlog.php
Original file line number Diff line number Diff line change
@@ -7,10 +7,8 @@
require_once("../lib/date.inc.php");
require_once("../lib/audit.inc.php");

#TODO: Order based on newest to oldest

$entries = array();
[$entries,$nb_entries] = displayauditlog($audit_log_file, $audit_log_days);
[$entries,$nb_entries] = displayauditlog($audit_log_file, $audit_log_days, $audit_log_sortby, $audit_log_reverse);

if (!empty($entries)) {
$smarty->assign("page_title", "auditlogtitle");
21 changes: 19 additions & 2 deletions lib/audit.inc.php
Original file line number Diff line number Diff line change
@@ -18,11 +18,11 @@ function auditlog($file, $dn, $admin, $action, $result, $comment) {
file_put_contents($file, json_encode($log, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL, FILE_APPEND | LOCK_EX);
}

function displayauditlog($audit_log_file, $audit_log_days) {
function displayauditlog($audit_log_file, $audit_log_days, $audit_log_sortby, $audit_log_reverse) {

$entries = array();

#Date calculation to limit oldest audit logs
# Date calculation to limit oldest audit logs
$olddatelog = new DateTime();
date_sub( $olddatelog, new DateInterval('P'.$audit_log_days.'D') );

@@ -35,9 +35,26 @@ function displayauditlog($audit_log_file, $audit_log_days) {
}
}

# Sort audit log with sort key and normal/reverse order
dateSort($entries, $audit_log_sortby, $audit_log_reverse);

$nb_entries = sizeof($entries);

return [$entries,$nb_entries];
}

function dateSort(array &$entries, $sortkey, $audit_log_reverse) {
$reverse_order = fn($a, $b) => strtotime($a[$sortkey]) < strtotime($b[$sortkey]);
$normal_order = fn($a, $b) => strtotime($a[$sortkey]) > strtotime($b[$sortkey]);

if ($audit_log_reverse) {
usort($entries, $reverse_order);
}
else {
usort($entries, $normal_order);
}

return true;
}

?>