Skip to content

Commit 231ec67

Browse files
authored
Merge pull request nextcloud#63305 from nextcloud/jtr/fix-auditLog-files-null
fix(admin_audit): handle audit reads for new files
2 parents c3e4c8f + 57e246a commit 231ec67

2 files changed

Lines changed: 35 additions & 35 deletions

File tree

apps/admin_audit/lib/Actions/Action.php

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,43 @@ public function __construct(
2121
* Log a single action with a log level of info
2222
*
2323
* @param string $text
24-
* @param array $params
25-
* @param array $elements
24+
* @param array<string, scalar|null|\DateTimeInterface> $params
25+
* @param list<string> $elements
2626
* @param bool $obfuscateParameters
2727
*/
28-
public function log(string $text,
28+
public function log(
29+
string $text,
2930
array $params,
3031
array $elements,
31-
bool $obfuscateParameters = false): void {
32+
bool $obfuscateParameters = false,
33+
): void {
3234
foreach ($elements as $element) {
33-
if (!isset($params[$element])) {
34-
if ($obfuscateParameters) {
35-
$this->logger->critical(
36-
'$params["' . $element . '"] was missing.',
37-
['app' => 'admin_audit']
38-
);
39-
} else {
40-
$this->logger->critical(
41-
'$params["' . $element . '"] was missing. Transferred value: {params}',
42-
['app' => 'admin_audit', 'params' => $params]
43-
);
35+
if (!array_key_exists($element, $params)) {
36+
$message = '$params["' . $element . '"] was missing.';
37+
$context = ['app' => 'admin_audit'];
38+
39+
if (!$obfuscateParameters) {
40+
$message .= ' Transferred value: {params}';
41+
$context['params'] = $params;
4442
}
43+
44+
$this->logger->critical($message, $context);
4545
return;
4646
}
4747
}
4848

4949
$replaceArray = [];
5050
foreach ($elements as $element) {
51-
if ($params[$element] instanceof \DateTime) {
52-
$params[$element] = $params[$element]->format('Y-m-d H:i:s');
51+
$value = $params[$element];
52+
if ($value instanceof \DateTimeInterface) {
53+
$value = $value->format('Y-m-d H:i:s');
5354
}
54-
$replaceArray[] = $params[$element];
55+
$replaceArray[] = $value;
5556
}
5657

5758
$this->logger->info(
58-
vsprintf(
59-
$text,
60-
$replaceArray
61-
),
62-
[
63-
'app' => 'admin_audit'
64-
]
59+
vsprintf($text, $replaceArray),
60+
['app' => 'admin_audit'],
6561
);
6662
}
6763
}

apps/admin_audit/lib/Actions/Files.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function read(BeforeNodeReadEvent $event): void {
3333
try {
3434
$node = $event->getNode();
3535
$params = [
36-
'id' => $node instanceof NonExistingFile ? null : $node->getId(),
36+
'id' => $node instanceof NonExistingFile ? 'not-yet-assigned' : $node->getId(),
3737
'path' => $node->getPath(),
3838
];
3939
} catch (InvalidPathException|NotFoundException $e) {
@@ -80,9 +80,10 @@ public function afterRename(NodeRenamedEvent $event): void {
8080
*/
8181
public function create(NodeCreatedEvent $event): void {
8282
try {
83+
$node = $event->getNode();
8384
$params = [
84-
'id' => $event->getNode()->getId(),
85-
'path' => $event->getNode()->getPath(),
85+
'id' => $node->getId(),
86+
'path' => $node->getPath(),
8687
];
8788
} catch (InvalidPathException|NotFoundException $e) {
8889
Server::get(LoggerInterface::class)->error(
@@ -105,11 +106,13 @@ public function create(NodeCreatedEvent $event): void {
105106
*/
106107
public function copy(NodeCopiedEvent $event): void {
107108
try {
109+
$source = $event->getSource();
110+
$target = $event->getTarget();
108111
$params = [
109-
'oldid' => $event->getSource()->getId(),
110-
'newid' => $event->getTarget()->getId(),
111-
'oldpath' => $event->getSource()->getPath(),
112-
'newpath' => $event->getTarget()->getPath(),
112+
'oldid' => $source->getId(),
113+
'newid' => $target->getId(),
114+
'oldpath' => $source->getPath(),
115+
'newpath' => $target->getPath(),
113116
];
114117
} catch (InvalidPathException|NotFoundException $e) {
115118
Server::get(LoggerInterface::class)->error(
@@ -128,8 +131,8 @@ public function copy(NodeCopiedEvent $event): void {
128131
* Logs writing of files
129132
*/
130133
public function write(NodeWrittenEvent $event): void {
131-
$node = $event->getNode();
132134
try {
135+
$node = $event->getNode();
133136
$params = [
134137
'id' => $node->getId(),
135138
'path' => $node->getPath(),
@@ -156,9 +159,10 @@ public function write(NodeWrittenEvent $event): void {
156159
*/
157160
public function delete(BeforeNodeDeletedEvent $event): void {
158161
try {
162+
$node = $event->getNode();
159163
$params = [
160-
'id' => $event->getNode()->getId(),
161-
'path' => $event->getNode()->getPath(),
164+
'id' => $node->getId(),
165+
'path' => $node->getPath(),
162166
];
163167
} catch (InvalidPathException|NotFoundException $e) {
164168
Server::get(LoggerInterface::class)->error(

0 commit comments

Comments
 (0)