Files
Joas Schilling d6fc01c4e4 fix(adminaudit): Allow named placeholders via CriticalActionPerformedEvent
Assisted-by: ClaudeCode:claude-opus-5.5
Signed-off-by: Joas Schilling <coding@schilljs.com>
2026-09-29 09:01:07 +00:00

69 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AdminAudit\Actions;
use OCA\AdminAudit\IAuditLogger;
class Action {
public function __construct(
private IAuditLogger $logger,
) {
}
/**
* Log a single action with a log level of info
*
* @param string $text
* @param array<string, scalar|null|\DateTimeInterface> $params
* @param list<string> $elements
* @param bool $obfuscateParameters
*/
public function log(
string $text,
array $params,
array $elements,
bool $obfuscateParameters = false,
): void {
foreach ($elements as $element) {
if (!array_key_exists($element, $params)) {
$message = '$params["' . $element . '"] was missing.';
$context = ['app' => 'admin_audit'];
if (!$obfuscateParameters) {
$message .= ' Transferred value: {params}';
$context['params'] = $params;
}
$this->logger->critical($message, $context);
return;
}
}
$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),
$context,
);
}
}