Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion apps/admin_audit/lib/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ public function log(
}

$replaceArray = [];
$context = ['app' => 'admin_audit'];
foreach ($elements as $element) {
$value = $params[$element];
if ($value instanceof \DateTimeInterface) {
$value = $value->format('Y-m-d H:i:s');
}
$replaceArray[] = $value;
// Named {placeholders} are interpolated by the logger
if (str_contains($text, '{' . $element . '}')) {
$context[$element] = $value;
}
}

$this->logger->info(
vsprintf($text, $replaceArray),
['app' => 'admin_audit'],
$context,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AdminAudit\Tests\Listener;

use OCA\AdminAudit\AuditLogger;
use OCA\AdminAudit\Listener\CriticalActionPerformedEventListener;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class CriticalActionPerformedEventListenerTest extends TestCase {
private AuditLogger&MockObject $logger;
private CriticalActionPerformedEventListener $listener;

protected function setUp(): void {
parent::setUp();

$this->logger = $this->createMock(AuditLogger::class);
$this->listener = new CriticalActionPerformedEventListener($this->logger);
}

public function testSprintfPlaceholders(): void {
$this->logger->expects($this->once())
->method('info')
->with('Share "42" was accepted by "alice"', ['app' => 'admin_audit']);

$this->listener->handle(new CriticalActionPerformedEvent('Share "%s" was accepted by "%s"', ['42', 'alice']));
}

public function testNamedPlaceholders(): void {
$this->logger->expects($this->once())
->method('info')
->with(
'Bruteforce attempt from "{ip}" detected for action "{action}" not throttled due to allow-list.',
['app' => 'admin_audit', 'ip' => '10.0.0.1', 'action' => 'login'],
);

$this->listener->handle(new CriticalActionPerformedEvent(
'Bruteforce attempt from "{ip}" detected for action "{action}" not throttled due to allow-list.',
['ip' => '10.0.0.1', 'action' => 'login'],
));
}
}
Loading