Refactor AuditLogManager to support multiple audit log types and improve timestamp handling

This commit is contained in:
netkas 2025-06-03 14:22:10 -04:00
parent d5a0603b69
commit b40dd60309
Signed by: netkas
GPG key ID: 4D8629441B76E4CC

View file

@ -2,6 +2,7 @@
namespace FederationServer\Classes\Managers; namespace FederationServer\Classes\Managers;
use DateTime;
use FederationServer\Classes\DatabaseConnection; use FederationServer\Classes\DatabaseConnection;
use FederationServer\Classes\Enums\AuditLogType; use FederationServer\Classes\Enums\AuditLogType;
use FederationServer\Classes\Logger; use FederationServer\Classes\Logger;
@ -158,9 +159,12 @@
} }
/** /**
* Retrieves the total count of audit log entries. * Retrieves audit log entries by entity.
* *
* @return int The total number of audit log entries. * @param string $entity The UUID of the entity to filter by.
* @param int $limit The maximum number of entries to retrieve.
* @param int $page The page number for pagination.
* @return AuditLogRecord[] An array of AuditLogRecord objects representing the entries.
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
*/ */
public static function getEntriesByEntity(string $entity, int $limit=100, int $page=1): array public static function getEntriesByEntity(string $entity, int $limit=100, int $page=1): array
@ -203,13 +207,13 @@
/** /**
* Retrieves audit log entries by type. * Retrieves audit log entries by type.
* *
* @param AuditLogType $type The type of audit log entries to retrieve. * @param AuditLogType[] $type The type of audit log entries to retrieve.
* @param int $limit The maximum number of entries to retrieve. * @param int $limit The maximum number of entries to retrieve.
* @param int $page The page number for pagination. * @param int $page The page number for pagination.
* @return AuditLogRecord[] An array of AuditLogRecord objects representing the entries. * @return AuditLogRecord[] An array of AuditLogRecord objects representing the entries.
* @throws DatabaseOperationException If there is an error preparing or executing the SQL statement. * @throws DatabaseOperationException If there is an error preparing or executing the SQL statement.
*/ */
public static function getEntriesByType(AuditLogType $type, int $limit=100, int $page=1): array public static function getEntriesByType(array $type, int $limit=100, int $page=1): array
{ {
if($limit <= 0 || $page <= 0) if($limit <= 0 || $page <= 0)
{ {
@ -220,9 +224,16 @@
try try
{ {
$stmt = DatabaseConnection::getConnection()->prepare("SELECT * FROM audit_log WHERE type = :type ORDER BY timestamp DESC LIMIT :limit OFFSET :offset"); $placeholders = rtrim(str_repeat('?,', count($type)), ',');
$type = $type->value; $sql = "SELECT * FROM audit_log WHERE type IN ($placeholders) ORDER BY timestamp DESC LIMIT :limit OFFSET :offset";
$stmt->bindParam(':type', $type); $stmt = DatabaseConnection::getConnection()->prepare($sql);
// Bind the type parameters
foreach ($type as $index => $t)
{
$stmt->bindValue($index + 1, $t->value);
}
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT); $stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT); $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute(); $stmt->execute();
@ -255,11 +266,12 @@
} }
$timestamp = time() - ($olderThanDays * 86400); // Convert days to seconds $timestamp = time() - ($olderThanDays * 86400); // Convert days to seconds
$timestamp = DateTime::createFromFormat('U', $timestamp)->getTimestamp();
try try
{ {
$stmt = DatabaseConnection::getConnection()->prepare("DELETE FROM audit_log WHERE timestamp < :timestamp"); $stmt = DatabaseConnection::getConnection()->prepare("DELETE FROM audit_log WHERE timestamp < :timestamp");
$stmt->bindParam(':timestamp', $timestamp, PDO::PARAM_INT); $stmt->bindParam(':timestamp', $timestamp);
$stmt->execute(); $stmt->execute();
} }
catch (PDOException $e) catch (PDOException $e)