Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UserConfigChangedEvent to fire whenever a user config value is changed #42039

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@
'OCP\\User\\Events\\PasswordUpdatedEvent' => $baseDir . '/lib/public/User/Events/PasswordUpdatedEvent.php',
'OCP\\User\\Events\\PostLoginEvent' => $baseDir . '/lib/public/User/Events/PostLoginEvent.php',
'OCP\\User\\Events\\UserChangedEvent' => $baseDir . '/lib/public/User/Events/UserChangedEvent.php',
'OCP\\User\\Events\\UserConfigChangedEvent' => $baseDir . '/lib/public/User/Events/UserConfigChangedEvent.php',
'OCP\\User\\Events\\UserCreatedEvent' => $baseDir . '/lib/public/User/Events/UserCreatedEvent.php',
'OCP\\User\\Events\\UserDeletedEvent' => $baseDir . '/lib/public/User/Events/UserDeletedEvent.php',
'OCP\\User\\Events\\UserFirstTimeLoggedInEvent' => $baseDir . '/lib/public/User/Events/UserFirstTimeLoggedInEvent.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\User\\Events\\PasswordUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/PasswordUpdatedEvent.php',
'OCP\\User\\Events\\PostLoginEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/PostLoginEvent.php',
'OCP\\User\\Events\\UserChangedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserChangedEvent.php',
'OCP\\User\\Events\\UserConfigChangedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserConfigChangedEvent.php',
'OCP\\User\\Events\\UserCreatedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserCreatedEvent.php',
'OCP\\User\\Events\\UserDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserDeletedEvent.php',
'OCP\\User\\Events\\UserFirstTimeLoggedInEvent' => __DIR__ . '/../../..' . '/lib/public/User/Events/UserFirstTimeLoggedInEvent.php',
Expand Down
11 changes: 11 additions & 0 deletions lib/private/AllConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\PreConditionNotMetException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\User\Events\UserConfigChangedEvent;

/**
* Class to combine all the configuration options ownCloud offers
Expand Down Expand Up @@ -256,6 +258,7 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu
$qb->executeStatement();

$this->userCache[$userId][$appName][$key] = (string)$value;
$this->triggerUserValueChange($userId, $appName, $key, $value, $prevValue);
return;
}
}
Expand All @@ -282,6 +285,14 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu
}
$this->userCache[$userId][$appName][$key] = (string)$value;
}
$this->triggerUserValueChange($userId, $appName, $key, $value, $prevValue);
}

private function triggerUserValueChange($userId, $appId, $key, $value, $oldValue = null) {
if (\OC::$server instanceof \OCP\IServerContainer) {
$dispatcher = \OC::$server->get(IEventDispatcher::class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot use DI for this as it is used in base.php over here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that the right link? The Allconfig constructor does not call setUserValue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@come-nc I believe I meant to say:

  1. AllConfig is instantiated in base.php as new \OC\AllConfig(new \OC\SystemConfig(self::$config))
  2. If I add IEventListener $eventListener in the constructor for DI in this class (the usual way), it could be problematic. I remember it causing errors when I first wrote this code, but maybe I should re-test? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes please, the event listener is available early so I would be surprised if this is called even earlier. Maybe for login time 🤔
If some value change call do not trigger the event we need to document that.

$dispatcher->dispatchTyped(new UserConfigChangedEvent($userId, $appId, $key, $value, $oldValue));
}
}

/**
Expand Down
98 changes: 98 additions & 0 deletions lib/public/User/Events/UserConfigChangedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Murena SAS <akhil.potukuchi.ext@murena.com>
*
* @author Murena SAS <akhil.potukuchi.ext@murena.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCP\User\Events;

use OCP\EventDispatcher\Event;

/**
* @since 31.0.0
*/

class UserConfigChangedEvent extends Event {
private string $userId;
private string $appId;
private string $key;
private mixed $value;
private mixed $oldValue;

/**
* @since 31.0.0
*/

public function __construct(string $userId,
string $appId,
string $key,
mixed $value,
mixed $oldValue = null) {
parent::__construct();
$this->userId = $userId;
$this->appId = $appId;
$this->key = $key;
$this->value = $value;
$this->oldValue = $oldValue;
}

/**
* @return string
* @since 31.0.0
*/
public function getUserId(): string {
return $this->userId;
}

/**
* @return string
* @since 31.0.0
*/
public function getAppId(): string {
return $this->appId;
}

/**
* @return string
* @since 31.0.0
*/
public function getKey(): string {
return $this->key;
}

/**
* @return mixed
* @since 31.0.0
*/
public function getValue() {
return $this->value;
}

/**
* @return mixed
* @since 31.0.0
*/
public function getOldValue() {
return $this->oldValue;
}
}