From fe30ca872b71951bd58eb470085e61687cacd7b4 Mon Sep 17 00:00:00 2001 From: Akhil Date: Tue, 5 Dec 2023 21:27:50 +0530 Subject: [PATCH] feat(config): Add UserConfigChangedEvent whenever user config is updated Signed-off-by: Akhil --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + lib/private/Config/UserConfig.php | 9 ++- .../User/Events/UserConfigChangedEvent.php | 65 +++++++++++++++++++ tests/lib/Config/UserConfigTest.php | 4 ++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 lib/public/User/Events/UserConfigChangedEvent.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 8bc0887be64..4cbade8e68e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -981,6 +981,7 @@ return array( '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', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 631b7664b66..79f4c88358f 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1022,6 +1022,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', diff --git a/lib/private/Config/UserConfig.php b/lib/private/Config/UserConfig.php index 05018adc3f8..f7d135c134d 100644 --- a/lib/private/Config/UserConfig.php +++ b/lib/private/Config/UserConfig.php @@ -23,9 +23,11 @@ use OCP\Config\ValueType; use OCP\DB\Exception as DBException; use OCP\DB\IResult; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IDBConnection; use OCP\Security\ICrypto; +use OCP\User\Events\UserConfigChangedEvent; use Psr\Log\LoggerInterface; /** @@ -75,6 +77,7 @@ class UserConfig implements IUserConfig { private readonly PresetManager $presetManager, protected LoggerInterface $logger, protected ICrypto $crypto, + protected IEventDispatcher $dispatcher, ) { } @@ -1123,12 +1126,14 @@ class UserConfig implements IUserConfig { } } + $oldValue = null; if ($this->hasKey($userId, $app, $key, $lazy)) { /** * no update if key is already known with set lazy status and value is * not different, unless sensitivity is switched from false to true. */ - if ($origValue === $this->getTypedValue($userId, $app, $key, $value, $lazy, $type) + $oldValue = $this->getTypedValue($userId, $app, $key, $value, $lazy, $type); + if ($origValue === $oldValue && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) { return false; } @@ -1210,6 +1215,8 @@ class UserConfig implements IUserConfig { $update->executeStatement(); } + $this->dispatcher->dispatchTyped(new UserConfigChangedEvent($userId, $app, $key, $value, $oldValue)); + if ($refreshCache) { $this->clearCache($userId); return true; diff --git a/lib/public/User/Events/UserConfigChangedEvent.php b/lib/public/User/Events/UserConfigChangedEvent.php new file mode 100644 index 00000000000..27b10cdba35 --- /dev/null +++ b/lib/public/User/Events/UserConfigChangedEvent.php @@ -0,0 +1,65 @@ + + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\User\Events; + +use OCP\AppFramework\Attribute\Listenable; +use OCP\EventDispatcher\Event; + +/** @since 33.0.0 */ +#[Listenable(since: '33.0.0')] +class UserConfigChangedEvent extends Event { + /** + * @since 33.0.0 + */ + public function __construct( + private string $userId, + private string $appId, + private string $key, + private mixed $value, + private mixed $oldValue = null, + ) { + parent::__construct(); + } + + /** + * @since 33.0.0 + */ + public function getUserId(): string { + return $this->userId; + } + + /** + * @since 33.0.0 + */ + public function getAppId(): string { + return $this->appId; + } + + /** + * @since 33.0.0 + */ + public function getKey(): string { + return $this->key; + } + + /** + * @since 33.0.0 + */ + public function getValue(): mixed { + return $this->value; + } + + /** + * @since 33.0.0 + */ + public function getOldValue(): mixed { + return $this->oldValue; + } +} diff --git a/tests/lib/Config/UserConfigTest.php b/tests/lib/Config/UserConfigTest.php index 9dd5ab10084..d570bf020f1 100644 --- a/tests/lib/Config/UserConfigTest.php +++ b/tests/lib/Config/UserConfigTest.php @@ -14,6 +14,7 @@ use OCP\Config\Exceptions\TypeConflictException; use OCP\Config\Exceptions\UnknownKeyException; use OCP\Config\IUserConfig; use OCP\Config\ValueType; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\IDBConnection; use OCP\Security\ICrypto; @@ -35,6 +36,7 @@ class UserConfigTest extends TestCase { private PresetManager $presetManager; private LoggerInterface $logger; private ICrypto $crypto; + private IEventDispatcher $dispatcher; private array $originalPreferences; /** @@ -181,6 +183,7 @@ class UserConfigTest extends TestCase { $this->presetManager = Server::get(PresetManager::class); $this->logger = Server::get(LoggerInterface::class); $this->crypto = Server::get(ICrypto::class); + $this->dispatcher = Server::get(IEventDispatcher::class); // storing current preferences and emptying the data table $sql = $this->connection->getQueryBuilder(); @@ -292,6 +295,7 @@ class UserConfigTest extends TestCase { $this->presetManager, $this->logger, $this->crypto, + $this->dispatcher ); $msg = ' generateUserConfig() failed to confirm cache status';