Skip to content

Commit 9cfb9c7

Browse files
authored
Merge pull request #59620 from nextcloud/backport/59218/stable32
[stable32] feat(settings): Log AI config changes to audit log
2 parents e839fc8 + b1d29c0 commit 9cfb9c7

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

apps/settings/lib/Controller/AISettingsController.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,28 @@
1010

1111
use OCA\Settings\Settings\Admin\ArtificialIntelligence;
1212
use OCP\AppFramework\Controller;
13+
use OCP\AppFramework\Http;
1314
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
1415
use OCP\AppFramework\Http\DataResponse;
16+
use OCP\EventDispatcher\IEventDispatcher;
1517
use OCP\IAppConfig;
1618
use OCP\IRequest;
19+
use OCP\Log\Audit\CriticalActionPerformedEvent;
1720

1821
class AISettingsController extends Controller {
1922

2023
public function __construct(
2124
$appName,
2225
IRequest $request,
26+
private string $userId,
2327
private IAppConfig $appConfig,
28+
private IEventDispatcher $eventDispatcher,
2429
) {
2530
parent::__construct($appName, $request);
2631
}
2732

2833
/**
29-
* Sets the email settings
34+
* Sets the AI settings
3035
*
3136
* @param array $settings
3237
* @return DataResponse
@@ -38,7 +43,23 @@ public function update($settings) {
3843
if (!isset($settings[$key])) {
3944
continue;
4045
}
41-
$this->appConfig->setValueString('core', $key, json_encode($settings[$key]), lazy: in_array($key, \OC\TaskProcessing\Manager::LAZY_CONFIG_KEYS, true));
46+
try {
47+
$settings[$key] = json_encode($settings[$key], flags: \JSON_THROW_ON_ERROR);
48+
} catch (\JsonException) {
49+
return new DataResponse(['error' => "Setting value for '$key' must be JSON-compatible"], Http::STATUS_BAD_REQUEST);
50+
}
51+
}
52+
foreach ($keys as $key) {
53+
if (!isset($settings[$key])) {
54+
continue;
55+
}
56+
$changed = $this->appConfig->setValueString('core', $key, $settings[$key], lazy: in_array($key, \OC\TaskProcessing\Manager::LAZY_CONFIG_KEYS, true));
57+
if ($changed) {
58+
$this->eventDispatcher->dispatchTyped(new CriticalActionPerformedEvent(
59+
'AI configuration was changed by user %s: %s was set to %s',
60+
[$this->userId, $key, $settings[$key]]
61+
));
62+
}
4263
}
4364

4465
return new DataResponse();

0 commit comments

Comments
 (0)