1010
1111use OCA \Settings \Settings \Admin \ArtificialIntelligence ;
1212use OCP \AppFramework \Controller ;
13+ use OCP \AppFramework \Http ;
1314use OCP \AppFramework \Http \Attribute \AuthorizedAdminSetting ;
1415use OCP \AppFramework \Http \DataResponse ;
16+ use OCP \EventDispatcher \IEventDispatcher ;
1517use OCP \IAppConfig ;
1618use OCP \IRequest ;
19+ use OCP \Log \Audit \CriticalActionPerformedEvent ;
1720
1821class 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