From 65e24f7def517d33ccaac15946db882fe35d8175 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 3 Oct 2024 12:52:26 -0100 Subject: [PATCH 1/5] feat(user-prefs): IUserPreferences Signed-off-by: Maxence Lange --- build/psalm-baseline.xml | 3 - .../Version31000Date20240814184402.php | 39 + lib/composer/composer/autoload_classmap.php | 9 + lib/composer/composer/autoload_static.php | 9 + lib/private/AllConfig.php | 236 +-- lib/private/Server.php | 2 + lib/private/UserPreferences.php | 1675 +++++++++++++++ .../Exceptions/IncorrectTypeException.php | 15 + .../Exceptions/TypeConflictException.php | 15 + .../Exceptions/UnknownKeyException.php | 15 + .../Exceptions/UserPreferencesException.php | 17 + .../UserPreferences/IUserPreferences.php | 609 ++++++ lib/public/UserPreferences/ValueType.php | 111 + .../UserPreferences/ValueTypeDefinition.php | 30 + tests/lib/AllConfigTest.php | 6 +- tests/lib/UserPreferencesTest.php | 1833 +++++++++++++++++ 16 files changed, 4429 insertions(+), 195 deletions(-) create mode 100644 core/Migrations/Version31000Date20240814184402.php create mode 100644 lib/private/UserPreferences.php create mode 100644 lib/public/UserPreferences/Exceptions/IncorrectTypeException.php create mode 100644 lib/public/UserPreferences/Exceptions/TypeConflictException.php create mode 100644 lib/public/UserPreferences/Exceptions/UnknownKeyException.php create mode 100644 lib/public/UserPreferences/Exceptions/UserPreferencesException.php create mode 100644 lib/public/UserPreferences/IUserPreferences.php create mode 100644 lib/public/UserPreferences/ValueType.php create mode 100644 lib/public/UserPreferences/ValueTypeDefinition.php create mode 100644 tests/lib/UserPreferencesTest.php diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml index 437be7441f8c9..ed96e4391f9cd 100644 --- a/build/psalm-baseline.xml +++ b/build/psalm-baseline.xml @@ -1328,9 +1328,6 @@ - - - diff --git a/core/Migrations/Version31000Date20240814184402.php b/core/Migrations/Version31000Date20240814184402.php new file mode 100644 index 0000000000000..20804b1b19efe --- /dev/null +++ b/core/Migrations/Version31000Date20240814184402.php @@ -0,0 +1,39 @@ +getTable('preferences'); + $table->addColumn('lazy', Types::SMALLINT, ['notnull' => true, 'default' => 0, 'length' => 1, 'unsigned' => true]); + $table->addColumn('type', Types::INTEGER, ['notnull' => true, 'default' => 2, 'unsigned' => true]); + $table->addIndex(['userid', 'lazy'], 'prefs_uid_lazy_i'); + + return $schema; + } +} diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 13fb25ab303f5..3e65291819b88 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -839,6 +839,13 @@ 'OCP\\UserMigration\\ISizeEstimationMigrator' => $baseDir . '/lib/public/UserMigration/ISizeEstimationMigrator.php', 'OCP\\UserMigration\\TMigratorBasicVersionHandling' => $baseDir . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php', 'OCP\\UserMigration\\UserMigrationException' => $baseDir . '/lib/public/UserMigration/UserMigrationException.php', + 'OCP\\UserPreferences\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php', + 'OCP\\UserPreferences\\Exceptions\\TypeConflictException' => $baseDir . '/lib/public/UserPreferences/Exceptions/TypeConflictException.php', + 'OCP\\UserPreferences\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/public/UserPreferences/Exceptions/UnknownKeyException.php', + 'OCP\\UserPreferences\\Exceptions\\UserPreferencesException' => $baseDir . '/lib/public/UserPreferences/Exceptions/UserPreferencesException.php', + 'OCP\\UserPreferences\\IUserPreferences' => $baseDir . '/lib/public/UserPreferences/IUserPreferences.php', + 'OCP\\UserPreferences\\ValueType' => $baseDir . '/lib/public/UserPreferences/ValueType.php', + 'OCP\\UserPreferences\\ValueTypeDefinition' => $baseDir . '/lib/public/UserPreferences/ValueTypeDefinition.php', 'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php', 'OCP\\UserStatus\\IProvider' => $baseDir . '/lib/public/UserStatus/IProvider.php', 'OCP\\UserStatus\\IUserStatus' => $baseDir . '/lib/public/UserStatus/IUserStatus.php', @@ -1386,6 +1393,7 @@ 'OC\\Core\\Migrations\\Version30000Date20240814180800' => $baseDir . '/core/Migrations/Version30000Date20240814180800.php', 'OC\\Core\\Migrations\\Version30000Date20240815080800' => $baseDir . '/core/Migrations/Version30000Date20240815080800.php', 'OC\\Core\\Migrations\\Version30000Date20240906095113' => $baseDir . '/core/Migrations/Version30000Date20240906095113.php', + 'OC\\Core\\Migrations\\Version31000Date20240814184402' => $baseDir . '/core/Migrations/Version31000Date20240814184402.php', 'OC\\Core\\Migrations\\Version31000Date20241018063111' => $baseDir . '/core/Migrations/Version31000Date20241018063111.php', 'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php', 'OC\\Core\\ResponseDefinitions' => $baseDir . '/core/ResponseDefinitions.php', @@ -1997,6 +2005,7 @@ 'OC\\Updater\\Exceptions\\ReleaseMetadataException' => $baseDir . '/lib/private/Updater/Exceptions/ReleaseMetadataException.php', 'OC\\Updater\\ReleaseMetadata' => $baseDir . '/lib/private/Updater/ReleaseMetadata.php', 'OC\\Updater\\VersionCheck' => $baseDir . '/lib/private/Updater/VersionCheck.php', + 'OC\\UserPreferences' => $baseDir . '/lib/private/UserPreferences.php', 'OC\\UserStatus\\ISettableProvider' => $baseDir . '/lib/private/UserStatus/ISettableProvider.php', 'OC\\UserStatus\\Manager' => $baseDir . '/lib/private/UserStatus/Manager.php', 'OC\\User\\AvailabilityCoordinator' => $baseDir . '/lib/private/User/AvailabilityCoordinator.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 526782636fbb5..46e59b2584696 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -880,6 +880,13 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\UserMigration\\ISizeEstimationMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/ISizeEstimationMigrator.php', 'OCP\\UserMigration\\TMigratorBasicVersionHandling' => __DIR__ . '/../../..' . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php', 'OCP\\UserMigration\\UserMigrationException' => __DIR__ . '/../../..' . '/lib/public/UserMigration/UserMigrationException.php', + 'OCP\\UserPreferences\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php', + 'OCP\\UserPreferences\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/TypeConflictException.php', + 'OCP\\UserPreferences\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/UnknownKeyException.php', + 'OCP\\UserPreferences\\Exceptions\\UserPreferencesException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/UserPreferencesException.php', + 'OCP\\UserPreferences\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/IUserPreferences.php', + 'OCP\\UserPreferences\\ValueType' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/ValueType.php', + 'OCP\\UserPreferences\\ValueTypeDefinition' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/ValueTypeDefinition.php', 'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php', 'OCP\\UserStatus\\IProvider' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IProvider.php', 'OCP\\UserStatus\\IUserStatus' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IUserStatus.php', @@ -1427,6 +1434,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Migrations\\Version30000Date20240814180800' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240814180800.php', 'OC\\Core\\Migrations\\Version30000Date20240815080800' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240815080800.php', 'OC\\Core\\Migrations\\Version30000Date20240906095113' => __DIR__ . '/../../..' . '/core/Migrations/Version30000Date20240906095113.php', + 'OC\\Core\\Migrations\\Version31000Date20240814184402' => __DIR__ . '/../../..' . '/core/Migrations/Version31000Date20240814184402.php', 'OC\\Core\\Migrations\\Version31000Date20241018063111' => __DIR__ . '/../../..' . '/core/Migrations/Version31000Date20241018063111.php', 'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php', 'OC\\Core\\ResponseDefinitions' => __DIR__ . '/../../..' . '/core/ResponseDefinitions.php', @@ -2038,6 +2046,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Updater\\Exceptions\\ReleaseMetadataException' => __DIR__ . '/../../..' . '/lib/private/Updater/Exceptions/ReleaseMetadataException.php', 'OC\\Updater\\ReleaseMetadata' => __DIR__ . '/../../..' . '/lib/private/Updater/ReleaseMetadata.php', 'OC\\Updater\\VersionCheck' => __DIR__ . '/../../..' . '/lib/private/Updater/VersionCheck.php', + 'OC\\UserPreferences' => __DIR__ . '/../../..' . '/lib/private/UserPreferences.php', 'OC\\UserStatus\\ISettableProvider' => __DIR__ . '/../../..' . '/lib/private/UserStatus/ISettableProvider.php', 'OC\\UserStatus\\Manager' => __DIR__ . '/../../..' . '/lib/private/UserStatus/Manager.php', 'OC\\User\\AvailabilityCoordinator' => __DIR__ . '/../../..' . '/lib/private/User/AvailabilityCoordinator.php', diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index 46b53e3c1b21d..bc54ea1e6afaa 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -7,10 +7,12 @@ namespace OC; use OCP\Cache\CappedMemoryCache; -use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; use OCP\IDBConnection; use OCP\PreConditionNotMetException; +use OCP\UserPreferences\Exceptions\TypeConflictException; +use OCP\UserPreferences\IUserPreferences; +use OCP\UserPreferences\ValueType; /** * Class to combine all the configuration options ownCloud offers @@ -226,62 +228,25 @@ public function deleteAppValues($appName) { * @param string $preCondition only update if the config value was previously the value passed as $preCondition * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met * @throws \UnexpectedValueException when trying to store an unexpected value + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { if (!is_int($value) && !is_float($value) && !is_string($value)) { throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value'); } - // TODO - FIXME - $this->fixDIInit(); - - if ($appName === 'settings' && $key === 'email') { - $value = strtolower((string)$value); - } - - $prevValue = $this->getUserValue($userId, $appName, $key, null); - - if ($prevValue !== null) { - if ($preCondition !== null && $prevValue !== (string)$preCondition) { - throw new PreConditionNotMetException(); - } elseif ($prevValue === (string)$value) { - return; - } else { - $qb = $this->connection->getQueryBuilder(); - $qb->update('preferences') - ->set('configvalue', $qb->createNamedParameter($value)) - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))) - ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName))) - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); - $qb->executeStatement(); - - $this->userCache[$userId][$appName][$key] = (string)$value; - return; + /** @var UserPreferences $userPreferences */ + $userPreferences = \OC::$server->get(IUserPreferences::class); + if ($preCondition !== null) { + try { + if ($userPreferences->getValueMixed($userId, $appName, $key) !== (string)$preCondition) { + throw new PreConditionNotMetException(); + } + } catch (TypeConflictException) { } } - $preconditionArray = []; - if (isset($preCondition)) { - $preconditionArray = [ - 'configvalue' => $preCondition, - ]; - } - - $this->connection->setValues('preferences', [ - 'userid' => $userId, - 'appid' => $appName, - 'configkey' => $key, - ], [ - 'configvalue' => $value, - ], $preconditionArray); - - // only add to the cache if we already loaded data for the user - if (isset($this->userCache[$userId])) { - if (!isset($this->userCache[$userId][$appName])) { - $this->userCache[$userId][$appName] = []; - } - $this->userCache[$userId][$appName][$key] = (string)$value; - } + $userPreferences->setValueMixed($userId, $appName, $key, (string)$value); } /** @@ -292,14 +257,19 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu * @param string $key the key under which the value is being stored * @param mixed $default the default value to be returned if the value isn't set * @return string + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function getUserValue($userId, $appName, $key, $default = '') { - $data = $this->getAllUserValues($userId); - if (isset($data[$appName][$key])) { - return $data[$appName][$key]; - } else { + if ($userId === null || $userId === '') { return $default; } + /** @var UserPreferences $userPreferences */ + $userPreferences = \OC::$server->get(IUserPreferences::class); + // because $default can be null ... + if (!$userPreferences->hasKey($userId, $appName, $key)) { + return $default; + } + return $userPreferences->getValueMixed($userId, $appName, $key, $default ?? ''); } /** @@ -308,14 +278,10 @@ public function getUserValue($userId, $appName, $key, $default = '') { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @return string[] + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function getUserKeys($userId, $appName) { - $data = $this->getAllUserValues($userId); - if (isset($data[$appName])) { - return array_map('strval', array_keys($data[$appName])); - } else { - return []; - } + return \OC::$server->get(IUserPreferences::class)->getKeys($userId, $appName); } /** @@ -324,56 +290,33 @@ public function getUserKeys($userId, $appName) { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @param string $key the key under which the value is being stored + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function deleteUserValue($userId, $appName, $key) { - // TODO - FIXME - $this->fixDIInit(); - - $qb = $this->connection->getQueryBuilder(); - $qb->delete('preferences') - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) - ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR))) - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR))) - ->executeStatement(); - - if (isset($this->userCache[$userId][$appName])) { - unset($this->userCache[$userId][$appName][$key]); - } + \OC::$server->get(IUserPreferences::class)->deletePreference($userId, $appName, $key); } /** * Delete all user values * * @param string $userId the userId of the user that we want to remove all values from + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function deleteAllUserValues($userId) { - // TODO - FIXME - $this->fixDIInit(); - $qb = $this->connection->getQueryBuilder(); - $qb->delete('preferences') - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) - ->executeStatement(); - - unset($this->userCache[$userId]); + if ($userId === null) { + return; + } + \OC::$server->get(IUserPreferences::class)->deleteAllPreferences($userId); } /** * Delete all user related values of one app * * @param string $appName the appName of the app that we want to remove all values from + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function deleteAppFromAllUsers($appName) { - // TODO - FIXME - $this->fixDIInit(); - - $qb = $this->connection->getQueryBuilder(); - $qb->delete('preferences') - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR))) - ->executeStatement(); - - foreach ($this->userCache as &$userCache) { - unset($userCache[$appName]); - } + \OC::$server->get(IUserPreferences::class)->deleteApp($appName); } /** @@ -385,35 +328,21 @@ public function deleteAppFromAllUsers($appName) { * [ $appId => * [ $key => $value ] * ] + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function getAllUserValues(?string $userId): array { - if (isset($this->userCache[$userId])) { - return $this->userCache[$userId]; - } if ($userId === null || $userId === '') { - $this->userCache[''] = []; - return $this->userCache['']; + return []; } - // TODO - FIXME - $this->fixDIInit(); - - $data = []; - - $qb = $this->connection->getQueryBuilder(); - $result = $qb->select('appid', 'configkey', 'configvalue') - ->from('preferences') - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) - ->executeQuery(); - while ($row = $result->fetch()) { - $appId = $row['appid']; - if (!isset($data[$appId])) { - $data[$appId] = []; + $values = \OC::$server->get(IUserPreferences::class)->getAllValues($userId); + $result = []; + foreach ($values as $app => $list) { + foreach ($list as $key => $value) { + $result[$app][$key] = (string)$value; } - $data[$appId][$row['configkey']] = $row['configvalue']; } - $this->userCache[$userId] = $data; - return $data; + return $result; } /** @@ -423,37 +352,10 @@ public function getAllUserValues(?string $userId): array { * @param string $key the key to get the value for * @param array $userIds the user IDs to fetch the values for * @return array Mapped values: userId => value + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function getUserValueForUsers($appName, $key, $userIds) { - // TODO - FIXME - $this->fixDIInit(); - - if (empty($userIds) || !is_array($userIds)) { - return []; - } - - $chunkedUsers = array_chunk($userIds, 50, true); - - $qb = $this->connection->getQueryBuilder(); - $qb->select('userid', 'configvalue') - ->from('preferences') - ->where($qb->expr()->eq('appid', $qb->createParameter('appName'))) - ->andWhere($qb->expr()->eq('configkey', $qb->createParameter('configKey'))) - ->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds'))); - - $userValues = []; - foreach ($chunkedUsers as $chunk) { - $qb->setParameter('appName', $appName, IQueryBuilder::PARAM_STR); - $qb->setParameter('configKey', $key, IQueryBuilder::PARAM_STR); - $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY); - $result = $qb->executeQuery(); - - while ($row = $result->fetch()) { - $userValues[$row['userid']] = $row['configvalue']; - } - } - - return $userValues; + return \OC::$server->get(IUserPreferences::class)->searchValuesByUsers($appName, $key, ValueType::MIXED, $userIds); } /** @@ -463,31 +365,10 @@ public function getUserValueForUsers($appName, $key, $userIds) { * @param string $key the key to get the user for * @param string $value the value to get the user for * @return list of user IDs + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function getUsersForUserValue($appName, $key, $value) { - // TODO - FIXME - $this->fixDIInit(); - - $qb = $this->connection->getQueryBuilder(); - $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) - ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) - : 'configvalue'; - $result = $qb->select('userid') - ->from('preferences') - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR))) - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR))) - ->andWhere($qb->expr()->eq( - $configValueColumn, - $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR)) - )->orderBy('userid') - ->executeQuery(); - - $userIDs = []; - while ($row = $result->fetch()) { - $userIDs[] = $row['userid']; - } - - return $userIDs; + return \OC::$server->get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value); } /** @@ -497,37 +378,14 @@ public function getUsersForUserValue($appName, $key, $value) { * @param string $key the key to get the user for * @param string $value the value to get the user for * @return list of user IDs + * @deprecated 31.0.0 - use {@see IUserPreferences} directly */ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) { - // TODO - FIXME - $this->fixDIInit(); - if ($appName === 'settings' && $key === 'email') { - // Email address is always stored lowercase in the database return $this->getUsersForUserValue($appName, $key, strtolower($value)); } - $qb = $this->connection->getQueryBuilder(); - $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) - ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) - : 'configvalue'; - - $result = $qb->select('userid') - ->from('preferences') - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR))) - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR))) - ->andWhere($qb->expr()->eq( - $qb->func()->lower($configValueColumn), - $qb->createNamedParameter(strtolower($value), IQueryBuilder::PARAM_STR)) - )->orderBy('userid') - ->executeQuery(); - - $userIDs = []; - while ($row = $result->fetch()) { - $userIDs[] = $row['userid']; - } - - return $userIDs; + return \OC::$server->get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value, true); } public function getSystemConfig() { diff --git a/lib/private/Server.php b/lib/private/Server.php index 27a5f2662f822..10d6dee9d07f2 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -237,6 +237,7 @@ use OCP\User\Events\UserLoggedInWithCookieEvent; use OCP\User\Events\UserLoggedOutEvent; use OCP\User\IAvailabilityCoordinator; +use OCP\UserPreferences\IUserPreferences; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; @@ -567,6 +568,7 @@ public function __construct($webRoot, \OC\Config $config) { }); $this->registerAlias(IAppConfig::class, \OC\AppConfig::class); + $this->registerAlias(IUserPreferences::class, \OC\UserPreferences::class); $this->registerService(IFactory::class, function (Server $c) { return new \OC\L10N\Factory( diff --git a/lib/private/UserPreferences.php b/lib/private/UserPreferences.php new file mode 100644 index 0000000000000..a5426e5d247b7 --- /dev/null +++ b/lib/private/UserPreferences.php @@ -0,0 +1,1675 @@ +>> ['user_id' => ['app_id' => ['key' => 'value']]] */ + private array $fastCache = []; // cache for normal preference keys + /** @var array>> ['user_id' => ['app_id' => ['key' => 'value']]] */ + private array $lazyCache = []; // cache for lazy preference keys + /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ + private array $valueTypes = []; // type for all preference values + /** @var array ['user_id' => bool] */ + private array $fastLoaded = []; + /** @var array ['user_id' => bool] */ + private array $lazyLoaded = []; + + public function __construct( + protected IDBConnection $connection, + protected LoggerInterface $logger, + protected ICrypto $crypto, + ) { + } + + /** + * @inheritDoc + * + * @param string $appId optional id of app + * + * @return list list of userIds + * @since 31.0.0 + */ + public function getUserIds(string $appId = ''): array { + $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true); + + $qb = $this->connection->getQueryBuilder(); + $qb->from('preferences'); + $qb->select('userid'); + $qb->groupBy('userid'); + if ($appId !== '') { + $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId))); + } + + $result = $qb->executeQuery(); + $rows = $result->fetchAll(); + $userIds = []; + foreach ($rows as $row) { + $userIds[] = $row['userid']; + } + + return $userIds; + } + + /** + * @inheritDoc + * + * @return list list of app ids + * @since 31.0.0 + */ + public function getApps(string $userId): array { + $this->assertParams($userId, allowEmptyApp: true); + $this->loadPreferencesAll($userId); + $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? [])); + sort($apps); + + return array_values(array_unique($apps)); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * + * @return list list of stored preference keys + * @since 31.0.0 + */ + public function getKeys(string $userId, string $app): array { + $this->assertParams($userId, $app); + $this->loadPreferencesAll($userId); + // array_merge() will remove numeric keys (here preference keys), so addition arrays instead + $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []))); + sort($keys); + + return array_values(array_unique($keys)); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences + * + * @return bool TRUE if key exists + * @since 31.0.0 + */ + public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool { + $this->assertParams($userId, $app, $key); + $this->loadPreferences($userId, $lazy); + + if ($lazy === null) { + $appCache = $this->getValues($userId, $app); + return isset($appCache[$key]); + } + + if ($lazy) { + return isset($this->lazyCache[$userId][$app][$key]); + } + + return isset($this->fastCache[$userId][$app][$key]); + } + + /** + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences + * + * @return bool + * @throws UnknownKeyException if preference key is not known + * @since 29.0.0 + */ + public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { + $this->assertParams($userId, $app, $key); + $this->loadPreferences($userId, $lazy); + + if (!isset($this->valueTypes[$userId][$app][$key])) { + throw new UnknownKeyException('unknown preference key'); + } + + return $this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key]); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app if of the app + * @param string $key preference key + * + * @return bool TRUE if preference is lazy loaded + * @throws UnknownKeyException if preference key is not known + * @see IUserPreferences for details about lazy loading + * @since 29.0.0 + */ + public function isLazy(string $userId, string $app, string $key): bool { + // there is a huge probability the non-lazy preferences are already loaded + if ($this->hasKey($userId, $app, $key, false)) { + return false; + } + + // key not found, we search in the lazy preferences + if ($this->hasKey($userId, $app, $key, true)) { + return true; + } + + throw new UnknownKeyException('unknown preference key'); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $prefix preference keys prefix to search + * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} + * + * @return array [key => value] + * @since 31.0.0 + */ + public function getValues( + string $userId, + string $app, + string $prefix = '', + bool $filtered = false, + ): array { + $this->assertParams($userId, $app, $prefix); + // if we want to filter values, we need to get sensitivity + $this->loadPreferencesAll($userId); + // array_merge() will remove numeric keys (here preference keys), so addition arrays instead + $values = array_filter( + $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered), + function (string $key) use ($prefix): bool { + return str_starts_with($key, $prefix); // filter values based on $prefix + }, ARRAY_FILTER_USE_KEY + ); + + return $values; + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} + * + * @return array> [appId => [key => value]] + * @since 31.0.0 + */ + public function getAllValues(string $userId, bool $filtered = false): array { + $this->assertParams($userId, allowEmptyApp: true); + $this->loadPreferencesAll($userId); + + $result = []; + foreach ($this->getApps($userId) as $app) { + // array_merge() will remove numeric keys (here preference keys), so addition arrays instead + $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []); + $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered); + } + + return $result; + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $key preference key + * @param bool $lazy search within lazy loaded preferences + * @param ValueType|null $typedAs enforce type for the returned values + * + * @return array [appId => value] + * @since 31.0.0 + */ + public function searchValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { + $this->assertParams($userId, '', $key, allowEmptyApp: true); + $this->loadPreferences($userId, $lazy); + + /** @var array> $cache */ + if ($lazy) { + $cache = $this->lazyCache[$userId]; + } else { + $cache = $this->fastCache[$userId]; + } + + $values = []; + foreach (array_keys($cache) as $app) { + if (isset($cache[$app][$key])) { + $value = $cache[$app][$key]; + try { + $this->decryptSensitiveValue($userId, $app, $key, $value); + $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy)); + } catch (IncorrectTypeException|UnknownKeyException) { + } + $values[$app] = $value; + } + } + + return $values; + } + + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param ValueType|null $typedAs enforce type for the returned values + * @param array|null $userIds limit to a list of user ids + * + * @return array [userId => value] + * @since 31.0.0 + */ + public function searchValuesByUsers( + string $app, + string $key, + ?ValueType $typedAs = null, + ?array $userIds = null, + ): array { + $this->assertParams('', $app, $key, allowEmptyUser: true); + + $qb = $this->connection->getQueryBuilder(); + $qb->select('userid', 'configvalue', 'type') + ->from('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); + + $values = []; + // this nested function will execute current Query and store result within $values. + $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult { + $result = $qb->executeQuery(); + while ($row = $result->fetch()) { + $value = $row['configvalue']; + try { + $value = $this->convertTypedValue($value, $typedAs ?? $this->extractValueType($row['type'])); + } catch (IncorrectTypeException) { + } + $values[$row['userid']] = $value; + } + return $result; + }; + + // if no userIds to filter, we execute query as it is and returns all values ... + if ($userIds === null) { + $result = $executeAndStoreValue($qb); + $result->closeCursor(); + return $values; + } + + // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values + $result = null; + $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds'))); + foreach (array_chunk($userIds, 50, true) as $chunk) { + $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY); + $result = $executeAndStoreValue($qb); + } + $result?->closeCursor(); + + return $values; + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): array { + return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param int $value preference value + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueInt(string $app, string $key, int $value): array { + return $this->searchUsersByValueString($app, $key, (string)$value); + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param array $values list of preference values + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValues(string $app, string $key, array $values): array { + return $this->searchUsersByTypedValue($app, $key, $values); + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param bool $value preference value + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueBool(string $app, string $key, bool $value): array { + $values = ['0', 'off', 'false', 'no']; + if ($value) { + $values = ['1', 'on', 'true', 'yes']; + } + return $this->searchUsersByValues($app, $key, $values); + } + + /** + * returns a list of users with preference key set to a specific value, or within the list of + * possible values + * + * @param string $app + * @param string $key + * @param string|array $value + * @param bool $caseInsensitive + * + * @return list + */ + private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): array { + $this->assertParams('', $app, $key, allowEmptyUser: true); + + $qb = $this->connection->getQueryBuilder(); + $qb->from('preferences'); + $qb->select('userid'); + $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); + $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); + + $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; + if (is_array($value)) { + $qb->andWhere($qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY))); + } else { + if ($caseInsensitive) { + $qb->andWhere($qb->expr()->eq( + $qb->func()->lower($configValueColumn), + $qb->createNamedParameter(strtolower($value))) + ); + } else { + $qb->andWhere($qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value))); + } + } + + $userIds = []; + $result = $qb->executeQuery(); + $rows = $result->fetchAll(); + foreach ($rows as $row) { + $userIds[] = $row['userid']; + } + + return $userIds; + } + + /** + * Get the preference value as string. + * If the value does not exist the given default will be returned. + * + * Set lazy to `null` to ignore it and get the value from either source. + * + * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $default preference value + * @param null|bool $lazy get preference as lazy loaded or not. can be NULL + * + * @return string the value or $default + * @throws TypeConflictException + * @internal + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see getValueString() + * @see getValueInt() + * @see getValueFloat() + * @see getValueBool() + * @see getValueArray() + */ + public function getValueMixed( + string $userId, + string $app, + string $key, + string $default = '', + ?bool $lazy = false, + ): string { + try { + $lazy = ($lazy === null) ? $this->isLazy($userId, $app, $key) : $lazy; + } catch (UnknownKeyException) { + return $default; + } + + return $this->getTypedValue( + $userId, + $app, + $key, + $default, + $lazy, + ValueType::MIXED + ); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return string stored preference value or $default if not set in database + * @throws InvalidArgumentException if one of the argument format is invalid + * @throws TypeConflictException in case of conflict with the value type set in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function getValueString( + string $userId, + string $app, + string $key, + string $default = '', + bool $lazy = false, + ): string { + return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param int $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return int stored preference value or $default if not set in database + * @throws InvalidArgumentException if one of the argument format is invalid + * @throws TypeConflictException in case of conflict with the value type set in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function getValueInt( + string $userId, + string $app, + string $key, + int $default = 0, + bool $lazy = false, + ): int { + return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param float $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return float stored preference value or $default if not set in database + * @throws InvalidArgumentException if one of the argument format is invalid + * @throws TypeConflictException in case of conflict with the value type set in database + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function getValueFloat( + string $userId, + string $app, + string $key, + float $default = 0, + bool $lazy = false, + ): float { + return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return bool stored preference value or $default if not set in database + * @throws InvalidArgumentException if one of the argument format is invalid + * @throws TypeConflictException in case of conflict with the value type set in database + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function getValueBool( + string $userId, + string $app, + string $key, + bool $default = false, + bool $lazy = false, + ): bool { + $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL)); + return in_array($b, ['1', 'true', 'yes', 'on']); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param array $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return array stored preference value or $default if not set in database + * @throws InvalidArgumentException if one of the argument format is invalid + * @throws TypeConflictException in case of conflict with the value type set in database + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function getValueArray( + string $userId, + string $app, + string $key, + array $default = [], + bool $lazy = false, + ): array { + try { + $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); + $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR); + + return is_array($value) ? $value : []; + } catch (JsonException) { + return []; + } + } + + /** + * @param string $userId + * @param string $app id of the app + * @param string $key preference key + * @param string $default default value + * @param bool $lazy search within lazy loaded preferences + * @param ValueType $type value type + * + * @return string + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + */ + private function getTypedValue( + string $userId, + string $app, + string $key, + string $default, + bool $lazy, + ValueType $type, + ): string { + $this->assertParams($userId, $app, $key, valueType: $type); + $this->loadPreferences($userId, $lazy); + + /** + * We ignore check if mixed type is requested. + * If type of stored value is set as mixed, we don't filter. + * If type of stored value is defined, we compare with the one requested. + */ + $knownType = $this->valueTypes[$userId][$app][$key] ?? 0; + if (!$this->isTyped(ValueType::MIXED, $type->value) + && $knownType > 0 + && !$this->isTyped(ValueType::MIXED, $knownType) + && !$this->isTyped($type, $knownType)) { + $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); + throw new TypeConflictException('conflict with value type from database'); + } + + /** + * - the pair $app/$key cannot exist in both array, + * - we should still return an existing non-lazy value even if current method + * is called with $lazy is true + * + * This way, lazyCache will be empty until the load for lazy preferences value is requested. + */ + if (isset($this->lazyCache[$userId][$app][$key])) { + $value = $this->lazyCache[$userId][$app][$key]; + } elseif (isset($this->fastCache[$userId][$app][$key])) { + $value = $this->fastCache[$userId][$app][$key]; + } else { + return $default; + } + + $this->decryptSensitiveValue($userId, $app, $key, $value); + return $value; + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * + * @return ValueType type of the value + * @throws UnknownKeyException if preference key is not known + * @throws IncorrectTypeException if preferences value type is not known + * @since 31.0.0 + */ + public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType { + $this->assertParams($userId, $app, $key); + $this->loadPreferences($userId, $lazy); + + if (!isset($this->valueTypes[$userId][$app][$key])) { + throw new UnknownKeyException('unknown preference key'); + } + + return $this->extractValueType($this->valueTypes[$userId][$app][$key]); + } + + /** + * convert bitflag from value type to ValueType + * + * @param int $type + * + * @return ValueType + * @throws IncorrectTypeException + */ + private function extractValueType(int $type): ValueType { + $type &= ~ValueType::SENSITIVE->value; + + try { + return ValueType::from($type); + } catch (ValueError) { + throw new IncorrectTypeException('invalid value type'); + } + } + + /** + * Store a preference key and its value in database as VALUE_MIXED + * + * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $lazy set preference as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * + * @return bool TRUE if value was different, therefor updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED + * @internal + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see setValueString() + * @see setValueInt() + * @see setValueFloat() + * @see setValueBool() + * @see setValueArray() + */ + public function setValueMixed( + string $userId, + string $app, + string $key, + string $value, + bool $lazy = false, + bool $sensitive = false, + ): bool { + return $this->setTypedValue( + $userId, + $app, + $key, + $value, + $lazy, + $sensitive, + ValueType::MIXED + ); + } + + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $lazy set preference as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * + * @return bool TRUE if value was different, therefor updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function setValueString( + string $userId, + string $app, + string $key, + string $value, + bool $lazy = false, + bool $sensitive = false, + ): bool { + return $this->setTypedValue( + $userId, + $app, + $key, + $value, + $lazy, + $sensitive, + ValueType::STRING + ); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param int $value preference value + * @param bool $lazy set preference as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * + * @return bool TRUE if value was different, therefor updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function setValueInt( + string $userId, + string $app, + string $key, + int $value, + bool $lazy = false, + bool $sensitive = false, + ): bool { + if ($value > 2000000000) { + $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); + } + + return $this->setTypedValue( + $userId, + $app, + $key, + (string)$value, + $lazy, + $sensitive, + ValueType::INT + ); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param float $value preference value + * @param bool $lazy set preference as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * + * @return bool TRUE if value was different, therefor updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function setValueFloat( + string $userId, + string $app, + string $key, + float $value, + bool $lazy = false, + bool $sensitive = false, + ): bool { + return $this->setTypedValue( + $userId, + $app, + $key, + (string)$value, + $lazy, + $sensitive, + ValueType::FLOAT + ); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $value preference value + * @param bool $lazy set preference as lazy loaded + * + * @return bool TRUE if value was different, therefor updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function setValueBool( + string $userId, + string $app, + string $key, + bool $value, + bool $lazy = false, + ): bool { + return $this->setTypedValue( + $userId, + $app, + $key, + ($value) ? '1' : '0', + $lazy, + false, + ValueType::BOOL + ); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param array $value preference value + * @param bool $lazy set preference as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * + * @return bool TRUE if value was different, therefor updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + * @throws JsonException + * @since 29.0.0 + * @see IUserPreferences for explanation about lazy loading + */ + public function setValueArray( + string $userId, + string $app, + string $key, + array $value, + bool $lazy = false, + bool $sensitive = false, + ): bool { + try { + return $this->setTypedValue( + $userId, + $app, + $key, + json_encode($value, JSON_THROW_ON_ERROR), + $lazy, + $sensitive, + ValueType::ARRAY + ); + } catch (JsonException $e) { + $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); + throw $e; + } + } + + /** + * Store a preference key and its value in database + * + * If preference key is already known with the exact same preference value and same sensitive/lazy status, the + * database is not updated. If preference value was previously stored as sensitive, status will not be + * altered. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $lazy preferences set as lazy loaded + * @param ValueType $type value type + * + * @return bool TRUE if value was updated in database + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one + * @see IUserPreferences for explanation about lazy loading + */ + private function setTypedValue( + string $userId, + string $app, + string $key, + string $value, + bool $lazy, + bool $sensitive, + ValueType $type, + ): bool { + $this->assertParams($userId, $app, $key, valueType: $type); + $this->loadPreferences($userId, $lazy); + + $inserted = $refreshCache = false; + $origValue = $value; + $typeValue = $type->value; + if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); + $typeValue = $typeValue | ValueType::SENSITIVE->value; + } + + 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) + && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) { + return false; + } + } else { + /** + * if key is not known yet, we try to insert. + * It might fail if the key exists with a different lazy flag. + */ + try { + $insert = $this->connection->getQueryBuilder(); + $insert->insert('preferences') + ->setValue('userid', $insert->createNamedParameter($userId)) + ->setValue('appid', $insert->createNamedParameter($app)) + ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) + ->setValue('type', $insert->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->setValue('configkey', $insert->createNamedParameter($key)) + ->setValue('configvalue', $insert->createNamedParameter($value)); + $insert->executeStatement(); + $inserted = true; + } catch (DBException $e) { + if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { + throw $e; // TODO: throw exception or just log and returns false !? + } + } + } + + /** + * We cannot insert a new row, meaning we need to update an already existing one + */ + if (!$inserted) { + $currType = $this->valueTypes[$userId][$app][$key] ?? 0; + if ($currType === 0) { // this might happen when switching lazy loading status + $this->loadPreferencesAll($userId); + $currType = $this->valueTypes[$userId][$app][$key] ?? 0; + } + + /** + * This should only happen during the upgrade process from 28 to 29. + * We only log a warning and set it to VALUE_MIXED. + */ + if ($currType === 0) { + $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]); + $currType = ValueType::MIXED->value; + } + + // if ($type->isSensitive()) {} + + /** + * we only accept a different type from the one stored in database + * if the one stored in database is not-defined (VALUE_MIXED) + */ + if (!$this->isTyped(ValueType::MIXED, $currType) && + ($type->value | ValueType::SENSITIVE->value) !== ($currType | ValueType::SENSITIVE->value)) { + try { + $currType = $this->extractValueType($currType)->getDefinition(); + $type = $type->getDefinition(); + } catch (IncorrectTypeException) { + $type = $type->value; + } + throw new TypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')'); + } + + if ($lazy !== $this->isLazy($userId, $app, $key)) { + $refreshCache = true; + } + + $update = $this->connection->getQueryBuilder(); + $update->update('preferences') + ->set('configvalue', $update->createNamedParameter($value)) + ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) + ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + + $update->executeStatement(); + } + + if ($refreshCache) { + $this->clearCache($userId); + return true; + } + + // update local cache + if ($lazy) { + $this->lazyCache[$userId][$app][$key] = $value; + } else { + $this->fastCache[$userId][$app][$key] = $value; + } + $this->valueTypes[$userId][$app][$key] = $typeValue; + + return true; + } + + /** + * Change the type of preference value. + * + * **WARNING:** Method is internal and **MUST** not be used as it may break things. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param ValueType $type value type + * + * @return bool TRUE if database update were necessary + * @throws UnknownKeyException if $key is now known in database + * @throws IncorrectTypeException if $type is not valid + * @internal + * @since 31.0.0 + */ + public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { + $this->assertParams($userId, $app, $key, valueType: $type); + $this->loadPreferencesAll($userId); + $this->isLazy($userId, $app, $key); // confirm key exists + $typeValue = $type->value; + + $currType = $this->valueTypes[$userId][$app][$key]; + if (($typeValue | ValueType::SENSITIVE->value) === ($currType | ValueType::SENSITIVE->value)) { + return false; + } + + // we complete with sensitive flag if the stored value is set as sensitive + if ($this->isTyped(ValueType::SENSITIVE, $currType)) { + $typeValue = $typeValue | ValueType::SENSITIVE->value; + } + + $update = $this->connection->getQueryBuilder(); + $update->update('preferences') + ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + $update->executeStatement(); + $this->valueTypes[$userId][$app][$key] = $typeValue; + + return true; + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset + * + * @return bool TRUE if entry was found in database and an update was necessary + * @since 31.0.0 + */ + public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool { + $this->assertParams($userId, $app, $key); + $this->loadPreferencesAll($userId); + + try { + if ($sensitive === $this->isSensitive($userId, $app, $key, null)) { + return false; + } + } catch (UnknownKeyException) { + return false; + } + + $lazy = $this->isLazy($userId, $app, $key); + if ($lazy) { + $cache = $this->lazyCache; + } else { + $cache = $this->fastCache; + } + + if (!isset($cache[$userId][$app][$key])) { + throw new UnknownKeyException('unknown preference key'); + } + + /** + * type returned by getValueType() is already cleaned from sensitive flag + * we just need to update it based on $sensitive and store it in database + */ + $typeValue = $this->getValueType($userId, $app, $key)->value; + $value = $cache[$userId][$app][$key]; + if ($sensitive) { + $typeValue |= ValueType::SENSITIVE->value; + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); + } else { + $this->decryptSensitiveValue($userId, $app, $key, $value); + } + + $update = $this->connection->getQueryBuilder(); + $update->update('preferences') + ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->set('configvalue', $update->createNamedParameter($value)) + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + $update->executeStatement(); + + $this->valueTypes[$userId][$app][$key] = $typeValue; + + return true; + } + + /** + * @inheritDoc + * + * @param string $app + * @param string $key + * @param bool $sensitive + * + * @since 31.0.0 + */ + public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void { + $this->assertParams('', $app, $key, allowEmptyUser: true); + foreach (array_keys($this->searchValuesByUsers($app, $key)) as $userId) { + try { + $this->updateSensitive($userId, $app, $key, $sensitive); + } catch (UnknownKeyException) { + // should not happen and can be ignored + } + } + + $this->clearCacheAll(); // we clear all cache + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset + * + * @return bool TRUE if entry was found in database and an update was necessary + * @since 31.0.0 + */ + public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool { + $this->assertParams($userId, $app, $key); + $this->loadPreferencesAll($userId); + + try { + if ($lazy === $this->isLazy($userId, $app, $key)) { + return false; + } + } catch (UnknownKeyException) { + return false; + } + + $update = $this->connection->getQueryBuilder(); + $update->update('preferences') + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + $update->executeStatement(); + + // At this point, it is a lot safer to clean cache + $this->clearCache($userId); + + return true; + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset + * + * @since 31.0.0 + */ + public function updateGlobalLazy(string $app, string $key, bool $lazy): void { + $this->assertParams('', $app, $key, allowEmptyUser: true); + + $update = $this->connection->getQueryBuilder(); + $update->update('preferences') + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + $update->executeStatement(); + + $this->clearCacheAll(); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * + * @return array + * @throws UnknownKeyException if preference key is not known in database + * @since 31.0.0 + */ + public function getDetails(string $userId, string $app, string $key): array { + $this->assertParams($userId, $app, $key); + $this->loadPreferencesAll($userId); + $lazy = $this->isLazy($userId, $app, $key); + + if ($lazy) { + $cache = $this->lazyCache[$userId]; + } else { + $cache = $this->fastCache[$userId]; + } + + $type = $this->getValueType($userId, $app, $key); + try { + $typeString = $type->getDefinition(); + } catch (IncorrectTypeException $e) { + $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); + $typeString = (string)$type->value; + } + + if (!isset($cache[$app][$key])) { + throw new UnknownKeyException('unknown preference key'); + } + + $value = $cache[$app][$key]; + $sensitive = $this->isSensitive($userId, $app, $key, null); + $this->decryptSensitiveValue($userId, $app, $key, $value); + + return [ + 'userId' => $userId, + 'app' => $app, + 'key' => $key, + 'value' => $value, + 'type' => $type->value, + 'lazy' => $lazy, + 'typeString' => $typeString, + 'sensitive' => $sensitive + ]; + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * + * @since 31.0.0 + */ + public function deletePreference(string $userId, string $app, string $key): void { + $this->assertParams($userId, $app, $key); + $qb = $this->connection->getQueryBuilder(); + $qb->delete('preferences') + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); + $qb->executeStatement(); + + unset($this->lazyCache[$userId][$app][$key]); + unset($this->fastCache[$userId][$app][$key]); + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * + * @since 31.0.0 + */ + public function deleteKey(string $app, string $key): void { + $this->assertParams('', $app, $key, allowEmptyUser: true); + $qb = $this->connection->getQueryBuilder(); + $qb->delete('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); + $qb->executeStatement(); + + $this->clearCacheAll(); + } + + /** + * @inheritDoc + * + * @param string $app id of the app + * + * @since 31.0.0 + */ + public function deleteApp(string $app): void { + $this->assertParams('', $app, allowEmptyUser: true); + $qb = $this->connection->getQueryBuilder(); + $qb->delete('preferences') + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); + $qb->executeStatement(); + + $this->clearCacheAll(); + } + + public function deleteAllPreferences(string $userId): void { + $this->assertParams($userId, '', allowEmptyApp: true); + $qb = $this->connection->getQueryBuilder(); + $qb->delete('preferences') + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); + $qb->executeStatement(); + + $this->clearCache($userId); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param bool $reload set to TRUE to refill cache instantly after clearing it. + * + * @since 31.0.0 + */ + public function clearCache(string $userId, bool $reload = false): void { + $this->assertParams($userId, allowEmptyApp: true); + $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false; + $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueTypes[$userId] = []; + + if (!$reload) { + return; + } + + $this->loadPreferencesAll($userId); + } + + /** + * @inheritDoc + * + * @since 31.0.0 + */ + public function clearCacheAll(): void { + $this->lazyLoaded = $this->fastLoaded = []; + $this->lazyCache = $this->fastCache = $this->valueTypes = []; + } + + /** + * For debug purpose. + * Returns the cached data. + * + * @return array + * @since 31.0.0 + * @internal + */ + public function statusCache(): array { + return [ + 'fastLoaded' => $this->fastLoaded, + 'fastCache' => $this->fastCache, + 'lazyLoaded' => $this->lazyLoaded, + 'lazyCache' => $this->lazyCache, + 'valueTypes' => $this->valueTypes, + ]; + } + + /** + * @param ValueType $needle bitflag to search + * @param int $type known value + * + * @return bool TRUE if bitflag $needle is set in $type + */ + private function isTyped(ValueType $needle, int $type): bool { + return (($needle->value & $type) !== 0); + } + + /** + * Confirm the string set for app and key fit the database description + * + * @param string $userId + * @param string $app assert $app fit in database + * @param string $prefKey assert preference key fit in database + * @param bool $allowEmptyUser + * @param bool $allowEmptyApp $app can be empty string + * @param ValueType|null $valueType assert value type is only one type + */ + private function assertParams( + string $userId = '', + string $app = '', + string $prefKey = '', + bool $allowEmptyUser = false, + bool $allowEmptyApp = false, + ?ValueType $valueType = null, + ): void { + if (!$allowEmptyUser && $userId === '') { + throw new InvalidArgumentException('userId cannot be an empty string'); + } + if (!$allowEmptyApp && $app === '') { + throw new InvalidArgumentException('app cannot be an empty string'); + } + if (strlen($userId) > self::USER_MAX_LENGTH) { + throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')'); + } + if (strlen($app) > self::APP_MAX_LENGTH) { + throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')'); + } + if (strlen($prefKey) > self::KEY_MAX_LENGTH) { + throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); + } + if ($valueType !== null) { + $valueFlag = $valueType->value; + $valueFlag &= ~ValueType::SENSITIVE->value; + if (ValueType::tryFrom($valueFlag) === null) { + throw new InvalidArgumentException('Unknown value type'); + } + } + } + + private function loadPreferencesAll(string $userId): void { + $this->loadPreferences($userId, null); + } + + /** + * Load normal preferences or preferences set as lazy loaded + * + * @param bool|null $lazy set to TRUE to load preferences set as lazy loaded, set to NULL to load all preferences + */ + private function loadPreferences(string $userId, ?bool $lazy = false): void { + if ($this->isLoaded($userId, $lazy)) { + return; + } + + if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log + $this->logger->debug('The loading of lazy UserPreferences values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); + } + + $qb = $this->connection->getQueryBuilder(); + $qb->from('preferences'); + $qb->select('userid', 'appid', 'configkey', 'configvalue', 'type'); + $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); + + // we only need value from lazy when loadPreferences does not specify it + if ($lazy !== null) { + $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); + } else { + $qb->addSelect('lazy'); + } + + $result = $qb->executeQuery(); + $rows = $result->fetchAll(); + foreach ($rows as $row) { + if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { + $this->lazyCache[$row['userid']][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; + } else { + $this->fastCache[$row['userid']][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; + } + $this->valueTypes[$row['userid']][$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0); + } + $result->closeCursor(); + $this->setAsLoaded($userId, $lazy); + } + + /** + * if $lazy is: + * - false: will returns true if fast preferences are loaded + * - true : will returns true if lazy preferences are loaded + * - null : will returns true if both preferences are loaded + * + * @param string $userId + * @param bool $lazy + * + * @return bool + */ + private function isLoaded(string $userId, ?bool $lazy): bool { + if ($lazy === null) { + return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false); + } + + return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false; + } + + /** + * if $lazy is: + * - false: set fast preferences as loaded + * - true : set lazy preferences as loaded + * - null : set both preferences as loaded + * + * @param string $userId + * @param bool $lazy + */ + private function setAsLoaded(string $userId, ?bool $lazy): void { + if ($lazy === null) { + $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true; + return; + } + + // We also create empty entry to keep both fastLoaded/lazyLoaded synced + if ($lazy) { + $this->lazyLoaded[$userId] = true; + $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false; + $this->fastCache[$userId] = $this->fastCache[$userId] ?? []; + } else { + $this->fastLoaded[$userId] = true; + $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false; + $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? []; + } + } + + /** + * **Warning:** this will load all lazy values from the database + * + * @param string $userId id of the user + * @param string $app id of the app + * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} + * + * @return array + */ + private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array { + foreach ($values as $key => $value) { + //$key = (string)$key; + try { + $type = $this->getValueType($userId, $app, (string)$key); + } catch (UnknownKeyException) { + continue; + } + + if ($this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key] ?? 0)) { + if ($filtered) { + $value = IConfig::SENSITIVE_VALUE; + $type = ValueType::STRING; + } else { + $this->decryptSensitiveValue($userId, $app, (string)$key, $value); + } + } + + $values[$key] = $this->convertTypedValue($value, $type); + } + + return $values; + } + + /** + * convert string value to the expected type + * + * @param string $value + * @param ValueType $type + * + * @return string|int|float|bool|array + */ + private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array { + switch ($type) { + case ValueType::INT: + return (int)$value; + case ValueType::FLOAT: + return (float)$value; + case ValueType::BOOL: + return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); + case ValueType::ARRAY: + try { + return json_decode($value, true, flags: JSON_THROW_ON_ERROR); + } catch (JsonException) { + // ignoreable + } + break; + } + return $value; + } + + + private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void { + if (!$this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key] ?? 0)) { + return; + } + + if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) { + return; + } + + try { + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); + } catch (\Exception $e) { + $this->logger->warning('could not decrypt sensitive value', [ + 'userId' => $userId, + 'app' => $app, + 'key' => $key, + 'value' => $value, + 'exception' => $e + ]); + } + } +} diff --git a/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php b/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php new file mode 100644 index 0000000000000..5c8f83dee5efe --- /dev/null +++ b/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php @@ -0,0 +1,15 @@ + list of userIds + * @since 31.0.0 + */ + public function getUserIds(string $appId = ''): array; + + /** + * Get list of all apps that have at least one preference + * value related to $userId stored in database + * + * **WARNING:** ignore lazy filtering, all user preferences are loaded from database + * + * @param string $userId id of the user + * + * @return list list of app ids + * @since 31.0.0 + */ + public function getApps(string $userId): array; + + /** + * Returns all keys stored in database, related to user+app. + * Please note that the values are not returned. + * + * **WARNING:** ignore lazy filtering, all user preferences are loaded from database + * + * @param string $userId id of the user + * @param string $app id of the app + * + * @return list list of stored preference keys + * @since 31.0.0 + */ + public function getKeys(string $userId, string $app): array; + + /** + * Check if a key exists in the list of stored preference values. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy search within lazy loaded preferences + * + * @return bool TRUE if key exists + * @since 31.0.0 + */ + public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool; + + /** + * best way to see if a value is set as sensitive (not displayed in report) + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool|null $lazy search within lazy loaded preferences + * + * @return bool TRUE if value is sensitive + * @throws UnknownKeyException if preference key is not known + * @since 31.0.0 + */ + public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool; + + /** + * Returns if the preference key stored in database is lazy loaded + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * + * @return bool TRUE if preference is lazy loaded + * @throws UnknownKeyException if preference key is not known + * @see IUserPreferences for details about lazy loading + * @since 31.0.0 + */ + public function isLazy(string $userId, string $app, string $key): bool; + + /** + * List all preference values from an app with preference key starting with $key. + * Returns an array with preference key as key, stored value as value. + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $prefix preference keys prefix to search, can be empty. + * @param bool $filtered filter sensitive preference values + * + * @return array [key => value] + * @since 31.0.0 + */ + public function getValues(string $userId, string $app, string $prefix = '', bool $filtered = false): array; + + /** + * List all preference values of a user. + * Returns an array with preference key as key, stored value as value. + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * + * @param string $userId id of the user + * @param bool $filtered filter sensitive preference values + * + * @return array [key => value] + * @since 31.0.0 + */ + public function getAllValues(string $userId, bool $filtered = false): array; + + /** + * List all apps storing a specific preference key and its stored value. + * Returns an array with appId as key, stored value as value. + * + * @param string $userId id of the user + * @param string $key preference key + * @param bool $lazy search within lazy loaded preferences + * @param ValueType|null $typedAs enforce type for the returned values + * + * @return array [appId => value] + * @since 31.0.0 + */ + public function searchValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array; + + /** + * List all users storing a specific preference key and its stored value. + * Returns an array with userId as key, stored value as value. + * + * **WARNING:** no caching, generate a fresh request + * + * @param string $app id of the app + * @param string $key preference key + * @param ValueType|null $typedAs enforce type for the returned values + * @param array|null $userIds limit the search to a list of user ids + * + * @return array [userId => value] + * @since 31.0.0 + */ + public function searchValuesByUsers(string $app, string $key, ?ValueType $typedAs = null, ?array $userIds = null): array; + + /** + * List all users storing a specific preference key/value pair. + * Returns a list of user ids. + * + * **WARNING:** no caching, generate a fresh request + * + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): array; + + /** + * List all users storing a specific preference key/value pair. + * Returns a list of user ids. + * + * **WARNING:** no caching, generate a fresh request + * + * @param string $app id of the app + * @param string $key preference key + * @param int $value preference value + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueInt(string $app, string $key, int $value): array; + + /** + * List all users storing a specific preference key/value pair. + * Returns a list of user ids. + * + * **WARNING:** no caching, generate a fresh request + * + * @param string $app id of the app + * @param string $key preference key + * @param array $values list of possible preference values + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValues(string $app, string $key, array $values): array; + + /** + * List all users storing a specific preference key/value pair. + * Returns a list of user ids. + * + * **WARNING:** no caching, generate a fresh request + * + * @param string $app id of the app + * @param string $key preference key + * @param bool $value preference value + * + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueBool(string $app, string $key, bool $value): array; + + /** + * Get user preference assigned to a preference key. + * If preference key is not found in database, default value is returned. + * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return string stored preference value or $default if not set in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see getValueInt() + * @see getValueFloat() + * @see getValueBool() + * @see getValueArray() + */ + public function getValueString(string $userId, string $app, string $key, string $default = '', bool $lazy = false): string; + + /** + * Get preference value assigned to a preference key. + * If preference key is not found in database, default value is returned. + * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param int $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return int stored preference value or $default if not set in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see getValueString() + * @see getValueFloat() + * @see getValueBool() + * @see getValueArray() + */ + public function getValueInt(string $userId, string $app, string $key, int $default = 0, bool $lazy = false): int; + + /** + * Get preference value assigned to a preference key. + * If preference key is not found in database, default value is returned. + * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param float $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return float stored preference value or $default if not set in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see getValueString() + * @see getValueInt() + * @see getValueBool() + * @see getValueArray() + */ + public function getValueFloat(string $userId, string $app, string $key, float $default = 0, bool $lazy = false): float; + + /** + * Get preference value assigned to a preference key. + * If preference key is not found in database, default value is returned. + * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $default default value + * @param bool $lazy search within lazy loaded preferences + * + * @return bool stored preference value or $default if not set in database + * @since 31.0.0 + * @see IUserPrefences for explanation about lazy loading + * @see getValueString() + * @see getValueInt() + * @see getValueFloat() + * @see getValueArray() + */ + public function getValueBool(string $userId, string $app, string $key, bool $default = false, bool $lazy = false): bool; + + /** + * Get preference value assigned to a preference key. + * If preference key is not found in database, default value is returned. + * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param array $default default value` + * @param bool $lazy search within lazy loaded preferences + * + * @return array stored preference value or $default if not set in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see getValueString() + * @see getValueInt() + * @see getValueFloat() + * @see getValueBool() + */ + public function getValueArray(string $userId, string $app, string $key, array $default = [], bool $lazy = false): array; + + /** + * returns the type of preference value + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * unless lazy is set to false + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool|null $lazy + * + * @return ValueType type of the value + * @throws UnknownKeyException if preference key is not known + * @throws IncorrectTypeException if preferences value type is not known + * @since 31.0.0 + */ + public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType; + + /** + * Store a preference key and its value in database + * + * If preference key is already known with the exact same preference value, the database is not updated. + * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * + * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param bool $lazy set preference as lazy loaded + * + * @return bool TRUE if value was different, therefor updated in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see setValueInt() + * @see setValueFloat() + * @see setValueBool() + * @see setValueArray() + */ + public function setValueString(string $userId, string $app, string $key, string $value, bool $lazy = false, bool $sensitive = false): bool; + + /** + * Store a preference key and its value in database + * + * When handling huge value around and/or above 2,147,483,647, a debug log will be generated + * on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers. + * + * When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()} + * + * If preference key is already known with the exact same preference value, the database is not updated. + * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * + * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param int $value preference value + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param bool $lazy set preference as lazy loaded + * + * @return bool TRUE if value was different, therefor updated in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see setValueString() + * @see setValueFloat() + * @see setValueBool() + * @see setValueArray() + */ + public function setValueInt(string $userId, string $app, string $key, int $value, bool $lazy = false, bool $sensitive = false): bool; + + /** + * Store a preference key and its value in database. + * + * If preference key is already known with the exact same preference value, the database is not updated. + * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * + * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param float $value preference value + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param bool $lazy set preference as lazy loaded + * + * @return bool TRUE if value was different, therefor updated in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see setValueString() + * @see setValueInt() + * @see setValueBool() + * @see setValueArray() + */ + public function setValueFloat(string $userId, string $app, string $key, float $value, bool $lazy = false, bool $sensitive = false): bool; + + /** + * Store a preference key and its value in database + * + * If preference key is already known with the exact same preference value, the database is not updated. + * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * + * If preference value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $value preference value + * @param bool $lazy set preference as lazy loaded + * + * @return bool TRUE if value was different, therefor updated in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see setValueString() + * @see setValueInt() + * @see setValueFloat() + * @see setValueArray() + */ + public function setValueBool(string $userId, string $app, string $key, bool $value, bool $lazy = false): bool; + + /** + * Store a preference key and its value in database + * + * If preference key is already known with the exact same preference value, the database is not updated. + * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * + * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param array $value preference value + * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param bool $lazy set preference as lazy loaded + * + * @return bool TRUE if value was different, therefor updated in database + * @since 31.0.0 + * @see IUserPreferences for explanation about lazy loading + * @see setValueString() + * @see setValueInt() + * @see setValueFloat() + * @see setValueBool() + */ + public function setValueArray(string $userId, string $app, string $key, array $value, bool $lazy = false, bool $sensitive = false): bool; + + /** + * switch sensitive status of a preference value + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset + * + * @return bool TRUE if database update were necessary + * @since 31.0.0 + */ + public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool; + + /** + * switch sensitive loading status of a preference key for all users + * + * **Warning:** heavy on resources, MUST only be used on occ command or migrations + * + * + * @param string $app id of the app + * @param string $key preference key + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset + * + * @since 31.0.0 + */ + public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void; + + /** + * switch lazy loading status of a preference value + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset + * + * @return bool TRUE if database update was necessary + * @since 31.0.0 + */ + public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool; + + /** + * switch lazy loading status of a preference key for all users + * + * **Warning:** heavy on resources, MUST only be used on occ command or migrations + * + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset + * @since 31.0.0 + */ + public function updateGlobalLazy(string $app, string $key, bool $lazy): void; + + /** + * returns an array contains details about a preference value + * + * ``` + * [ + * "app" => "myapp", + * "key" => "mykey", + * "value" => "its_value", + * "lazy" => false, + * "type" => 4, + * "typeString" => "string", + * 'sensitive' => true + * ] + * ``` + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * + * @return array + * @throws UnknownKeyException if preference key is not known in database + * @since 31.0.0 + */ + public function getDetails(string $userId, string $app, string $key): array; + + /** + * Delete single preference key from database. + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * + * @since 31.0.0 + */ + public function deletePreference(string $userId, string $app, string $key): void; + + /** + * Delete preference values from all users linked to a specific preference keys + * + * @param string $app id of the app + * @param string $key preference key + * + * @since 31.0.0 + */ + public function deleteKey(string $app, string $key): void; + + /** + * delete all preference keys linked to an app + * + * @param string $app id of the app + * @since 31.0.0 + */ + public function deleteApp(string $app): void; + + /** + * delete all preference keys linked to a user + * + * @param string $userId id of the user + * @since 31.0.0 + */ + public function deleteAllPreferences(string $userId): void; + + /** + * Clear the cache for a single user + * + * The cache will be rebuilt only the next time a user preference is requested. + * + * @param string $userId id of the user + * @param bool $reload set to TRUE to refill cache instantly after clearing it + * + * @since 31.0.0 + */ + public function clearCache(string $userId, bool $reload = false): void; + + /** + * Clear the cache for all users. + * The cache will be rebuilt only the next time a user preference is requested. + * + * @since 31.0.0 + */ + public function clearCacheAll(): void; +} diff --git a/lib/public/UserPreferences/ValueType.php b/lib/public/UserPreferences/ValueType.php new file mode 100644 index 0000000000000..57f65b504c7ff --- /dev/null +++ b/lib/public/UserPreferences/ValueType.php @@ -0,0 +1,111 @@ +fromValueDefinition(ValueTypeDefinition::from($definition)); + } catch (ValueError) { + throw new IncorrectTypeException('unknown string definition'); + } + } + + /** + * get ValueType from ValueTypeDefinition + * + * @param ValueTypeDefinition $definition + * + * @return self + * @throws IncorrectTypeException + * @since 31.0.0 + */ + public function fromValueDefinition(ValueTypeDefinition $definition): self { + try { + return match ($definition) { + ValueTypeDefinition::MIXED => self::MIXED, + ValueTypeDefinition::STRING => self::STRING, + ValueTypeDefinition::INT => self::INT, + ValueTypeDefinition::FLOAT => self::FLOAT, + ValueTypeDefinition::BOOL => self::BOOL, + ValueTypeDefinition::ARRAY => self::ARRAY + }; + } catch (UnhandledMatchError) { + throw new IncorrectTypeException('unknown definition ' . $definition->value); + } + } + + /** + * get string definition for current enum value + * + * @return string + * @throws IncorrectTypeException + * @since 31.0.0 + */ + public function getDefinition(): string { + return $this->getValueTypeDefinition()->value; + } + + /** + * get ValueTypeDefinition for current enum value + * + * @return ValueTypeDefinition + * @throws IncorrectTypeException + * @since 31.0.0 + */ + public function getValueTypeDefinition(): ValueTypeDefinition { + try { + /** @psalm-suppress UnhandledMatchCondition */ + return match ($this) { + self::MIXED => ValueTypeDefinition::MIXED, + self::STRING => ValueTypeDefinition::STRING, + self::INT => ValueTypeDefinition::INT, + self::FLOAT => ValueTypeDefinition::FLOAT, + self::BOOL => ValueTypeDefinition::BOOL, + self::ARRAY => ValueTypeDefinition::ARRAY, + }; + } catch (UnhandledMatchError) { + throw new IncorrectTypeException('unknown type definition ' . $this->value); + } + } +} diff --git a/lib/public/UserPreferences/ValueTypeDefinition.php b/lib/public/UserPreferences/ValueTypeDefinition.php new file mode 100644 index 0000000000000..116c20b3bafb0 --- /dev/null +++ b/lib/public/UserPreferences/ValueTypeDefinition.php @@ -0,0 +1,30 @@ +connection->executeUpdate( @@ -328,7 +328,7 @@ public function testGetUserKeysAllInts(): void { ); } - $value = $config->getUserKeys('userFetch', 'appFetch1'); + $value = $config->getUserKeys('userFetch8', 'appFetch1'); $this->assertEquals(['123', '456'], $value); $this->assertIsString($value[0]); $this->assertIsString($value[1]); diff --git a/tests/lib/UserPreferencesTest.php b/tests/lib/UserPreferencesTest.php new file mode 100644 index 0000000000000..b7e1c229bcba0 --- /dev/null +++ b/tests/lib/UserPreferencesTest.php @@ -0,0 +1,1833 @@ +>> [userId => [appId => prefKey, prefValue, valueType, lazy, sensitive]]] + */ + private array $basePreferences = + [ + 'user1' => + [ + 'app1' => [ + 'key1' => ['key1', 'value1'], + 'key22' => ['key22', '31'], + 'fast_string' => ['fast_string', 'f_value', ValueType::STRING], + 'lazy_string' => ['lazy_string', 'l_value', ValueType::STRING, true], + 'fast_string_sensitive' => [ + 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, true + ], + 'lazy_string_sensitive' => [ + 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, true + ], + 'fast_int' => ['fast_int', 11, ValueType::INT], + 'lazy_int' => ['lazy_int', 12, ValueType::INT, true], + 'fast_int_sensitive' => ['fast_int_sensitive', 2024, ValueType::INT, false, true], + 'lazy_int_sensitive' => ['lazy_int_sensitive', 2048, ValueType::INT, true, true], + 'fast_float' => ['fast_float', 3.14, ValueType::FLOAT], + 'lazy_float' => ['lazy_float', 3.14159, ValueType::FLOAT, true], + 'fast_float_sensitive' => [ + 'fast_float_sensitive', 1.41, ValueType::FLOAT, false, true + ], + 'lazy_float_sensitive' => [ + 'lazy_float_sensitive', 1.4142, ValueType::FLOAT, true, true + ], + 'fast_array' => ['fast_array', ['year' => 2024], ValueType::ARRAY], + 'lazy_array' => ['lazy_array', ['month' => 'October'], ValueType::ARRAY, true], + 'fast_array_sensitive' => [ + 'fast_array_sensitive', ['password' => 'pwd'], ValueType::ARRAY, false, true + ], + 'lazy_array_sensitive' => [ + 'lazy_array_sensitive', ['password' => 'qwerty'], ValueType::ARRAY, true, true + ], + 'fast_boolean' => ['fast_boolean', true, ValueType::BOOL], + 'fast_boolean_0' => ['fast_boolean_0', false, ValueType::BOOL], + 'lazy_boolean' => ['lazy_boolean', true, ValueType::BOOL, true], + 'lazy_boolean_0' => ['lazy_boolean_0', false, ValueType::BOOL, true], + ], + 'app2' => [ + 'key2' => ['key2', 'value2a', ValueType::STRING], + 'key3' => ['key3', 'value3', ValueType::STRING, true, false], + 'key4' => ['key4', 'value4', ValueType::STRING, false, true], + 'key8' => ['key8', 11, ValueType::INT], + 'key9' => ['key9', 'value9a', ValueType::STRING], + ], + 'app3' => [ + 'key1' => ['key1', 'value123'], + 'key3' => ['key3', 'value3'], + 'key8' => ['key8', 12, ValueType::INT, false, true], + 'key9' => ['key9', 'value9b', ValueType::STRING, false, true], + 'key10' => ['key10', true, ValueType::BOOL], + ], + 'only-lazy' => [ + 'key1' => ['key1', 'value456', ValueType::STRING, true], + 'key2' => ['key2', 'value2c', ValueType::STRING, true, true], + 'key3' => ['key3', 42, ValueType::INT, true], + 'key4' => ['key4', 17.42, ValueType::FLOAT, true], + 'key5' => ['key5', true, ValueType::BOOL, true], + ] + ], + 'user2' => + [ + 'app1' => [ + '1' => ['1', 'value1'], + '2' => ['2', 'value2', ValueType::STRING, true, true], + '3' => ['3', 17, ValueType::INT, true], + '4' => ['4', 42, ValueType::INT, false, true], + '5' => ['5', 17.42, ValueType::FLOAT, false], + '6' => ['6', true, ValueType::BOOL, false], + ], + 'app2' => [ + 'key2' => ['key2', 'value2b', ValueType::STRING], + 'key3' => ['key3', 'value3', ValueType::STRING, true, false], + 'key4' => ['key4', 'value4', ValueType::STRING, false, true], + 'key8' => ['key8', 12, ValueType::INT], + ], + 'app3' => [ + 'key10' => ['key10', false, ValueType::BOOL], + ], + 'only-lazy' => [ + 'key1' => ['key1', 'value1', ValueType::STRING, true] + ] + ], + 'user3' => + [ + 'app2' => [ + 'key2' => ['key2', 'value2c'], + 'key3' => ['key3', 'value3', ValueType::STRING, true, false], + 'key4' => ['key4', 'value4', ValueType::STRING, false, true], + 'fast_string_sensitive' => [ + 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, true + ], + 'lazy_string_sensitive' => [ + 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, true + ], + ], + 'only-lazy' => [ + 'key3' => ['key3', 'value3', ValueType::STRING, true] + ] + ], + 'user4' => + [ + 'app2' => [ + 'key1' => ['key1', 'value1'], + 'key2' => ['key2', 'value2A'], + 'key3' => ['key3', 'value3', ValueType::STRING, true, false], + 'key4' => ['key4', 'value4', ValueType::STRING, false, true], + ], + 'app3' => [ + 'key10' => ['key10', true, ValueType::BOOL], + ], + 'only-lazy' => [ + 'key1' => ['key1', 123, ValueType::INT, true] + ] + ], + 'user5' => + [ + 'app1' => [ + 'key1' => ['key1', 'value1'] + ], + 'app2' => [ + 'key8' => ['key8', 12, ValueType::INT] + ], + 'only-lazy' => [ + 'key1' => ['key1', 'value1', ValueType::STRING, true] + ] + ], + + ]; + + protected function setUp(): void { + parent::setUp(); + + $this->connection = \OCP\Server::get(IDBConnection::class); + $this->logger = \OCP\Server::get(LoggerInterface::class); + $this->crypto = \OCP\Server::get(ICrypto::class); + + // storing current preferences and emptying the data table + $sql = $this->connection->getQueryBuilder(); + $sql->select('*') + ->from('preferences'); + $result = $sql->executeQuery(); + $this->originalPreferences = $result->fetchAll(); + $result->closeCursor(); + + $sql = $this->connection->getQueryBuilder(); + $sql->delete('preferences'); + $sql->executeStatement(); + + $sql = $this->connection->getQueryBuilder(); + $sql->insert('preferences') + ->values( + [ + 'userid' => $sql->createParameter('userid'), + 'appid' => $sql->createParameter('appid'), + 'configkey' => $sql->createParameter('configkey'), + 'configvalue' => $sql->createParameter('configvalue'), + 'type' => $sql->createParameter('type'), + 'lazy' => $sql->createParameter('lazy') + ] + ); + + foreach ($this->basePreferences as $userId => $userData) { + foreach ($userData as $appId => $appData) { + foreach ($appData as $key => $row) { + $value = $row[1]; + $type = ($row[2] ?? ValueType::MIXED)->value; + + if ($type === ValueType::ARRAY->value) { + $value = json_encode($value); + } + + if (($row[4] ?? false) === true) { + $type |= ValueType::SENSITIVE->value; + $value = self::invokePrivate(UserPreferences::class, 'ENCRYPTION_PREFIX') + . $this->crypto->encrypt((string)$value); + $this->basePreferences[$userId][$appId][$key]['encrypted'] = $value; + } + + if ($type === ValueType::BOOL->value && $value === false) { + $value = '0'; + } + + $sql->setParameters( + [ + 'userid' => $userId, + 'appid' => $appId, + 'configkey' => $row[0], + 'configvalue' => $value, + 'type' => $type, + 'lazy' => (($row[3] ?? false) === true) ? 1 : 0 + ] + )->executeStatement(); + } + } + } + } + + protected function tearDown(): void { + $sql = $this->connection->getQueryBuilder(); + $sql->delete('preferences'); + $sql->executeStatement(); + + $sql = $this->connection->getQueryBuilder(); + $sql->insert('preferences') + ->values( + [ + 'userid' => $sql->createParameter('userid'), + 'appid' => $sql->createParameter('appid'), + 'configkey' => $sql->createParameter('configkey'), + 'configvalue' => $sql->createParameter('configvalue'), + 'lazy' => $sql->createParameter('lazy'), + 'type' => $sql->createParameter('type'), + ] + ); + + foreach ($this->originalPreferences as $key => $configs) { + $sql->setParameter('userid', $configs['userid']) + ->setParameter('appid', $configs['appid']) + ->setParameter('configkey', $configs['configkey']) + ->setParameter('configvalue', $configs['configvalue']) + ->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0') + ->setParameter('type', $configs['type']); + $sql->executeStatement(); + } + + parent::tearDown(); + } + + /** + * @param array $preLoading preload the 'fast' cache for a list of users) + * + * @return IUserPreferences + */ + private function generateUserPreferences(array $preLoading = []): IUserPreferences { + $preferences = new \OC\UserPreferences( + $this->connection, + $this->logger, + $this->crypto, + ); + $msg = ' generateUserPreferences() failed to confirm cache status'; + + // confirm cache status + $status = $preferences->statusCache(); + $this->assertSame([], $status['fastLoaded'], $msg); + $this->assertSame([], $status['lazyLoaded'], $msg); + $this->assertSame([], $status['fastCache'], $msg); + $this->assertSame([], $status['lazyCache'], $msg); + foreach ($preLoading as $preLoadUser) { + // simple way to initiate the load of non-lazy preferences values in cache + $preferences->getValueString($preLoadUser, 'core', 'preload'); + + // confirm cache status + $status = $preferences->statusCache(); + $this->assertSame(true, $status['fastLoaded'][$preLoadUser], $msg); + $this->assertSame(false, $status['lazyLoaded'][$preLoadUser], $msg); + + $apps = array_values(array_diff(array_keys($this->basePreferences[$preLoadUser]), ['only-lazy'])); + $this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache'][$preLoadUser]), $msg); + $this->assertSame([], array_keys($status['lazyCache'][$preLoadUser]), $msg); + } + + return $preferences; + } + + public function testGetUserIdsEmpty(): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing(array_keys($this->basePreferences), $preferences->getUserIds()); + } + + public function testGetUserIds(): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing(['user1', 'user2', 'user5'], $preferences->getUserIds('app1')); + } + + public function testGetApps(): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing( + array_keys($this->basePreferences['user1']), $preferences->getApps('user1') + ); + } + + public function testGetKeys(): void { + $preferences = $this->generateUserPreferences(['user1']); + $this->assertEqualsCanonicalizing( + array_keys($this->basePreferences['user1']['app1']), $preferences->getKeys('user1', 'app1') + ); + } + + /** + * @return array[] + */ + public function providerHasKey(): array { + return [ + ['user1', 'app1', 'key1', false, true], + ['user0', 'app1', 'key1', false, false], + ['user1', 'app1', 'key1', true, false], + ['user1', 'app1', 'key0', false, false], + ['user1', 'app1', 'key0', true, false], + ['user1', 'app1', 'fast_string_sensitive', false, true], + ['user1', 'app1', 'lazy_string_sensitive', true, true], + ['user2', 'only-lazy', 'key1', false, false], + ['user2', 'only-lazy', 'key1', true, true], + ]; + } + + /** + * @dataProvider providerHasKey + */ + public function testHasKey(string $userId, string $appId, string $key, ?bool $lazy, bool $result): void { + $preferences = $this->generateUserPreferences(); + $this->assertEquals($result, $preferences->hasKey($userId, $appId, $key, $lazy)); + } + + /** + * @return array[] + */ + public function providerIsSensitive(): array { + return [ + ['user1', 'app1', 'key1', false, false, false], + ['user0', 'app1', 'key1', false, false, true], + ['user1', 'app1', 'key1', true, false, true], + ['user1', 'app1', 'key1', null, false, false], + ['user1', 'app1', 'key0', false, false, true], + ['user1', 'app1', 'key0', true, false, true], + ['user1', 'app1', 'fast_string_sensitive', false, true, false], + ['user1', 'app1', 'lazy_string_sensitive', true, true, false], + ['user1', 'app1', 'fast_string_sensitive', true, true, true], + ['user1', 'app1', 'lazy_string_sensitive', false, true, true], + ['user1', 'app1', 'lazy_string_sensitive', null, true, false], + ['user2', 'only-lazy', 'key1', false, false, true], + ['user2', 'only-lazy', 'key1', true, false, false], + ['user2', 'only-lazy', 'key1', null, false, false], + ]; + } + + /** + * @dataProvider providerIsSensitive + */ + public function testIsSensitive( + string $userId, + string $appId, + string $key, + ?bool $lazy, + bool $result, + bool $exception, + ): void { + $preferences = $this->generateUserPreferences(); + if ($exception) { + $this->expectException(UnknownKeyException::class); + } + + $this->assertEquals($result, $preferences->isSensitive($userId, $appId, $key, $lazy)); + } + + /** + * @return array[] + */ + public function providerIsLazy(): array { + return [ + ['user1', 'app1', 'key1', false, false], + ['user0', 'app1', 'key1', false, true], + ['user1', 'app1', 'key0', false, true], + ['user1', 'app1', 'key0', false, true], + ['user1', 'app1', 'fast_string_sensitive', false, false], + ['user1', 'app1', 'lazy_string_sensitive', true, false], + ['user2', 'only-lazy', 'key1', true, false], + ]; + } + + /** + * @dataProvider providerIsLazy + */ + public function testIsLazy( + string $userId, + string $appId, + string $key, + bool $result, + bool $exception, + ): void { + $preferences = $this->generateUserPreferences(); + if ($exception) { + $this->expectException(UnknownKeyException::class); + } + + $this->assertEquals($result, $preferences->isLazy($userId, $appId, $key)); + } + + public function providerGetValues(): array { + return [ + [ + 'user1', 'app1', '', true, + [ + 'fast_array' => ['year' => 2024], + 'fast_array_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'fast_boolean' => true, + 'fast_boolean_0' => false, + 'fast_float' => 3.14, + 'fast_float_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'fast_int' => 11, + 'fast_int_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'fast_string' => 'f_value', + 'fast_string_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'key1' => 'value1', + 'key22' => '31', + 'lazy_array' => ['month' => 'October'], + 'lazy_array_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'lazy_boolean' => true, + 'lazy_boolean_0' => false, + 'lazy_float' => 3.14159, + 'lazy_float_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'lazy_int' => 12, + 'lazy_int_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'lazy_string' => 'l_value', + 'lazy_string_sensitive' => '***REMOVED SENSITIVE VALUE***', + ] + ], + [ + 'user1', 'app1', '', false, + [ + 'fast_array' => ['year' => 2024], + 'fast_array_sensitive' => ['password' => 'pwd'], + 'fast_boolean' => true, + 'fast_boolean_0' => false, + 'fast_float' => 3.14, + 'fast_float_sensitive' => 1.41, + 'fast_int' => 11, + 'fast_int_sensitive' => 2024, + 'fast_string' => 'f_value', + 'fast_string_sensitive' => 'fs_value', + 'key1' => 'value1', + 'key22' => '31', + 'lazy_array' => ['month' => 'October'], + 'lazy_array_sensitive' => ['password' => 'qwerty'], + 'lazy_boolean' => true, + 'lazy_boolean_0' => false, + 'lazy_float' => 3.14159, + 'lazy_float_sensitive' => 1.4142, + 'lazy_int' => 12, + 'lazy_int_sensitive' => 2048, + 'lazy_string' => 'l_value', + 'lazy_string_sensitive' => 'ls_value' + ] + ], + [ + 'user1', 'app1', 'fast_', true, + [ + 'fast_array' => ['year' => 2024], + 'fast_array_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'fast_boolean' => true, + 'fast_boolean_0' => false, + 'fast_float' => 3.14, + 'fast_float_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'fast_int' => 11, + 'fast_int_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'fast_string' => 'f_value', + 'fast_string_sensitive' => '***REMOVED SENSITIVE VALUE***', + ] + ], + [ + 'user1', 'app1', 'fast_', false, + [ + 'fast_array' => ['year' => 2024], + 'fast_array_sensitive' => ['password' => 'pwd'], + 'fast_boolean' => true, + 'fast_boolean_0' => false, + 'fast_float' => 3.14, + 'fast_float_sensitive' => 1.41, + 'fast_int' => 11, + 'fast_int_sensitive' => 2024, + 'fast_string' => 'f_value', + 'fast_string_sensitive' => 'fs_value', + ] + ], + [ + 'user1', 'app1', 'key1', true, + [ + 'key1' => 'value1', + ] + ], + [ + 'user2', 'app1', '', false, + [ + '1' => 'value1', + '4' => 42, + '5' => 17.42, + '6' => true, + '2' => 'value2', + '3' => 17, + ] + ], + [ + 'user2', 'app1', '', true, + [ + '1' => 'value1', + '4' => '***REMOVED SENSITIVE VALUE***', + '5' => 17.42, + '6' => true, + '2' => '***REMOVED SENSITIVE VALUE***', + '3' => 17, + ] + ], + ]; + } + + /** + * @dataProvider providerGetValues + */ + public function testGetValues( + string $userId, + string $appId, + string $prefix, + bool $filtered, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertJsonStringEqualsJsonString( + json_encode($result), json_encode($preferences->getValues($userId, $appId, $prefix, $filtered)) + ); + } + + public function providerGetAllValues(): array { + return [ + [ + 'user2', false, + [ + 'app1' => [ + '1' => 'value1', + '4' => 42, + '5' => 17.42, + '6' => true, + '2' => 'value2', + '3' => 17, + ], + 'app2' => [ + 'key2' => 'value2b', + 'key3' => 'value3', + 'key4' => 'value4', + 'key8' => 12, + ], + 'app3' => [ + 'key10' => false, + ], + 'only-lazy' => [ + 'key1' => 'value1', + ] + ], + ], + [ + 'user2', true, + [ + 'app1' => [ + '1' => 'value1', + '4' => '***REMOVED SENSITIVE VALUE***', + '5' => 17.42, + '6' => true, + '2' => '***REMOVED SENSITIVE VALUE***', + '3' => 17, + ], + 'app2' => [ + 'key2' => 'value2b', + 'key3' => 'value3', + 'key4' => '***REMOVED SENSITIVE VALUE***', + 'key8' => 12, + ], + 'app3' => [ + 'key10' => false, + ], + 'only-lazy' => [ + 'key1' => 'value1', + ] + ], + ], + [ + 'user3', true, + [ + 'app2' => [ + 'key2' => 'value2c', + 'key3' => 'value3', + 'key4' => '***REMOVED SENSITIVE VALUE***', + 'fast_string_sensitive' => '***REMOVED SENSITIVE VALUE***', + 'lazy_string_sensitive' => '***REMOVED SENSITIVE VALUE***', + ], + 'only-lazy' => [ + 'key3' => 'value3', + ] + ], + ], + [ + 'user3', false, + [ + 'app2' => [ + 'key2' => 'value2c', + 'key3' => 'value3', + 'key4' => 'value4', + 'fast_string_sensitive' => 'fs_value', + 'lazy_string_sensitive' => 'ls_value', + ], + 'only-lazy' => [ + 'key3' => 'value3', + ] + ], + ], + ]; + } + + /** + * @dataProvider providerGetAllValues + */ + public function testGetAllValues( + string $userId, + bool $filtered, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing($result, $preferences->getAllValues($userId, $filtered)); + } + + public function providerSearchValuesByApps(): array { + return [ + [ + 'user1', 'key1', false, null, + [ + 'app1' => 'value1', + 'app3' => 'value123' + ] + ], + [ + 'user1', 'key1', true, null, + [ + 'only-lazy' => 'value456' + ] + ], + [ + 'user1', 'key8', false, null, + [ + 'app2' => 11, + 'app3' => 12, + ] + ], + [ + 'user1', 'key9', false, ValueType::INT, + [ + 'app2' => 0, + 'app3' => 0, + ] + ] + ]; + } + + /** + * @dataProvider providerSearchValuesByApps + */ + public function testSearchValuesByApps( + string $userId, + string $key, + bool $lazy, + ?ValueType $typedAs, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEquals($result, $preferences->searchValuesByApps($userId, $key, $lazy, $typedAs)); + } + + public function providerSearchValuesByUsers(): array { + return [ + [ + 'app2', 'key2', null, null, + [ + 'user1' => 'value2a', + 'user2' => 'value2b', + 'user3' => 'value2c', + 'user4' => 'value2A' + ] + ], + [ + 'app2', 'key2', null, ['user1', 'user3'], + [ + 'user1' => 'value2a', + 'user3' => 'value2c', + ] + ], + [ + 'app2', 'key2', ValueType::INT, ['user1', 'user3'], + [ + 'user1' => 0, + 'user3' => 0, + ] + ], + [ + 'app2', 'key8', ValueType::INT, null, + [ + 'user1' => 11, + 'user2' => 12, + 'user5' => 12, + ] + ], + ]; + } + + /** + * @dataProvider providerSearchValuesByUsers + */ + public function testSearchValuesByUsers( + string $app, + string $key, + ?ValueType $typedAs = null, + ?array $userIds = null, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing( + $result, $preferences->searchValuesByUsers($app, $key, $typedAs, $userIds) + ); + } + + public function providerSearchValuesByValueString(): array { + return [ + ['app2', 'key2', 'value2a', false, ['user1']], + ['app2', 'key2', 'value2A', false, ['user4']], + ['app2', 'key2', 'value2A', true, ['user1', 'user4']] + ]; + } + + /** + * @dataProvider providerSearchValuesByValueString + */ + public function testSearchUsersByValueString( + string $app, + string $key, + string|array $value, + bool $ci, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing( + $result, $preferences->searchUsersByValueString($app, $key, $value, $ci) + ); + } + + public function providerSearchValuesByValueInt(): array { + return [ + ['app3', 'key8', 12, []], // sensitive value, cannot search + ['app2', 'key8', 12, ['user2', 'user5']], // sensitive value, cannot search + ['only-lazy', 'key1', 123, ['user4']], + ]; + } + + /** + * @dataProvider providerSearchValuesByValueInt + */ + public function testSearchUsersByValueInt( + string $app, + string $key, + int $value, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueInt($app, $key, $value)); + } + + public function providerSearchValuesByValues(): array { + return [ + ['app2', 'key2', ['value2a', 'value2b'], ['user1', 'user2']], + ['app2', 'key2', ['value2a', 'value2c'], ['user1', 'user3']], + ]; + } + + /** + * @dataProvider providerSearchValuesByValues + */ + public function testSearchUsersByValues( + string $app, + string $key, + array $values, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValues($app, $key, $values)); + } + + public function providerSearchValuesByValueBool(): array { + return [ + ['app3', 'key10', true, ['user1', 'user4']], + ['app3', 'key10', false, ['user2']], + ]; + } + + /** + * @dataProvider providerSearchValuesByValueBool + */ + public function testSearchUsersByValueBool( + string $app, + string $key, + bool $value, + array $result, + ): void { + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueBool($app, $key, $value)); + } + + public function providerGetValueMixed(): array { + return [ + [ + ['user1'], 'user1', 'app1', 'key0', 'default_because_unknown_key', true, + 'default_because_unknown_key' + ], + [ + null, 'user1', 'app1', 'key0', 'default_because_unknown_key', true, + 'default_because_unknown_key' + ], + [ + ['user1'], 'user1', 'app1', 'key0', 'default_because_unknown_key', false, + 'default_because_unknown_key' + ], + [ + null, 'user1', 'app1', 'key0', 'default_because_unknown_key', false, + 'default_because_unknown_key' + ], + [['user1'], 'user1', 'app1', 'fast_string', 'default_because_unknown_key', false, 'f_value'], + [null, 'user1', 'app1', 'fast_string', 'default_because_unknown_key', false, 'f_value'], + [['user1'], 'user1', 'app1', 'fast_string', 'default_because_unknown_key', true, 'f_value'], + // because non-lazy are already loaded + [ + null, 'user1', 'app1', 'fast_string', 'default_because_unknown_key', true, + 'default_because_unknown_key' + ], + [ + ['user1'], 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', false, + 'default_because_unknown_key' + ], + [ + null, 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', false, + 'default_because_unknown_key' + ], + [['user1'], 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', true, 'l_value'], + [null, 'user1', 'app1', 'lazy_string', 'default_because_unknown_key', true, 'l_value'], + [ + ['user1'], 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', false, + 'fs_value' + ], + [ + null, 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', false, + 'fs_value' + ], + [ + ['user1'], 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', true, + 'fs_value' + ], + [ + null, 'user1', 'app1', 'fast_string_sensitive', 'default_because_unknown_key', true, + 'default_because_unknown_key' + ], + [ + ['user1'], 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', false, + 'default_because_unknown_key' + ], + [ + null, 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', false, + 'default_because_unknown_key' + ], + [ + ['user1'], 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', true, + 'ls_value' + ], + [null, 'user1', 'app1', 'lazy_string_sensitive', 'default_because_unknown_key', true, 'ls_value'], + ]; + } + + /** + * @dataProvider providerGetValueMixed + */ + public function testGetValueMixed( + ?array $preload, + string $userId, + string $app, + string $key, + string $default, + bool $lazy, + string $result, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($result, $preferences->getValueMixed($userId, $app, $key, $default, $lazy)); + } + + /** + * @dataProvider providerGetValueMixed + */ + public function testGetValueString( + ?array $preload, + string $userId, + string $app, + string $key, + string $default, + bool $lazy, + string $result, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($result, $preferences->getValueString($userId, $app, $key, $default, $lazy)); + } + + public function providerGetValueInt(): array { + return [ + [['user1'], 'user1', 'app1', 'key0', 54321, true, 54321], + [null, 'user1', 'app1', 'key0', 54321, true, 54321], + [['user1'], 'user1', 'app1', 'key0', 54321, false, 54321], + [null, 'user1', 'app1', 'key0', 54321, false, 54321], + [null, 'user1', 'app1', 'key22', 54321, false, 31], + [['user1'], 'user1', 'app1', 'fast_int', 54321, false, 11], + [null, 'user1', 'app1', 'fast_int', 54321, false, 11], + [['user1'], 'user1', 'app1', 'fast_int', 54321, true, 11], + [null, 'user1', 'app1', 'fast_int', 54321, true, 54321], + [['user1'], 'user1', 'app1', 'fast_int_sensitive', 54321, false, 2024], + [null, 'user1', 'app1', 'fast_int_sensitive', 54321, false, 2024], + [['user1'], 'user1', 'app1', 'fast_int_sensitive', 54321, true, 2024], + [null, 'user1', 'app1', 'fast_int_sensitive', 54321, true, 54321], + [['user1'], 'user1', 'app1', 'lazy_int', 54321, false, 54321], + [null, 'user1', 'app1', 'lazy_int', 54321, false, 54321], + [['user1'], 'user1', 'app1', 'lazy_int', 54321, true, 12], + [null, 'user1', 'app1', 'lazy_int', 54321, true, 12], + [['user1'], 'user1', 'app1', 'lazy_int_sensitive', 54321, false, 54321], + [null, 'user1', 'app1', 'lazy_int_sensitive', 54321, false, 54321], + [['user1'], 'user1', 'app1', 'lazy_int_sensitive', 54321, true, 2048], + [null, 'user1', 'app1', 'lazy_int_sensitive', 54321, true, 2048], + ]; + } + + /** + * @dataProvider providerGetValueInt + */ + public function testGetValueInt( + ?array $preload, + string $userId, + string $app, + string $key, + int $default, + bool $lazy, + int $result, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($result, $preferences->getValueInt($userId, $app, $key, $default, $lazy)); + } + + public function providerGetValueFloat(): array { + return [ + [['user1'], 'user1', 'app1', 'key0', 54.321, true, 54.321], + [null, 'user1', 'app1', 'key0', 54.321, true, 54.321], + [['user1'], 'user1', 'app1', 'key0', 54.321, false, 54.321], + [null, 'user1', 'app1', 'key0', 54.321, false, 54.321], + [['user1'], 'user1', 'app1', 'fast_float', 54.321, false, 3.14], + [null, 'user1', 'app1', 'fast_float', 54.321, false, 3.14], + [['user1'], 'user1', 'app1', 'fast_float', 54.321, true, 3.14], + [null, 'user1', 'app1', 'fast_float', 54.321, true, 54.321], + [['user1'], 'user1', 'app1', 'fast_float_sensitive', 54.321, false, 1.41], + [null, 'user1', 'app1', 'fast_float_sensitive', 54.321, false, 1.41], + [['user1'], 'user1', 'app1', 'fast_float_sensitive', 54.321, true, 1.41], + [null, 'user1', 'app1', 'fast_float_sensitive', 54.321, true, 54.321], + [['user1'], 'user1', 'app1', 'lazy_float', 54.321, false, 54.321], + [null, 'user1', 'app1', 'lazy_float', 54.321, false, 54.321], + [['user1'], 'user1', 'app1', 'lazy_float', 54.321, true, 3.14159], + [null, 'user1', 'app1', 'lazy_float', 54.321, true, 3.14159], + [['user1'], 'user1', 'app1', 'lazy_float_sensitive', 54.321, false, 54.321], + [null, 'user1', 'app1', 'lazy_float_sensitive', 54.321, false, 54.321], + [['user1'], 'user1', 'app1', 'lazy_float_sensitive', 54.321, true, 1.4142], + [null, 'user1', 'app1', 'lazy_float_sensitive', 54.321, true, 1.4142], + ]; + } + + /** + * @dataProvider providerGetValueFloat + */ + public function testGetValueFloat( + ?array $preload, + string $userId, + string $app, + string $key, + float $default, + bool $lazy, + float $result, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($result, $preferences->getValueFloat($userId, $app, $key, $default, $lazy)); + } + + public function providerGetValueBool(): array { + return [ + [['user1'], 'user1', 'app1', 'key0', false, true, false], + [null, 'user1', 'app1', 'key0', false, true, false], + [['user1'], 'user1', 'app1', 'key0', true, true, true], + [null, 'user1', 'app1', 'key0', true, true, true], + [['user1'], 'user1', 'app1', 'key0', false, false, false], + [null, 'user1', 'app1', 'key0', false, false, false], + [['user1'], 'user1', 'app1', 'key0', true, false, true], + [null, 'user1', 'app1', 'key0', true, false, true], + [['user1'], 'user1', 'app1', 'fast_boolean', false, false, true], + [null, 'user1', 'app1', 'fast_boolean', false, false, true], + [['user1'], 'user1', 'app1', 'fast_boolean_0', false, false, false], + [null, 'user1', 'app1', 'fast_boolean_0', false, false, false], + [['user1'], 'user1', 'app1', 'fast_boolean', true, false, true], + [null, 'user1', 'app1', 'fast_boolean', true, false, true], + [['user1'], 'user1', 'app1', 'fast_boolean_0', true, false, false], + [null, 'user1', 'app1', 'fast_boolean_0', true, false, false], + [['user1'], 'user1', 'app1', 'fast_boolean', false, true, true], + [null, 'user1', 'app1', 'fast_boolean', false, true, false], + [['user1'], 'user1', 'app1', 'fast_boolean_0', false, true, false], + [null, 'user1', 'app1', 'fast_boolean_0', false, true, false], + [['user1'], 'user1', 'app1', 'fast_boolean', true, true, true], + [null, 'user1', 'app1', 'fast_boolean', true, true, true], + [['user1'], 'user1', 'app1', 'fast_boolean_0', true, true, false], + [null, 'user1', 'app1', 'fast_boolean_0', true, true, true], + [['user1'], 'user1', 'app1', 'lazy_boolean', false, false, false], + [null, 'user1', 'app1', 'lazy_boolean', false, false, false], + [['user1'], 'user1', 'app1', 'lazy_boolean_0', false, false, false], + [null, 'user1', 'app1', 'lazy_boolean_0', false, false, false], + [['user1'], 'user1', 'app1', 'lazy_boolean', true, false, true], + [null, 'user1', 'app1', 'lazy_boolean', true, false, true], + [['user1'], 'user1', 'app1', 'lazy_boolean_0', true, false, true], + [null, 'user1', 'app1', 'lazy_boolean_0', true, false, true], + [['user1'], 'user1', 'app1', 'lazy_boolean', false, true, true], + [null, 'user1', 'app1', 'lazy_boolean', false, true, true], + [['user1'], 'user1', 'app1', 'lazy_boolean_0', false, true, false], + [null, 'user1', 'app1', 'lazy_boolean_0', false, true, false], + [['user1'], 'user1', 'app1', 'lazy_boolean', true, true, true], + [null, 'user1', 'app1', 'lazy_boolean', true, true, true], + [['user1'], 'user1', 'app1', 'lazy_boolean_0', true, true, false], + [null, 'user1', 'app1', 'lazy_boolean_0', true, true, false], + ]; + } + + /** + * @dataProvider providerGetValueBool + */ + public function testGetValueBool( + ?array $preload, + string $userId, + string $app, + string $key, + bool $default, + bool $lazy, + bool $result, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($result, $preferences->getValueBool($userId, $app, $key, $default, $lazy)); + } + + public function providerGetValueArray(): array { + return [ + [ + ['user1'], 'user1', 'app1', 'key0', ['default_because_unknown_key'], true, + ['default_because_unknown_key'] + ], + [ + null, 'user1', 'app1', 'key0', ['default_because_unknown_key'], true, + ['default_because_unknown_key'] + ], + [ + ['user1'], 'user1', 'app1', 'key0', ['default_because_unknown_key'], false, + ['default_because_unknown_key'] + ], + [ + null, 'user1', 'app1', 'key0', ['default_because_unknown_key'], false, + ['default_because_unknown_key'] + ], + ]; + } + + /** + * @dataProvider providerGetValueArray + */ + public function testGetValueArray( + ?array $preload, + string $userId, + string $app, + string $key, + array $default, + bool $lazy, + array $result, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEqualsCanonicalizing( + $result, $preferences->getValueArray($userId, $app, $key, $default, $lazy) + ); + } + + public function providerGetValueType(): array { + return [ + [null, 'user1', 'app1', 'key1', false, ValueType::MIXED], + [null, 'user1', 'app1', 'key1', true, null, UnknownKeyException::class], + [null, 'user1', 'app1', 'fast_string', true, ValueType::STRING, UnknownKeyException::class], + [['user1'], 'user1', 'app1', 'fast_string', true, ValueType::STRING], + [null, 'user1', 'app1', 'fast_string', false, ValueType::STRING], + [null, 'user1', 'app1', 'lazy_string', true, ValueType::STRING], + [null, 'user1', 'app1', 'lazy_string', false, ValueType::STRING, UnknownKeyException::class], + [ + null, 'user1', 'app1', 'fast_string_sensitive', true, ValueType::STRING, + UnknownKeyException::class + ], + [['user1'], 'user1', 'app1', 'fast_string_sensitive', true, ValueType::STRING], + [null, 'user1', 'app1', 'fast_string_sensitive', false, ValueType::STRING], + [null, 'user1', 'app1', 'lazy_string_sensitive', true, ValueType::STRING], + [ + null, 'user1', 'app1', 'lazy_string_sensitive', false, ValueType::STRING, + UnknownKeyException::class + ], + [null, 'user1', 'app1', 'fast_int', true, ValueType::INT, UnknownKeyException::class], + [['user1'], 'user1', 'app1', 'fast_int', true, ValueType::INT], + [null, 'user1', 'app1', 'fast_int', false, ValueType::INT], + [null, 'user1', 'app1', 'lazy_int', true, ValueType::INT], + [null, 'user1', 'app1', 'lazy_int', false, ValueType::INT, UnknownKeyException::class], + [null, 'user1', 'app1', 'fast_float', true, ValueType::FLOAT, UnknownKeyException::class], + [['user1'], 'user1', 'app1', 'fast_float', true, ValueType::FLOAT], + [null, 'user1', 'app1', 'fast_float', false, ValueType::FLOAT], + [null, 'user1', 'app1', 'lazy_float', true, ValueType::FLOAT], + [null, 'user1', 'app1', 'lazy_float', false, ValueType::FLOAT, UnknownKeyException::class], + [null, 'user1', 'app1', 'fast_boolean', true, ValueType::BOOL, UnknownKeyException::class], + [['user1'], 'user1', 'app1', 'fast_boolean', true, ValueType::BOOL], + [null, 'user1', 'app1', 'fast_boolean', false, ValueType::BOOL], + [null, 'user1', 'app1', 'lazy_boolean', true, ValueType::BOOL], + [null, 'user1', 'app1', 'lazy_boolean', false, ValueType::BOOL, UnknownKeyException::class], + ]; + } + + /** + * @dataProvider providerGetValueType + */ + public function testGetValueType( + ?array $preload, + string $userId, + string $app, + string $key, + ?bool $lazy, + ?ValueType $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $type = $preferences->getValueType($userId, $app, $key, $lazy); + if ($exception === null) { + $this->assertEquals($result->value, $type->value); + } + } + + public function providerSetValueMixed(): array { + return [ + [null, 'user1', 'app1', 'key1', 'value', false, false, true], + [null, 'user1', 'app1', 'key1', '12345', true, false, true], + [null, 'user1', 'app1', 'key1', '12345', true, true, true], + [null, 'user1', 'app1', 'key1', 'value1', false, false, false], + [null, 'user1', 'app1', 'key1', 'value1', true, false, true], + [null, 'user1', 'app1', 'key1', 'value1', false, true, true], + [ + null, 'user1', 'app1', 'fast_string', 'f_value_2', false, false, true, + TypeConflictException::class + ], + [ + null, 'user1', 'app1', 'fast_string', 'f_value', true, false, true, + TypeConflictException::class + ], + [null, 'user1', 'app1', 'fast_string', 'f_value', true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_int', 'n_value', false, false, true, TypeConflictException::class], + [ + null, 'user1', 'app1', 'fast_float', 'n_value', false, false, true, + TypeConflictException::class + ], + [ + null, 'user1', 'app1', 'lazy_string', 'l_value_2', false, false, true, + TypeConflictException::class + ], + [null, 'user1', 'app1', 'lazy_string', 'l_value', true, false, false], + [null, 'user1', 'app1', 'lazy_string', 'l_value', true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_int', 'l_value', false, false, true, TypeConflictException::class], + [ + null, 'user1', 'app1', 'lazy_float', 'l_value', false, false, true, + TypeConflictException::class + ], + ]; + } + + /** + * @dataProvider providerSetValueMixed + */ + public function testSetValueMixed( + ?array $preload, + string $userId, + string $app, + string $key, + string $value, + bool $lazy, + bool $sensitive, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->setValueMixed($userId, $app, $key, $value, $lazy, $sensitive); + + if ($exception === null) { + $this->assertEquals($result, $edited); + } + } + + + public function providerSetValueString(): array { + return [ + [null, 'user1', 'app1', 'key1', 'value', false, false, true], + [null, 'user1', 'app1', 'key1', '12345', true, false, true], + [null, 'user1', 'app1', 'key1', '12345', true, true, true], + [null, 'user1', 'app1', 'key1', 'value1', false, false, false], + [null, 'user1', 'app1', 'key1', 'value1', true, false, true], + [null, 'user1', 'app1', 'key1', 'value1', false, true, true], + [null, 'user1', 'app1', 'fast_string', 'f_value_2', false, false, true], + [null, 'user1', 'app1', 'fast_string', 'f_value', false, false, false], + [null, 'user1', 'app1', 'fast_string', 'f_value', true, false, true], + [null, 'user1', 'app1', 'fast_string', 'f_value', true, true, true], + [null, 'user1', 'app1', 'lazy_string', 'l_value_2', false, false, true], + [null, 'user1', 'app1', 'lazy_string', 'l_value', true, false, false], + [null, 'user1', 'app1', 'lazy_string', 'l_value', true, true, true], + [null, 'user1', 'app1', 'fast_string_sensitive', 'fs_value', false, true, false], + [null, 'user1', 'app1', 'fast_string_sensitive', 'fs_value', true, true, true], + [null, 'user1', 'app1', 'fast_string_sensitive', 'fs_value', true, false, true], + [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value', false, true, true], + [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value', true, true, false], + [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value', true, false, false], + [null, 'user1', 'app1', 'lazy_string_sensitive', 'ls_value_2', true, false, true], + [null, 'user1', 'app1', 'fast_int', 'n_value', false, false, true, TypeConflictException::class], + [ + null, 'user1', 'app1', 'fast_float', 'n_value', false, false, true, + TypeConflictException::class + ], + [ + null, 'user1', 'app1', 'fast_float', 'n_value', false, false, true, + TypeConflictException::class + ], + [null, 'user1', 'app1', 'lazy_int', 'n_value', false, false, true, TypeConflictException::class], + [ + null, 'user1', 'app1', 'lazy_boolean', 'n_value', false, false, true, + TypeConflictException::class + ], + [ + null, 'user1', 'app1', 'lazy_float', 'n_value', false, false, true, + TypeConflictException::class + ], + ]; + } + + /** + * @dataProvider providerSetValueString + */ + public function testSetValueString( + ?array $preload, + string $userId, + string $app, + string $key, + string $value, + bool $lazy, + bool $sensitive, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->setValueString($userId, $app, $key, $value, $lazy, $sensitive); + if ($exception !== null) { + return; + } + + $this->assertEquals($result, $edited); + if ($result) { + $this->assertEquals($value, $preferences->getValueString($userId, $app, $key, $value, $lazy)); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($value, $preferences->getValueString($userId, $app, $key, $value, $lazy)); + } + } + + public function providerSetValueInt(): array { + return [ + [null, 'user1', 'app1', 'key1', 12345, false, false, true], + [null, 'user1', 'app1', 'key1', 12345, true, false, true], + [null, 'user1', 'app1', 'key1', 12345, true, true, true], + [null, 'user1', 'app1', 'fast_int', 11, false, false, false], + [null, 'user1', 'app1', 'fast_int', 111, false, false, true], + [null, 'user1', 'app1', 'fast_int', 111, true, false, true], + [null, 'user1', 'app1', 'fast_int', 111, false, true, true], + [null, 'user1', 'app1', 'fast_int', 11, true, false, true], + [null, 'user1', 'app1', 'fast_int', 11, false, true, true], + [null, 'user1', 'app1', 'lazy_int', 12, false, false, true], + [null, 'user1', 'app1', 'lazy_int', 121, false, false, true], + [null, 'user1', 'app1', 'lazy_int', 121, true, false, true], + [null, 'user1', 'app1', 'lazy_int', 121, false, true, true], + [null, 'user1', 'app1', 'lazy_int', 12, true, false, false], + [null, 'user1', 'app1', 'lazy_int', 12, false, true, true], + [null, 'user1', 'app1', 'fast_string', 12345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', 12345, false, false, false, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', 12345, true, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', 12345, true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', 12345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', 12345, true, false, false, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', 12345, true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_float', 12345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_float', 12345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_boolean', 12345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_float', 12345, false, false, true, TypeConflictException::class], + ]; + } + + /** + * @dataProvider providerSetValueInt + */ + public function testSetValueInt( + ?array $preload, + string $userId, + string $app, + string $key, + int $value, + bool $lazy, + bool $sensitive, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->setValueInt($userId, $app, $key, $value, $lazy, $sensitive); + + if ($exception !== null) { + return; + } + + $this->assertEquals($result, $edited); + if ($result) { + $this->assertEquals($value, $preferences->getValueInt($userId, $app, $key, $value, $lazy)); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($value, $preferences->getValueInt($userId, $app, $key, $value, $lazy)); + } + } + + public function providerSetValueFloat(): array { + return [ + [null, 'user1', 'app1', 'key1', 12.345, false, false, true], + [null, 'user1', 'app1', 'key1', 12.345, true, false, true], + [null, 'user1', 'app1', 'key1', 12.345, true, true, true], + [null, 'user1', 'app1', 'fast_float', 3.14, false, false, false], + [null, 'user1', 'app1', 'fast_float', 3.15, false, false, true], + [null, 'user1', 'app1', 'fast_float', 3.15, true, false, true], + [null, 'user1', 'app1', 'fast_float', 3.15, false, true, true], + [null, 'user1', 'app1', 'fast_float', 3.14, true, false, true], + [null, 'user1', 'app1', 'fast_float', 3.14, false, true, true], + [null, 'user1', 'app1', 'lazy_float', 3.14159, false, false, true], + [null, 'user1', 'app1', 'lazy_float', 3.14158, false, false, true], + [null, 'user1', 'app1', 'lazy_float', 3.14158, true, false, true], + [null, 'user1', 'app1', 'lazy_float', 3.14158, false, true, true], + [null, 'user1', 'app1', 'lazy_float', 3.14159, true, false, false], + [null, 'user1', 'app1', 'lazy_float', 3.14159, false, true, true], + [null, 'user1', 'app1', 'fast_string', 12.345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', 12.345, false, false, false, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', 12.345, true, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', 12.345, true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', 12.345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', 12.345, true, false, false, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_array', 12.345, true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_int', 12.345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_int', 12.345, false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_boolean', 12.345, false, false, true, TypeConflictException::class], + ]; + } + + /** + * @dataProvider providerSetValueFloat + */ + public function testSetValueFloat( + ?array $preload, + string $userId, + string $app, + string $key, + float $value, + bool $lazy, + bool $sensitive, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->setValueFloat($userId, $app, $key, $value, $lazy, $sensitive); + + if ($exception !== null) { + return; + } + + $this->assertEquals($result, $edited); + if ($result) { + $this->assertEquals($value, $preferences->getValueFloat($userId, $app, $key, $value, $lazy)); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($value, $preferences->getValueFloat($userId, $app, $key, $value, $lazy)); + } + } + + + public function providerSetValueArray(): array { + return [ + [null, 'user1', 'app1', 'key1', [], false, false, true], + [null, 'user1', 'app1', 'key1', [], true, false, true], + [null, 'user1', 'app1', 'key1', [], true, true, true], + [null, 'user1', 'app1', 'fast_array', ['year' => 2024], false, false, false], + [null, 'user1', 'app1', 'fast_array', [], false, false, true], + [null, 'user1', 'app1', 'fast_array', [], true, false, true], + [null, 'user1', 'app1', 'fast_array', [], false, true, true], + [null, 'user1', 'app1', 'fast_array', ['year' => 2024], true, false, true], + [null, 'user1', 'app1', 'fast_array', ['year' => 2024], false, true, true], + [null, 'user1', 'app1', 'lazy_array', ['month' => 'October'], false, false, true], + [null, 'user1', 'app1', 'lazy_array', ['month' => 'September'], false, false, true], + [null, 'user1', 'app1', 'lazy_array', ['month' => 'September'], true, false, true], + [null, 'user1', 'app1', 'lazy_array', ['month' => 'September'], false, true, true], + [null, 'user1', 'app1', 'lazy_array', ['month' => 'October'], true, false, false], + [null, 'user1', 'app1', 'lazy_array', ['month' => 'October'], false, true, true], + [null, 'user1', 'app1', 'fast_string', [], false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', [], false, false, false, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', [], true, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_string', [], true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', [], false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', [], true, false, false, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_string', [], true, true, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_int', [], false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'fast_int', [], false, false, true, TypeConflictException::class], + [null, 'user1', 'app1', 'lazy_boolean', [], false, false, true, TypeConflictException::class], + ]; + } + + /** + * @dataProvider providerSetValueArray + */ + public function testSetValueArray( + ?array $preload, + string $userId, + string $app, + string $key, + array $value, + bool $lazy, + bool $sensitive, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->setValueArray($userId, $app, $key, $value, $lazy, $sensitive); + + if ($exception !== null) { + return; + } + + $this->assertEquals($result, $edited); + if ($result) { + $this->assertEqualsCanonicalizing( + $value, $preferences->getValueArray($userId, $app, $key, $value, $lazy) + ); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEqualsCanonicalizing( + $value, $preferences->getValueArray($userId, $app, $key, $value, $lazy) + ); + } + } + + public function providerUpdateSensitive(): array { + return [ + [null, 'user1', 'app1', 'key1', false, false], + [['user1'], 'user1', 'app1', 'key1', false, false], + [null, 'user1', 'app1', 'key1', true, true], + [['user1'], 'user1', 'app1', 'key1', true, true], + ]; + } + + /** + * @dataProvider providerUpdateSensitive + */ + public function testUpdateSensitive( + ?array $preload, + string $userId, + string $app, + string $key, + bool $sensitive, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->updateSensitive($userId, $app, $key, $sensitive); + if ($exception !== null) { + return; + } + + $this->assertEquals($result, $edited); + if ($result) { + $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key)); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key)); + if ($sensitive) { + $this->assertEquals(true, str_starts_with( + $preferences->statusCache()['fastCache'][$userId][$app][$key] ?? + $preferences->statusCache()['lazyCache'][$userId][$app][$key], + '$UserPreferencesEncryption$') + ); + } + } + } + + public function providerUpdateGlobalSensitive(): array { + return [[true], [false]]; + } + + /** + * @dataProvider providerUpdateGlobalSensitive + */ + public function testUpdateGlobalSensitive(bool $sensitive): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $app = 'app2'; + if ($sensitive) { + $key = 'key2'; + $value = 'value2a'; + } else { + $key = 'key4'; + $value = 'value4'; + } + + $this->assertEquals($value, $preferences->getValueString('user1', $app, $key)); + foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { + $preferences->getValueString($userId, $app, $key); // cache loading for userId + $this->assertEquals( + !$sensitive, str_starts_with( + $preferences->statusCache()['fastCache'][$userId][$app][$key] ?? + $preferences->statusCache()['lazyCache'][$userId][$app][$key], + '$UserPreferencesEncryption$' + ) + ); + } + + $preferences->updateGlobalSensitive($app, $key, $sensitive); + + $this->assertEquals($value, $preferences->getValueString('user1', $app, $key)); + foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { + $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key)); + // should only work if updateGlobalSensitive drop cache + $this->assertEquals($sensitive, str_starts_with( + $preferences->statusCache()['fastCache'][$userId][$app][$key] ?? + $preferences->statusCache()['lazyCache'][$userId][$app][$key], + '$UserPreferencesEncryption$') + ); + } + } + + public function providerUpdateLazy(): array { + return [ + [null, 'user1', 'app1', 'key1', false, false], + [['user1'], 'user1', 'app1', 'key1', false, false], + [null, 'user1', 'app1', 'key1', true, true], + [['user1'], 'user1', 'app1', 'key1', true, true], + ]; + } + + /** + * @dataProvider providerUpdateLazy + */ + public function testUpdateLazy( + ?array $preload, + string $userId, + string $app, + string $key, + bool $lazy, + bool $result, + ?string $exception = null, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + if ($exception !== null) { + $this->expectException($exception); + } + + $edited = $preferences->updateLazy($userId, $app, $key, $lazy); + if ($exception !== null) { + return; + } + + $this->assertEquals($result, $edited); + if ($result) { + $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key)); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key)); + } + } + + public function providerUpdateGlobalLazy(): array { + return [[true], [false]]; + } + + /** + * @dataProvider providerUpdateGlobalLazy + */ + public function testUpdateGlobalLazy(bool $lazy): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $app = 'app2'; + if ($lazy) { + $key = 'key4'; + $value = 'value4'; + } else { + $key = 'key3'; + $value = 'value3'; + } + + $this->assertEquals($value, $preferences->getValueString('user1', $app, $key, '', !$lazy)); + foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { + $this->assertEquals(!$lazy, $preferences->isLazy($userId, $app, $key)); + } + + $preferences->updateGlobalLazy($app, $key, $lazy); + $this->assertEquals($value, $preferences->getValueString('user1', $app, $key, '', $lazy)); + foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { + $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key)); + } + } + + public function providerGetDetails(): array { + return [ + [ + 'user3', 'app2', 'key2', + [ + 'userId' => 'user3', + 'app' => 'app2', + 'key' => 'key2', + 'value' => 'value2c', + 'type' => 2, + 'lazy' => false, + 'typeString' => 'mixed', + 'sensitive' => false + ] + ], + [ + 'user1', 'app1', 'lazy_int', + [ + 'userId' => 'user1', + 'app' => 'app1', + 'key' => 'lazy_int', + 'value' => 12, + 'type' => 8, + 'lazy' => true, + 'typeString' => 'int', + 'sensitive' => false + ] + ], + [ + 'user1', 'app1', 'fast_float_sensitive', + [ + 'userId' => 'user1', + 'app' => 'app1', + 'key' => 'fast_float_sensitive', + 'value' => 1.41, + 'type' => 16, + 'lazy' => false, + 'typeString' => 'float', + 'sensitive' => true + ] + ], + ]; + } + + /** + * @dataProvider providerGetDetails + */ + public function testGetDetails(string $userId, string $app, string $key, array $result): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEqualsCanonicalizing($result, $preferences->getDetails($userId, $app, $key)); + } + + + public function providerDeletePreference(): array { + return [ + [null, 'user1', 'app1', 'key22'], + [['user1'], 'user1', 'app1', 'fast_string_sensitive'], + [null, 'user1', 'app1', 'lazy_array_sensitive'], + [['user2'], 'user1', 'app1', 'lazy_array_sensitive'], + [null, 'user2', 'only-lazy', 'key1'], + [['user2'], 'user2', 'only-lazy', 'key1'], + ]; + } + + /** + * @dataProvider providerDeletePreference + */ + public function testDeletePreference( + ?array $preload, + string $userId, + string $app, + string $key, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $lazy = $preferences->isLazy($userId, $app, $key); + + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals(true, $preferences->hasKey($userId, $app, $key, $lazy)); + $preferences->deletePreference($userId, $app, $key); + $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, $lazy)); + $preferences = $this->generateUserPreferences($preload ?? []); + $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, $lazy)); + } + + public function providerDeleteKey(): array { + return [ + [null, 'app2', 'key3'], + [['user1'], 'app2', 'key3'], + [null, 'only-lazy', 'key1'], + [['user2'], 'only-lazy', 'key1'], + [null, 'app2', 'lazy_string_sensitive'], + [['user3', 'user1'], 'app2', 'lazy_string_sensitive'], + ]; + } + + /** + * @dataProvider providerDeleteKey + */ + public function testDeleteKey( + ?array $preload, + string $app, + string $key, + ): void { + $preferences = $this->generateUserPreferences($preload ?? []); + $preferences->deleteKey($app, $key); + + foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { + $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, null)); + $preferencesTemp = $this->generateUserPreferences($preload ?? []); + $this->assertEquals(false, $preferencesTemp->hasKey($userId, $app, $key, null)); + } + } + + public function testDeleteApp(): void { + $preferences = $this->generateUserPreferences(); + $preferences->deleteApp('only-lazy'); + + foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { + $this->assertEquals(false, in_array('only-lazy', $preferences->getApps($userId))); + $preferencesTemp = $this->generateUserPreferences(); + $this->assertEquals(false, in_array('only-lazy', $preferencesTemp->getApps($userId))); + } + } + + public function testDeleteAllPreferences(): void { + $preferences = $this->generateUserPreferences(); + $preferences->deleteAllPreferences('user1'); + + $this->assertEqualsCanonicalizing([], $preferences->getApps('user1')); + $preferences = $this->generateUserPreferences(); + $this->assertEqualsCanonicalizing([], $preferences->getApps('user1')); + } + + public function testClearCache(): void { + $preferences = $this->generateUserPreferences(['user1', 'user2']); + $preferences->clearCache('user1'); + + $this->assertEquals(true, $preferences->statusCache()['fastLoaded']['user2']); + $this->assertEquals(false, $preferences->statusCache()['fastLoaded']['user1']); + $this->assertEquals('value2a', $preferences->getValueString('user1', 'app2', 'key2')); + $this->assertEquals(false, $preferences->statusCache()['lazyLoaded']['user1']); + $this->assertEquals(true, $preferences->statusCache()['fastLoaded']['user1']); + } + + public function testClearCacheAll(): void { + $preferences = $this->generateUserPreferences(['user1', 'user2']); + $preferences->clearCacheAll(); + $this->assertEqualsCanonicalizing( + [ + 'fastLoaded' => [], + 'fastCache' => [], + 'lazyLoaded' => [], + 'lazyCache' => [], + 'valueTypes' => [], + ], + $preferences->statusCache() + ); + } +} From e73513bdd1f42da656b79bf78d8d3763386d26b1 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 17 Oct 2024 08:48:09 -0100 Subject: [PATCH 2/5] fix(user-prefs): adding sensitive and indexed as flags Signed-off-by: Maxence Lange --- .../Version31000Date20240814184402.php | 19 +- lib/private/AllConfig.php | 48 ++- lib/private/UserPreferences.php | 393 ++++++++++++------ .../UserPreferences/IUserPreferences.php | 81 +++- lib/public/UserPreferences/ValueType.php | 76 +--- .../UserPreferences/ValueTypeDefinition.php | 30 -- tests/lib/UserPreferencesTest.php | 121 +++--- 7 files changed, 481 insertions(+), 287 deletions(-) delete mode 100644 lib/public/UserPreferences/ValueTypeDefinition.php diff --git a/core/Migrations/Version31000Date20240814184402.php b/core/Migrations/Version31000Date20240814184402.php index 20804b1b19efe..87b7e93db97d0 100644 --- a/core/Migrations/Version31000Date20240814184402.php +++ b/core/Migrations/Version31000Date20240814184402.php @@ -14,6 +14,7 @@ use OCP\Migration\Attributes\AddColumn; use OCP\Migration\Attributes\AddIndex; use OCP\Migration\Attributes\ColumnType; +use OCP\Migration\Attributes\DropIndex; use OCP\Migration\Attributes\IndexType; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; @@ -23,7 +24,11 @@ */ #[AddColumn(table: 'preferences', name: 'lazy', type: ColumnType::SMALLINT, description: 'lazy loading to user preferences')] #[AddColumn(table: 'preferences', name: 'type', type: ColumnType::SMALLINT, description: 'typed values to user preferences')] -#[AddIndex(table: 'preferences', type: IndexType::INDEX, description: 'new index including lazy flag')] +#[AddColumn(table: 'preferences', name: 'flag', type: ColumnType::INTEGER, description: 'bitflag about the value')] +#[AddColumn(table: 'preferences', name: 'indexed', type: ColumnType::INTEGER, description: 'non-array value can be set as indexed')] +#[DropIndex(table: 'preferences', type: IndexType::INDEX, description: 'remove previous app/key index', notes: ['will be re-created to include \'indexed\' field'])] +#[AddIndex(table: 'preferences', type: IndexType::INDEX, description: 'new index including user+lazy')] +#[AddIndex(table: 'preferences', type: IndexType::INDEX, description: 'new index including app/key and indexed')] class Version31000Date20240814184402 extends SimpleMigrationStep { public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { /** @var ISchemaWrapper $schema */ @@ -31,8 +36,18 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table = $schema->getTable('preferences'); $table->addColumn('lazy', Types::SMALLINT, ['notnull' => true, 'default' => 0, 'length' => 1, 'unsigned' => true]); - $table->addColumn('type', Types::INTEGER, ['notnull' => true, 'default' => 2, 'unsigned' => true]); + $table->addColumn('type', Types::SMALLINT, ['notnull' => true, 'default' => 0, 'unsigned' => true]); + $table->addColumn('flags', Types::INTEGER, ['notnull' => true, 'default' => 0, 'unsigned' => true]); + $table->addColumn('indexed', Types::STRING, ['notnull' => true, 'default' => '', 'length' => 64]); + + // removing this index from Version13000Date20170718121200 + // $table->addIndex(['appid', 'configkey'], 'preferences_app_key'); + if ($table->hasIndex('preferences_app_key')) { + $table->dropIndex('preferences_app_key'); + } + $table->addIndex(['userid', 'lazy'], 'prefs_uid_lazy_i'); + $table->addIndex(['appid', 'configkey', 'indexed', 'flags'], 'prefs_app_key_ind_fl_i'); return $schema; } diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index bc54ea1e6afaa..b2ebe322d9eef 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -226,9 +226,15 @@ public function deleteAppValues($appName) { * @param string $key the key under which the value is being stored * @param string|float|int $value the value that you want to store * @param string $preCondition only update if the config value was previously the value passed as $preCondition + * * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met * @throws \UnexpectedValueException when trying to store an unexpected value * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @see IUserPreferences::getValueString + * @see IUserPreferences::getValueInt + * @see IUserPreferences::getValueFloat + * @see IUserPreferences::getValueArray + * @see IUserPreferences::getValueBool */ public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { if (!is_int($value) && !is_float($value) && !is_string($value)) { @@ -236,7 +242,7 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu } /** @var UserPreferences $userPreferences */ - $userPreferences = \OC::$server->get(IUserPreferences::class); + $userPreferences = \OCP\Server::get(IUserPreferences::class); if ($preCondition !== null) { try { if ($userPreferences->getValueMixed($userId, $appName, $key) !== (string)$preCondition) { @@ -256,15 +262,21 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu * @param string $appName the appName that we stored the value under * @param string $key the key under which the value is being stored * @param mixed $default the default value to be returned if the value isn't set + * * @return string * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @see IUserPreferences::getValueString + * @see IUserPreferences::getValueInt + * @see IUserPreferences::getValueFloat + * @see IUserPreferences::getValueArray + * @see IUserPreferences::getValueBool */ public function getUserValue($userId, $appName, $key, $default = '') { if ($userId === null || $userId === '') { return $default; } /** @var UserPreferences $userPreferences */ - $userPreferences = \OC::$server->get(IUserPreferences::class); + $userPreferences = \OCP\Server::get(IUserPreferences::class); // because $default can be null ... if (!$userPreferences->hasKey($userId, $appName, $key)) { return $default; @@ -278,10 +290,10 @@ public function getUserValue($userId, $appName, $key, $default = '') { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @return string[] - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::getKeys} directly */ public function getUserKeys($userId, $appName) { - return \OC::$server->get(IUserPreferences::class)->getKeys($userId, $appName); + return \OCP\Server::get(IUserPreferences::class)->getKeys($userId, $appName); } /** @@ -290,33 +302,33 @@ public function getUserKeys($userId, $appName) { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @param string $key the key under which the value is being stored - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::deletePreference} directly */ public function deleteUserValue($userId, $appName, $key) { - \OC::$server->get(IUserPreferences::class)->deletePreference($userId, $appName, $key); + \OCP\Server::get(IUserPreferences::class)->deletePreference($userId, $appName, $key); } /** * Delete all user values * * @param string $userId the userId of the user that we want to remove all values from - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::deleteAllPreferences} directly */ public function deleteAllUserValues($userId) { if ($userId === null) { return; } - \OC::$server->get(IUserPreferences::class)->deleteAllPreferences($userId); + \OCP\Server::get(IUserPreferences::class)->deleteAllPreferences($userId); } /** * Delete all user related values of one app * * @param string $appName the appName of the app that we want to remove all values from - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::deleteApp} directly */ public function deleteAppFromAllUsers($appName) { - \OC::$server->get(IUserPreferences::class)->deleteApp($appName); + \OCP\Server::get(IUserPreferences::class)->deleteApp($appName); } /** @@ -328,14 +340,14 @@ public function deleteAppFromAllUsers($appName) { * [ $appId => * [ $key => $value ] * ] - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::getAllValues} directly */ public function getAllUserValues(?string $userId): array { if ($userId === null || $userId === '') { return []; } - $values = \OC::$server->get(IUserPreferences::class)->getAllValues($userId); + $values = \OCP\Server::get(IUserPreferences::class)->getAllValues($userId); $result = []; foreach ($values as $app => $list) { foreach ($list as $key => $value) { @@ -352,10 +364,10 @@ public function getAllUserValues(?string $userId): array { * @param string $key the key to get the value for * @param array $userIds the user IDs to fetch the values for * @return array Mapped values: userId => value - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::getValuesByUsers} directly */ public function getUserValueForUsers($appName, $key, $userIds) { - return \OC::$server->get(IUserPreferences::class)->searchValuesByUsers($appName, $key, ValueType::MIXED, $userIds); + return \OCP\Server::get(IUserPreferences::class)->getValuesByUsers($appName, $key, ValueType::MIXED, $userIds); } /** @@ -365,10 +377,10 @@ public function getUserValueForUsers($appName, $key, $userIds) { * @param string $key the key to get the user for * @param string $value the value to get the user for * @return list of user IDs - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly */ public function getUsersForUserValue($appName, $key, $value) { - return \OC::$server->get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value); + return \OCP\Server::get(IUserPreferences::class)->searchUsersByValueDeprecated($appName, $key, $value); } /** @@ -378,14 +390,14 @@ public function getUsersForUserValue($appName, $key, $value) { * @param string $key the key to get the user for * @param string $value the value to get the user for * @return list of user IDs - * @deprecated 31.0.0 - use {@see IUserPreferences} directly + * @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly */ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) { if ($appName === 'settings' && $key === 'email') { return $this->getUsersForUserValue($appName, $key, strtolower($value)); } - return \OC::$server->get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value, true); + return \OCP\Server::get(IUserPreferences::class)->searchUsersByValueDeprecated($appName, $key, $value, true); } public function getSystemConfig() { diff --git a/lib/private/UserPreferences.php b/lib/private/UserPreferences.php index a5426e5d247b7..22313292e94af 100644 --- a/lib/private/UserPreferences.php +++ b/lib/private/UserPreferences.php @@ -22,7 +22,6 @@ use OCP\UserPreferences\IUserPreferences; use OCP\UserPreferences\ValueType; use Psr\Log\LoggerInterface; -use ValueError; /** * This class provides an easy way for apps to store user preferences in the @@ -45,15 +44,20 @@ class UserPreferences implements IUserPreferences { private const USER_MAX_LENGTH = 64; private const APP_MAX_LENGTH = 32; private const KEY_MAX_LENGTH = 64; + private const INDEX_MAX_LENGTH = 64; private const ENCRYPTION_PREFIX = '$UserPreferencesEncryption$'; private const ENCRYPTION_PREFIX_LENGTH = 27; // strlen(self::ENCRYPTION_PREFIX) - /** @var array>> ['user_id' => ['app_id' => ['key' => 'value']]] */ + /** @var array>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */ private array $fastCache = []; // cache for normal preference keys /** @var array>> ['user_id' => ['app_id' => ['key' => 'value']]] */ private array $lazyCache = []; // cache for lazy preference keys - /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ + /** @var array|<'flags', int>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]] */ + private array $valueDetails = []; // type for all preference values + /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ private array $valueTypes = []; // type for all preference values + /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ + private array $valueFlags = []; // type for all preference values /** @var array ['user_id' => bool] */ private array $fastLoaded = []; /** @var array ['user_id' => bool] */ @@ -157,6 +161,8 @@ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = f } /** + * @inheritDoc + * * @param string $userId id of the user * @param string $app id of the app * @param string $key preference key @@ -164,17 +170,40 @@ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = f * * @return bool * @throws UnknownKeyException if preference key is not known - * @since 29.0.0 + * @since 31.0.0 */ public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { $this->assertParams($userId, $app, $key); $this->loadPreferences($userId, $lazy); - if (!isset($this->valueTypes[$userId][$app][$key])) { + if (!isset($this->valueDetails[$userId][$app][$key])) { throw new UnknownKeyException('unknown preference key'); } - return $this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key]); + return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']); + } + + /** + * @inheritDoc + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences + * + * @return bool + * @throws UnknownKeyException if preference key is not known + * @since 31.0.0 + */ + public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool { + $this->assertParams($userId, $app, $key); + $this->loadPreferences($userId, $lazy); + + if (!isset($this->valueDetails[$userId][$app][$key])) { + throw new UnknownKeyException('unknown preference key'); + } + + return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']); } /** @@ -187,15 +216,16 @@ public function isSensitive(string $userId, string $app, string $key, ?bool $laz * @return bool TRUE if preference is lazy loaded * @throws UnknownKeyException if preference key is not known * @see IUserPreferences for details about lazy loading - * @since 29.0.0 + * @since 31.0.0 */ public function isLazy(string $userId, string $app, string $key): bool { // there is a huge probability the non-lazy preferences are already loaded + // meaning that we can start by only checking if a current non-lazy key exists if ($this->hasKey($userId, $app, $key, false)) { - return false; + return false; // meaning key is not lazy. } - // key not found, we search in the lazy preferences + // as key is not found as non-lazy, we load and search in the lazy preferences if ($this->hasKey($userId, $app, $key, true)) { return true; } @@ -268,7 +298,7 @@ public function getAllValues(string $userId, bool $filtered = false): array { * @return array [appId => value] * @since 31.0.0 */ - public function searchValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { + public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { $this->assertParams($userId, '', $key, allowEmptyApp: true); $this->loadPreferences($userId, $lazy); @@ -307,7 +337,7 @@ public function searchValuesByApps(string $userId, string $key, bool $lazy = fal * @return array [userId => value] * @since 31.0.0 */ - public function searchValuesByUsers( + public function getValuesByUsers( string $app, string $key, ?ValueType $typedAs = null, @@ -328,7 +358,7 @@ public function searchValuesByUsers( while ($row = $result->fetch()) { $value = $row['configvalue']; try { - $value = $this->convertTypedValue($value, $typedAs ?? $this->extractValueType($row['type'])); + $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int) $row['type'])); } catch (IncorrectTypeException) { } $values[$row['userid']] = $value; @@ -370,6 +400,22 @@ public function searchUsersByValueString(string $app, string $key, string $value return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); } + /** + * @inheritDoc + * + * @param string $app id of the app + * @param string $key preference key + * @param string $value preference value + * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string + * @internal + * @deprecated since 31.0.0 - {@see } + * @return list + * @since 31.0.0 + */ + public function searchUsersByValueDeprecated(string $app, string $key, string $value, bool $caseInsensitive = false): array { + return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive, true); + } + /** * @inheritDoc * @@ -424,6 +470,7 @@ public function searchUsersByValueBool(string $app, string $key, bool $value): a * @param string $key * @param string|array $value * @param bool $caseInsensitive + * @param bool $withinNotIndexedField DEPRECATED: should only be used to stay compatible with not-indexed/pre-31 preferences value * * @return list */ @@ -436,20 +483,39 @@ private function searchUsersByTypedValue(string $app, string $key, string|array $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); + // search within 'indexed' OR 'configvalue' (but if 'flags' is not set as indexed) + // TODO: when implementing config lexicon remove the searches on 'configvalue' if value is set as indexed $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; if (is_array($value)) { - $qb->andWhere($qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY))); + $where = $qb->expr()->orX( + $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)), + $qb->expr()->andX( + $qb->createFunction('NOT ' . $qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED)), + $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)) + ) + ); } else { if ($caseInsensitive) { - $qb->andWhere($qb->expr()->eq( - $qb->func()->lower($configValueColumn), - $qb->createNamedParameter(strtolower($value))) + $where = $qb->expr()->orX( + $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value))), + $qb->expr()->andX( + $qb->createFunction('NOT ' . $qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED)), + $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value))) + ) ); } else { - $qb->andWhere($qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value))); + $where = $qb->expr()->orX( + $qb->expr()->eq('indexed', $qb->createNamedParameter($value)), + $qb->expr()->andX( + $qb->createFunction('NOT ' . $qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED)), + $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) + ) + ); } } + $qb->andWhere($where); + $userIds = []; $result = $qb->executeQuery(); $rows = $result->fetchAll(); @@ -493,7 +559,7 @@ public function getValueMixed( ?bool $lazy = false, ): string { try { - $lazy = ($lazy === null) ? $this->isLazy($userId, $app, $key) : $lazy; + $lazy ??= $this->isLazy($userId, $app, $key); } catch (UnknownKeyException) { return $default; } @@ -570,7 +636,7 @@ public function getValueInt( * @return float stored preference value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function getValueFloat( @@ -595,7 +661,7 @@ public function getValueFloat( * @return bool stored preference value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function getValueBool( @@ -621,7 +687,7 @@ public function getValueBool( * @return array stored preference value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function getValueArray( @@ -668,11 +734,11 @@ private function getTypedValue( * If type of stored value is set as mixed, we don't filter. * If type of stored value is defined, we compare with the one requested. */ - $knownType = $this->valueTypes[$userId][$app][$key] ?? 0; - if (!$this->isTyped(ValueType::MIXED, $type->value) - && $knownType > 0 - && !$this->isTyped(ValueType::MIXED, $knownType) - && !$this->isTyped($type, $knownType)) { + $knownType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; + if ($type !== ValueType::MIXED + && $knownType !== null + && $knownType !== ValueType::MIXED + && $type !== $knownType) { $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); throw new TypeConflictException('conflict with value type from database'); } @@ -712,29 +778,35 @@ public function getValueType(string $userId, string $app, string $key, ?bool $la $this->assertParams($userId, $app, $key); $this->loadPreferences($userId, $lazy); - if (!isset($this->valueTypes[$userId][$app][$key])) { + if (!isset($this->valueDetails[$userId][$app][$key]['type'])) { throw new UnknownKeyException('unknown preference key'); } - return $this->extractValueType($this->valueTypes[$userId][$app][$key]); + return $this->valueDetails[$userId][$app][$key]['type']; } /** - * convert bitflag from value type to ValueType + * @inheritDoc * - * @param int $type + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy lazy loading * - * @return ValueType - * @throws IncorrectTypeException + * @return ValueType type of the value + * @throws UnknownKeyException if preference key is not known + * @throws IncorrectTypeException if preferences value type is not known + * @since 31.0.0 */ - private function extractValueType(int $type): ValueType { - $type &= ~ValueType::SENSITIVE->value; + public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int { + $this->assertParams($userId, $app, $key); + $this->loadPreferences($userId, $lazy); - try { - return ValueType::from($type); - } catch (ValueError) { - throw new IncorrectTypeException('invalid value type'); + if (!isset($this->valueDetails[$userId][$app][$key])) { + throw new UnknownKeyException('unknown preference key'); } + + return $this->valueDetails[$userId][$app][$key]['flags']; } /** @@ -752,7 +824,7 @@ private function extractValueType(int $type): ValueType { * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED * @internal - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading * @see setValueString() * @see setValueInt() @@ -766,7 +838,7 @@ public function setValueMixed( string $key, string $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -774,7 +846,7 @@ public function setValueMixed( $key, $value, $lazy, - $sensitive, + $flags, ValueType::MIXED ); } @@ -792,7 +864,7 @@ public function setValueMixed( * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function setValueString( @@ -801,7 +873,7 @@ public function setValueString( string $key, string $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -809,7 +881,7 @@ public function setValueString( $key, $value, $lazy, - $sensitive, + $flags, ValueType::STRING ); } @@ -826,7 +898,7 @@ public function setValueString( * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function setValueInt( @@ -835,7 +907,7 @@ public function setValueInt( string $key, int $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { if ($value > 2000000000) { $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); @@ -847,7 +919,7 @@ public function setValueInt( $key, (string)$value, $lazy, - $sensitive, + $flags, ValueType::INT ); } @@ -864,7 +936,7 @@ public function setValueInt( * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function setValueFloat( @@ -873,7 +945,7 @@ public function setValueFloat( string $key, float $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -881,7 +953,7 @@ public function setValueFloat( $key, (string)$value, $lazy, - $sensitive, + $flags, ValueType::FLOAT ); } @@ -897,7 +969,7 @@ public function setValueFloat( * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function setValueBool( @@ -906,6 +978,7 @@ public function setValueBool( string $key, bool $value, bool $lazy = false, + int $flags = 0 ): bool { return $this->setTypedValue( $userId, @@ -913,7 +986,7 @@ public function setValueBool( $key, ($value) ? '1' : '0', $lazy, - false, + $flags, ValueType::BOOL ); } @@ -931,7 +1004,7 @@ public function setValueBool( * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one * @throws JsonException - * @since 29.0.0 + * @since 31.0.0 * @see IUserPreferences for explanation about lazy loading */ public function setValueArray( @@ -940,7 +1013,7 @@ public function setValueArray( string $key, array $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { try { return $this->setTypedValue( @@ -949,7 +1022,7 @@ public function setValueArray( $key, json_encode($value, JSON_THROW_ON_ERROR), $lazy, - $sensitive, + $flags, ValueType::ARRAY ); } catch (JsonException $e) { @@ -982,7 +1055,7 @@ private function setTypedValue( string $key, string $value, bool $lazy, - bool $sensitive, + int $flags, ValueType $type, ): bool { $this->assertParams($userId, $app, $key, valueType: $type); @@ -990,10 +1063,22 @@ private function setTypedValue( $inserted = $refreshCache = false; $origValue = $value; - $typeValue = $type->value; + $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags); if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); - $typeValue = $typeValue | ValueType::SENSITIVE->value; + $flags |= UserPreferences::FLAG_SENSITIVE; + } + + // if requested, we fill the 'indexed' field with current value + $indexed = ''; + if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) { + if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) { + $this->logger->warning('sensitive value are not to be indexed'); + } else if (strlen($value) > self::USER_MAX_LENGTH) { + $this->logger->warning('value is too lengthy to be indexed'); + } else { + $indexed = $value; + } } if ($this->hasKey($userId, $app, $key, $lazy)) { @@ -1016,7 +1101,9 @@ private function setTypedValue( ->setValue('userid', $insert->createNamedParameter($userId)) ->setValue('appid', $insert->createNamedParameter($app)) ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) - ->setValue('type', $insert->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->setValue('type', $insert->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) + ->setValue('flags', $insert->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) + ->setValue('indexed', $insert->createNamedParameter($indexed)) ->setValue('configkey', $insert->createNamedParameter($key)) ->setValue('configvalue', $insert->createNamedParameter($value)); $insert->executeStatement(); @@ -1032,36 +1119,34 @@ private function setTypedValue( * We cannot insert a new row, meaning we need to update an already existing one */ if (!$inserted) { - $currType = $this->valueTypes[$userId][$app][$key] ?? 0; - if ($currType === 0) { // this might happen when switching lazy loading status + $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; + if ($currType === null) { // this might happen when switching lazy loading status $this->loadPreferencesAll($userId); - $currType = $this->valueTypes[$userId][$app][$key] ?? 0; + $currType = $this->valueDetails[$userId][$app][$key]['type']; } /** - * This should only happen during the upgrade process from 28 to 29. * We only log a warning and set it to VALUE_MIXED. */ - if ($currType === 0) { - $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]); - $currType = ValueType::MIXED->value; + if ($currType === null) { + $this->logger->warning('Value type is set to zero (0) in database. This is not supposed to happens', ['app' => $app, 'key' => $key]); + $currType = ValueType::MIXED; } - // if ($type->isSensitive()) {} - /** * we only accept a different type from the one stored in database * if the one stored in database is not-defined (VALUE_MIXED) */ - if (!$this->isTyped(ValueType::MIXED, $currType) && - ($type->value | ValueType::SENSITIVE->value) !== ($currType | ValueType::SENSITIVE->value)) { + if ($currType !== ValueType::MIXED && + $currType !== $type) { try { - $currType = $this->extractValueType($currType)->getDefinition(); - $type = $type->getDefinition(); + $currTypeDef = $currType->getDefinition(); + $typeDef = $type->getDefinition(); } catch (IncorrectTypeException) { - $type = $type->value; + $currTypeDef = $currType->value; + $typeDef = $type->value; } - throw new TypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')'); + throw new TypeConflictException('conflict between new type (' . $typeDef . ') and old type (' . $currTypeDef . ')'); } if ($lazy !== $this->isLazy($userId, $app, $key)) { @@ -1072,7 +1157,9 @@ private function setTypedValue( $update->update('preferences') ->set('configvalue', $update->createNamedParameter($value)) ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) - ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) + ->set('indexed', $update->createNamedParameter($indexed)) ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); @@ -1091,7 +1178,10 @@ private function setTypedValue( } else { $this->fastCache[$userId][$app][$key] = $value; } - $this->valueTypes[$userId][$app][$key] = $typeValue; + $this->valueDetails[$userId][$app][$key] = [ + 'type' => $type, + 'flags' => $flags + ]; return true; } @@ -1116,26 +1206,16 @@ public function updateType(string $userId, string $app, string $key, ValueType $ $this->assertParams($userId, $app, $key, valueType: $type); $this->loadPreferencesAll($userId); $this->isLazy($userId, $app, $key); // confirm key exists - $typeValue = $type->value; - - $currType = $this->valueTypes[$userId][$app][$key]; - if (($typeValue | ValueType::SENSITIVE->value) === ($currType | ValueType::SENSITIVE->value)) { - return false; - } - - // we complete with sensitive flag if the stored value is set as sensitive - if ($this->isTyped(ValueType::SENSITIVE, $currType)) { - $typeValue = $typeValue | ValueType::SENSITIVE->value; - } $update = $this->connection->getQueryBuilder(); $update->update('preferences') - ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); $update->executeStatement(); - $this->valueTypes[$userId][$app][$key] = $typeValue; + + $this->valueDetails[$userId][$app][$key]['type'] = $type; return true; } @@ -1174,29 +1254,26 @@ public function updateSensitive(string $userId, string $app, string $key, bool $ throw new UnknownKeyException('unknown preference key'); } - /** - * type returned by getValueType() is already cleaned from sensitive flag - * we just need to update it based on $sensitive and store it in database - */ - $typeValue = $this->getValueType($userId, $app, $key)->value; $value = $cache[$userId][$app][$key]; + $flags = $this->getValueFlags($userId, $app, $key); if ($sensitive) { - $typeValue |= ValueType::SENSITIVE->value; + $flags |= self::FLAG_SENSITIVE; $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); } else { + $flags &= ~self::FLAG_SENSITIVE; $this->decryptSensitiveValue($userId, $app, $key, $value); } $update = $this->connection->getQueryBuilder(); $update->update('preferences') - ->set('type', $update->createNamedParameter($typeValue, IQueryBuilder::PARAM_INT)) + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) ->set('configvalue', $update->createNamedParameter($value)) ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); $update->executeStatement(); - $this->valueTypes[$userId][$app][$key] = $typeValue; + $this->valueDetails[$userId][$app][$key]['flags'] = $flags; return true; } @@ -1212,7 +1289,7 @@ public function updateSensitive(string $userId, string $app, string $key, bool $ */ public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void { $this->assertParams('', $app, $key, allowEmptyUser: true); - foreach (array_keys($this->searchValuesByUsers($app, $key)) as $userId) { + foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { try { $this->updateSensitive($userId, $app, $key, $sensitive); } catch (UnknownKeyException) { @@ -1223,6 +1300,89 @@ public function updateGlobalSensitive(string $app, string $key, bool $sensitive) $this->clearCacheAll(); // we clear all cache } + /** + * @inheritDoc + * + * @param string $userId + * @param string $app + * @param string $key + * @param bool $indexed + * + * @return bool + * @throws DBException + * @throws IncorrectTypeException + * @throws UnknownKeyException + * @since 31.0.0 + */ + public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { + $this->assertParams($userId, $app, $key); + $this->loadPreferencesAll($userId); + + try { + if ($indexed === $this->isIndexed($userId, $app, $key, null)) { + return false; + } + } catch (UnknownKeyException) { + return false; + } + + $lazy = $this->isLazy($userId, $app, $key); + if ($lazy) { + $cache = $this->lazyCache; + } else { + $cache = $this->fastCache; + } + + if (!isset($cache[$userId][$app][$key])) { + throw new UnknownKeyException('unknown preference key'); + } + + $value = $cache[$userId][$app][$key]; + $flags = $this->getValueFlags($userId, $app, $key); + if ($indexed) { + $indexed = $value; + } else { + $flags &= ~self::FLAG_INDEXED; + $indexed = ''; + } + + $update = $this->connection->getQueryBuilder(); + $update->update('preferences') + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) + ->set('indexed', $update->createNamedParameter($indexed)) + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + $update->executeStatement(); + + $this->valueDetails[$userId][$app][$key]['flags'] = $flags; + + return true; + } + + + /** + * @inheritDoc + * + * @param string $app + * @param string $key + * @param bool $indexed + * + * @since 31.0.0 + */ + public function updateGlobalIndexed(string $app, string $key, bool $indexed): void { + $this->assertParams('', $app, $key, allowEmptyUser: true); + foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { + try { + $this->updateIndexed($userId, $app, $key, $indexed); + } catch (UnknownKeyException) { + // should not happen and can be ignored + } + } + + $this->clearCacheAll(); // we clear all cache + } + /** * @inheritDoc * @@ -1411,7 +1571,7 @@ public function deleteAllPreferences(string $userId): void { public function clearCache(string $userId, bool $reload = false): void { $this->assertParams($userId, allowEmptyApp: true); $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false; - $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueTypes[$userId] = []; + $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueDetails[$userId] = []; if (!$reload) { return; @@ -1427,7 +1587,7 @@ public function clearCache(string $userId, bool $reload = false): void { */ public function clearCacheAll(): void { $this->lazyLoaded = $this->fastLoaded = []; - $this->lazyCache = $this->fastCache = $this->valueTypes = []; + $this->lazyCache = $this->fastCache = $this->valueDetails = []; } /** @@ -1444,18 +1604,18 @@ public function statusCache(): array { 'fastCache' => $this->fastCache, 'lazyLoaded' => $this->lazyLoaded, 'lazyCache' => $this->lazyCache, - 'valueTypes' => $this->valueTypes, + 'valueDetails' => $this->valueDetails, ]; } /** - * @param ValueType $needle bitflag to search - * @param int $type known value + * @param int $needle bitflag to search + * @param int $flags all flags * - * @return bool TRUE if bitflag $needle is set in $type + * @return bool TRUE if bitflag $needle is set in $flags */ - private function isTyped(ValueType $needle, int $type): bool { - return (($needle->value & $type) !== 0); + private function isFlagged(int $needle, int $flags): bool { + return (($needle & $flags) !== 0); } /** @@ -1492,11 +1652,10 @@ private function assertParams( throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); } if ($valueType !== null) { - $valueFlag = $valueType->value; - $valueFlag &= ~ValueType::SENSITIVE->value; - if (ValueType::tryFrom($valueFlag) === null) { - throw new InvalidArgumentException('Unknown value type'); - } +// $valueFlag = $valueType->value; +// if (ValueType::tryFrom($valueFlag) === null) { +// throw new InvalidArgumentException('Unknown value type'); +// } } } @@ -1520,7 +1679,7 @@ private function loadPreferences(string $userId, ?bool $lazy = false): void { $qb = $this->connection->getQueryBuilder(); $qb->from('preferences'); - $qb->select('userid', 'appid', 'configkey', 'configvalue', 'type'); + $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags'); $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); // we only need value from lazy when loadPreferences does not specify it @@ -1534,11 +1693,11 @@ private function loadPreferences(string $userId, ?bool $lazy = false): void { $rows = $result->fetchAll(); foreach ($rows as $row) { if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { - $this->lazyCache[$row['userid']][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; + $this->lazyCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; } else { - $this->fastCache[$row['userid']][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; + $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; } - $this->valueTypes[$row['userid']][$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0); + $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int) $row['flags']]; } $result->closeCursor(); $this->setAsLoaded($userId, $lazy); @@ -1608,7 +1767,7 @@ private function formatAppValues(string $userId, string $app, array $values, boo continue; } - if ($this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key] ?? 0)) { + if ($this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { if ($filtered) { $value = IConfig::SENSITIVE_VALUE; $type = ValueType::STRING; @@ -1652,7 +1811,7 @@ private function convertTypedValue(string $value, ValueType $type): string|int|f private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void { - if (!$this->isTyped(ValueType::SENSITIVE, $this->valueTypes[$userId][$app][$key] ?? 0)) { + if (!$this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { return; } diff --git a/lib/public/UserPreferences/IUserPreferences.php b/lib/public/UserPreferences/IUserPreferences.php index e050f2ea6af0d..f2635c0ffafbf 100644 --- a/lib/public/UserPreferences/IUserPreferences.php +++ b/lib/public/UserPreferences/IUserPreferences.php @@ -15,6 +15,9 @@ * @since 31.0.0 */ interface IUserPreferences { + public const FLAG_SENSITIVE = 1; // value is sensitive + public const FLAG_INDEXED = 2; // value should be indexed + /** * Get list of all userIds with preferences stored in database. * If $appId is specified, will only limit the search to this value @@ -82,6 +85,25 @@ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = f */ public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool; + /** + * best way to see if a value is set as indexed (so it can be search) + * + * @see self::searchUsersByValueString() + * @see self::searchUsersByValueInt() + * @see self::searchUsersByValueBool() + * @see self::searchUsersByValues() + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool|null $lazy search within lazy loaded preferences + * + * @return bool TRUE if value is sensitive + * @throws UnknownKeyException if preference key is not known + * @since 31.0.0 + */ + public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool; + /** * Returns if the preference key stored in database is lazy loaded * @@ -140,7 +162,7 @@ public function getAllValues(string $userId, bool $filtered = false): array; * @return array [appId => value] * @since 31.0.0 */ - public function searchValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array; + public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array; /** * List all users storing a specific preference key and its stored value. @@ -156,7 +178,7 @@ public function searchValuesByApps(string $userId, string $key, bool $lazy = fal * @return array [userId => value] * @since 31.0.0 */ - public function searchValuesByUsers(string $app, string $key, ?ValueType $typedAs = null, ?array $userIds = null): array; + public function getValuesByUsers(string $app, string $key, ?ValueType $typedAs = null, ?array $userIds = null): array; /** * List all users storing a specific preference key/value pair. @@ -342,6 +364,24 @@ public function getValueArray(string $userId, string $app, string $key, array $d */ public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType; + /** + * returns a bitflag related to preference value + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * unless lazy is set to false + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $lazy lazy loading + * + * @return int a bitflag in relation to the preference value + * @throws UnknownKeyException if preference key is not known + * @throws IncorrectTypeException if preferences value type is not known + * @since 31.0.0 + */ + public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int; + /** * Store a preference key and its value in database * @@ -365,7 +405,7 @@ public function getValueType(string $userId, string $app, string $key, ?bool $la * @see setValueBool() * @see setValueArray() */ - public function setValueString(string $userId, string $app, string $key, string $value, bool $lazy = false, bool $sensitive = false): bool; + public function setValueString(string $userId, string $app, string $key, string $value, bool $lazy = false, int $flags = 0): bool; /** * Store a preference key and its value in database @@ -395,7 +435,7 @@ public function setValueString(string $userId, string $app, string $key, string * @see setValueBool() * @see setValueArray() */ - public function setValueInt(string $userId, string $app, string $key, int $value, bool $lazy = false, bool $sensitive = false): bool; + public function setValueInt(string $userId, string $app, string $key, int $value, bool $lazy = false, int $flags = 0): bool; /** * Store a preference key and its value in database. @@ -420,7 +460,7 @@ public function setValueInt(string $userId, string $app, string $key, int $value * @see setValueBool() * @see setValueArray() */ - public function setValueFloat(string $userId, string $app, string $key, float $value, bool $lazy = false, bool $sensitive = false): bool; + public function setValueFloat(string $userId, string $app, string $key, float $value, bool $lazy = false, int $flags = 0): bool; /** * Store a preference key and its value in database @@ -469,7 +509,7 @@ public function setValueBool(string $userId, string $app, string $key, bool $val * @see setValueFloat() * @see setValueBool() */ - public function setValueArray(string $userId, string $app, string $key, array $value, bool $lazy = false, bool $sensitive = false): bool; + public function setValueArray(string $userId, string $app, string $key, array $value, bool $lazy = false, int $flags = 0): bool; /** * switch sensitive status of a preference value @@ -491,7 +531,6 @@ public function updateSensitive(string $userId, string $app, string $key, bool $ * * **Warning:** heavy on resources, MUST only be used on occ command or migrations * - * * @param string $app id of the app * @param string $key preference key * @param bool $sensitive TRUE to set as sensitive, FALSE to unset @@ -500,6 +539,34 @@ public function updateSensitive(string $userId, string $app, string $key, bool $ */ public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void; + + /** + * switch indexed status of a preference value + * + * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * + * @param string $userId id of the user + * @param string $app id of the app + * @param string $key preference key + * @param bool $indexed TRUE to set as indexed, FALSE to unset + * + * @return bool TRUE if database update were necessary + * @since 31.0.0 + */ + public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool; + + /** + * switch sensitive loading status of a preference key for all users + * + * **Warning:** heavy on resources, MUST only be used on occ command or migrations + * + * @param string $app id of the app + * @param string $key preference key + * @param bool $indexed TRUE to set as indexed, FALSE to unset + * @since 31.0.0 + */ + public function updateGlobalIndexed(string $app, string $key, bool $indexed): void; + /** * switch lazy loading status of a preference value * diff --git a/lib/public/UserPreferences/ValueType.php b/lib/public/UserPreferences/ValueType.php index 57f65b504c7ff..280dc5a8b4248 100644 --- a/lib/public/UserPreferences/ValueType.php +++ b/lib/public/UserPreferences/ValueType.php @@ -10,7 +10,6 @@ use OCP\UserPreferences\Exceptions\IncorrectTypeException; use UnhandledMatchError; -use ValueError; /** * Listing of available value type for user preferences @@ -20,22 +19,20 @@ */ enum ValueType: int { /** @since 31.0.0 */ - case SENSITIVE = 1; + case MIXED = 0; /** @since 31.0.0 */ - case MIXED = 2; + case STRING = 1; /** @since 31.0.0 */ - case STRING = 4; + case INT = 2; /** @since 31.0.0 */ - case INT = 8; + case FLOAT = 3; /** @since 31.0.0 */ - case FLOAT = 16; + case BOOL = 4; /** @since 31.0.0 */ - case BOOL = 32; - /** @since 31.0.0 */ - case ARRAY = 64; + case ARRAY = 5; /** - * get ValueType from string based on ValueTypeDefinition + * get ValueType from string * * @param string $definition * @@ -43,35 +40,18 @@ enum ValueType: int { * @throws IncorrectTypeException * @since 31.0.0 */ - public function fromStringDefinition(string $definition): self { - try { - return $this->fromValueDefinition(ValueTypeDefinition::from($definition)); - } catch (ValueError) { - throw new IncorrectTypeException('unknown string definition'); - } - } - - /** - * get ValueType from ValueTypeDefinition - * - * @param ValueTypeDefinition $definition - * - * @return self - * @throws IncorrectTypeException - * @since 31.0.0 - */ - public function fromValueDefinition(ValueTypeDefinition $definition): self { + public static function fromStringDefinition(string $definition): self { try { return match ($definition) { - ValueTypeDefinition::MIXED => self::MIXED, - ValueTypeDefinition::STRING => self::STRING, - ValueTypeDefinition::INT => self::INT, - ValueTypeDefinition::FLOAT => self::FLOAT, - ValueTypeDefinition::BOOL => self::BOOL, - ValueTypeDefinition::ARRAY => self::ARRAY + 'mixed' => self::MIXED, + 'string' => self::STRING, + 'int' => self::INT, + 'float' => self::FLOAT, + 'bool' => self::BOOL, + 'array' => self::ARRAY }; - } catch (UnhandledMatchError) { - throw new IncorrectTypeException('unknown definition ' . $definition->value); + } catch (\UnhandledMatchError ) { + throw new IncorrectTypeException('unknown string definition'); } } @@ -83,26 +63,14 @@ public function fromValueDefinition(ValueTypeDefinition $definition): self { * @since 31.0.0 */ public function getDefinition(): string { - return $this->getValueTypeDefinition()->value; - } - - /** - * get ValueTypeDefinition for current enum value - * - * @return ValueTypeDefinition - * @throws IncorrectTypeException - * @since 31.0.0 - */ - public function getValueTypeDefinition(): ValueTypeDefinition { try { - /** @psalm-suppress UnhandledMatchCondition */ return match ($this) { - self::MIXED => ValueTypeDefinition::MIXED, - self::STRING => ValueTypeDefinition::STRING, - self::INT => ValueTypeDefinition::INT, - self::FLOAT => ValueTypeDefinition::FLOAT, - self::BOOL => ValueTypeDefinition::BOOL, - self::ARRAY => ValueTypeDefinition::ARRAY, + self::MIXED => 'mixed', + self::STRING => 'string', + self::INT => 'int', + self::FLOAT => 'float', + self::BOOL => 'bool', + self::ARRAY => 'array', }; } catch (UnhandledMatchError) { throw new IncorrectTypeException('unknown type definition ' . $this->value); diff --git a/lib/public/UserPreferences/ValueTypeDefinition.php b/lib/public/UserPreferences/ValueTypeDefinition.php deleted file mode 100644 index 116c20b3bafb0..0000000000000 --- a/lib/public/UserPreferences/ValueTypeDefinition.php +++ /dev/null @@ -1,30 +0,0 @@ - ['fast_string', 'f_value', ValueType::STRING], 'lazy_string' => ['lazy_string', 'l_value', ValueType::STRING, true], 'fast_string_sensitive' => [ - 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, true + 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE ], 'lazy_string_sensitive' => [ - 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, true + 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE ], 'fast_int' => ['fast_int', 11, ValueType::INT], 'lazy_int' => ['lazy_int', 12, ValueType::INT, true], - 'fast_int_sensitive' => ['fast_int_sensitive', 2024, ValueType::INT, false, true], - 'lazy_int_sensitive' => ['lazy_int_sensitive', 2048, ValueType::INT, true, true], + 'fast_int_sensitive' => ['fast_int_sensitive', 2024, ValueType::INT, false, UserPreferences::FLAG_SENSITIVE], + 'lazy_int_sensitive' => ['lazy_int_sensitive', 2048, ValueType::INT, true, UserPreferences::FLAG_SENSITIVE], 'fast_float' => ['fast_float', 3.14, ValueType::FLOAT], 'lazy_float' => ['lazy_float', 3.14159, ValueType::FLOAT, true], 'fast_float_sensitive' => [ - 'fast_float_sensitive', 1.41, ValueType::FLOAT, false, true + 'fast_float_sensitive', 1.41, ValueType::FLOAT, false, UserPreferences::FLAG_SENSITIVE ], 'lazy_float_sensitive' => [ - 'lazy_float_sensitive', 1.4142, ValueType::FLOAT, true, true + 'lazy_float_sensitive', 1.4142, ValueType::FLOAT, true, UserPreferences::FLAG_SENSITIVE ], 'fast_array' => ['fast_array', ['year' => 2024], ValueType::ARRAY], 'lazy_array' => ['lazy_array', ['month' => 'October'], ValueType::ARRAY, true], 'fast_array_sensitive' => [ - 'fast_array_sensitive', ['password' => 'pwd'], ValueType::ARRAY, false, true + 'fast_array_sensitive', ['password' => 'pwd'], ValueType::ARRAY, false, UserPreferences::FLAG_SENSITIVE ], 'lazy_array_sensitive' => [ - 'lazy_array_sensitive', ['password' => 'qwerty'], ValueType::ARRAY, true, true + 'lazy_array_sensitive', ['password' => 'qwerty'], ValueType::ARRAY, true, UserPreferences::FLAG_SENSITIVE ], 'fast_boolean' => ['fast_boolean', true, ValueType::BOOL], 'fast_boolean_0' => ['fast_boolean_0', false, ValueType::BOOL], @@ -74,22 +74,22 @@ class UserPreferencesTest extends TestCase { 'lazy_boolean_0' => ['lazy_boolean_0', false, ValueType::BOOL, true], ], 'app2' => [ - 'key2' => ['key2', 'value2a', ValueType::STRING], - 'key3' => ['key3', 'value3', ValueType::STRING, true, false], - 'key4' => ['key4', 'value4', ValueType::STRING, false, true], - 'key8' => ['key8', 11, ValueType::INT], + 'key2' => ['key2', 'value2a', ValueType::STRING, false, 0, true], + 'key3' => ['key3', 'value3', ValueType::STRING, true], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key8' => ['key8', 11, ValueType::INT, false, 0, true], 'key9' => ['key9', 'value9a', ValueType::STRING], ], 'app3' => [ 'key1' => ['key1', 'value123'], 'key3' => ['key3', 'value3'], - 'key8' => ['key8', 12, ValueType::INT, false, true], - 'key9' => ['key9', 'value9b', ValueType::STRING, false, true], - 'key10' => ['key10', true, ValueType::BOOL], + 'key8' => ['key8', 12, ValueType::INT, false, UserPreferences::FLAG_SENSITIVE, true], + 'key9' => ['key9', 'value9b', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key10' => ['key10', true, ValueType::BOOL, false, 0, true], ], 'only-lazy' => [ - 'key1' => ['key1', 'value456', ValueType::STRING, true], - 'key2' => ['key2', 'value2c', ValueType::STRING, true, true], + 'key1' => ['key1', 'value456', ValueType::STRING, true, 0, true], + 'key2' => ['key2', 'value2c', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE], 'key3' => ['key3', 42, ValueType::INT, true], 'key4' => ['key4', 17.42, ValueType::FLOAT, true], 'key5' => ['key5', true, ValueType::BOOL, true], @@ -99,36 +99,36 @@ class UserPreferencesTest extends TestCase { [ 'app1' => [ '1' => ['1', 'value1'], - '2' => ['2', 'value2', ValueType::STRING, true, true], + '2' => ['2', 'value2', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE], '3' => ['3', 17, ValueType::INT, true], - '4' => ['4', 42, ValueType::INT, false, true], + '4' => ['4', 42, ValueType::INT, false, UserPreferences::FLAG_SENSITIVE], '5' => ['5', 17.42, ValueType::FLOAT, false], '6' => ['6', true, ValueType::BOOL, false], ], 'app2' => [ - 'key2' => ['key2', 'value2b', ValueType::STRING], - 'key3' => ['key3', 'value3', ValueType::STRING, true, false], - 'key4' => ['key4', 'value4', ValueType::STRING, false, true], - 'key8' => ['key8', 12, ValueType::INT], + 'key2' => ['key2', 'value2b', ValueType::STRING, false, 0, true], + 'key3' => ['key3', 'value3', ValueType::STRING, true], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key8' => ['key8', 12, ValueType::INT, false, 0, true], ], 'app3' => [ - 'key10' => ['key10', false, ValueType::BOOL], + 'key10' => ['key10', false, ValueType::BOOL, false, 0, true], ], 'only-lazy' => [ - 'key1' => ['key1', 'value1', ValueType::STRING, true] + 'key1' => ['key1', 'value1', ValueType::STRING, true, 0, true] ] ], 'user3' => [ 'app2' => [ - 'key2' => ['key2', 'value2c'], - 'key3' => ['key3', 'value3', ValueType::STRING, true, false], - 'key4' => ['key4', 'value4', ValueType::STRING, false, true], + 'key2' => ['key2', 'value2c', ValueType::MIXED, false, 0, true], + 'key3' => ['key3', 'value3', ValueType::STRING, true, ], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], 'fast_string_sensitive' => [ - 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, true + 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE ], 'lazy_string_sensitive' => [ - 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, true + 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE ], ], 'only-lazy' => [ @@ -139,15 +139,15 @@ class UserPreferencesTest extends TestCase { [ 'app2' => [ 'key1' => ['key1', 'value1'], - 'key2' => ['key2', 'value2A'], - 'key3' => ['key3', 'value3', ValueType::STRING, true, false], - 'key4' => ['key4', 'value4', ValueType::STRING, false, true], + 'key2' => ['key2', 'value2A', ValueType::MIXED, false, 0, true], + 'key3' => ['key3', 'value3', ValueType::STRING, true,], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], ], 'app3' => [ - 'key10' => ['key10', true, ValueType::BOOL], + 'key10' => ['key10', true, ValueType::BOOL, false, 0, true], ], 'only-lazy' => [ - 'key1' => ['key1', 123, ValueType::INT, true] + 'key1' => ['key1', 123, ValueType::INT, true, 0, true] ] ], 'user5' => @@ -156,10 +156,10 @@ class UserPreferencesTest extends TestCase { 'key1' => ['key1', 'value1'] ], 'app2' => [ - 'key8' => ['key8', 12, ValueType::INT] + 'key8' => ['key8', 12, ValueType::INT, false, 0, true] ], 'only-lazy' => [ - 'key1' => ['key1', 'value1', ValueType::STRING, true] + 'key1' => ['key1', 'value1', ValueType::STRING, true, 0, true] ] ], @@ -193,7 +193,9 @@ protected function setUp(): void { 'configkey' => $sql->createParameter('configkey'), 'configvalue' => $sql->createParameter('configvalue'), 'type' => $sql->createParameter('type'), - 'lazy' => $sql->createParameter('lazy') + 'lazy' => $sql->createParameter('lazy'), + 'flags' => $sql->createParameter('flags'), + 'indexed' => $sql->createParameter('indexed') ] ); @@ -207,17 +209,18 @@ protected function setUp(): void { $value = json_encode($value); } - if (($row[4] ?? false) === true) { - $type |= ValueType::SENSITIVE->value; - $value = self::invokePrivate(UserPreferences::class, 'ENCRYPTION_PREFIX') - . $this->crypto->encrypt((string)$value); - $this->basePreferences[$userId][$appId][$key]['encrypted'] = $value; - } - if ($type === ValueType::BOOL->value && $value === false) { $value = '0'; } + $flags = $row[4] ?? 0; + if ((UserPreferences::FLAG_SENSITIVE & $flags) !== 0) { + $value = self::invokePrivate(UserPreferences::class, 'ENCRYPTION_PREFIX') + . $this->crypto->encrypt((string)$value); + } else { + $indexed = (($row[5] ?? false) === true) ? $value : ''; + } + $sql->setParameters( [ 'userid' => $userId, @@ -225,7 +228,9 @@ protected function setUp(): void { 'configkey' => $row[0], 'configvalue' => $value, 'type' => $type, - 'lazy' => (($row[3] ?? false) === true) ? 1 : 0 + 'lazy' => (($row[3] ?? false) === true) ? 1 : 0, + 'flags' => $flags, + 'indexed' => $indexed ?? '' ] )->executeStatement(); } @@ -696,7 +701,7 @@ public function testSearchValuesByApps( array $result, ): void { $preferences = $this->generateUserPreferences(); - $this->assertEquals($result, $preferences->searchValuesByApps($userId, $key, $lazy, $typedAs)); + $this->assertEquals($result, $preferences->getValuesByApps($userId, $key, $lazy, $typedAs)); } public function providerSearchValuesByUsers(): array { @@ -747,7 +752,7 @@ public function testSearchValuesByUsers( ): void { $preferences = $this->generateUserPreferences(); $this->assertEqualsCanonicalizing( - $result, $preferences->searchValuesByUsers($app, $key, $typedAs, $userIds) + $result, $preferences->getValuesByUsers($app, $key, $typedAs, $userIds) ); } @@ -770,9 +775,7 @@ public function testSearchUsersByValueString( array $result, ): void { $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing( - $result, $preferences->searchUsersByValueString($app, $key, $value, $ci) - ); + $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueString($app, $key, $value, $ci)); } public function providerSearchValuesByValueInt(): array { @@ -1235,7 +1238,7 @@ public function testSetValueMixed( $this->expectException($exception); } - $edited = $preferences->setValueMixed($userId, $app, $key, $value, $lazy, $sensitive); + $edited = $preferences->setValueMixed($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception === null) { $this->assertEquals($result, $edited); @@ -1305,7 +1308,7 @@ public function testSetValueString( $this->expectException($exception); } - $edited = $preferences->setValueString($userId, $app, $key, $value, $lazy, $sensitive); + $edited = $preferences->setValueString($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; } @@ -1368,7 +1371,7 @@ public function testSetValueInt( $this->expectException($exception); } - $edited = $preferences->setValueInt($userId, $app, $key, $value, $lazy, $sensitive); + $edited = $preferences->setValueInt($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; @@ -1431,7 +1434,7 @@ public function testSetValueFloat( $this->expectException($exception); } - $edited = $preferences->setValueFloat($userId, $app, $key, $value, $lazy, $sensitive); + $edited = $preferences->setValueFloat($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; @@ -1495,7 +1498,7 @@ public function testSetValueArray( $this->expectException($exception); } - $edited = $preferences->setValueArray($userId, $app, $key, $value, $lazy, $sensitive); + $edited = $preferences->setValueArray($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; @@ -1681,7 +1684,7 @@ public function providerGetDetails(): array { 'app' => 'app2', 'key' => 'key2', 'value' => 'value2c', - 'type' => 2, + 'type' => 0, 'lazy' => false, 'typeString' => 'mixed', 'sensitive' => false @@ -1694,7 +1697,7 @@ public function providerGetDetails(): array { 'app' => 'app1', 'key' => 'lazy_int', 'value' => 12, - 'type' => 8, + 'type' => 2, 'lazy' => true, 'typeString' => 'int', 'sensitive' => false @@ -1707,7 +1710,7 @@ public function providerGetDetails(): array { 'app' => 'app1', 'key' => 'fast_float_sensitive', 'value' => 1.41, - 'type' => 16, + 'type' => 3, 'lazy' => false, 'typeString' => 'float', 'sensitive' => true From 7c04818c5c078fb1dc7c5b79f295fc40a60a6296 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 21 Oct 2024 18:31:14 -0100 Subject: [PATCH 3/5] feat(user-prefs): iterator instead of array on search Signed-off-by: Maxence Lange --- .../Version31000Date20240814184402.php | 2 +- lib/composer/composer/autoload_classmap.php | 14 ++- lib/composer/composer/autoload_static.php | 14 ++- lib/private/AllConfig.php | 15 +-- lib/private/{ => Config}/UserPreferences.php | 102 +++++++----------- lib/private/Server.php | 4 +- .../Exceptions/IncorrectTypeException.php} | 4 +- .../Exceptions/TypeConflictException.php | 6 +- .../Exceptions/UnknownKeyException.php | 6 +- .../IUserPreferences.php | 40 +++++-- .../{UserPreferences => Config}/ValueType.php | 9 +- lib/public/IConfig.php | 2 +- .../Exceptions/IncorrectTypeException.php | 15 --- tests/lib/UserPreferencesTest.php | 20 ++-- 14 files changed, 114 insertions(+), 139 deletions(-) rename lib/private/{ => Config}/UserPreferences.php (94%) rename lib/public/{UserPreferences/Exceptions/UserPreferencesException.php => Config/Exceptions/IncorrectTypeException.php} (68%) rename lib/public/{UserPreferences => Config}/Exceptions/TypeConflictException.php (64%) rename lib/public/{UserPreferences => Config}/Exceptions/UnknownKeyException.php (64%) rename lib/public/{UserPreferences => Config}/IUserPreferences.php (95%) rename lib/public/{UserPreferences => Config}/ValueType.php (87%) delete mode 100644 lib/public/UserPreferences/Exceptions/IncorrectTypeException.php diff --git a/core/Migrations/Version31000Date20240814184402.php b/core/Migrations/Version31000Date20240814184402.php index 87b7e93db97d0..14b32a704beda 100644 --- a/core/Migrations/Version31000Date20240814184402.php +++ b/core/Migrations/Version31000Date20240814184402.php @@ -38,7 +38,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table->addColumn('lazy', Types::SMALLINT, ['notnull' => true, 'default' => 0, 'length' => 1, 'unsigned' => true]); $table->addColumn('type', Types::SMALLINT, ['notnull' => true, 'default' => 0, 'unsigned' => true]); $table->addColumn('flags', Types::INTEGER, ['notnull' => true, 'default' => 0, 'unsigned' => true]); - $table->addColumn('indexed', Types::STRING, ['notnull' => true, 'default' => '', 'length' => 64]); + $table->addColumn('indexed', Types::STRING, ['notnull' => false, 'default' => '', 'length' => 64]); // removing this index from Version13000Date20170718121200 // $table->addIndex(['appid', 'configkey'], 'preferences_app_key'); diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 3e65291819b88..c3abaefd56c55 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -223,6 +223,11 @@ 'OCP\\Common\\Exception\\NotFoundException' => $baseDir . '/lib/public/Common/Exception/NotFoundException.php', 'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => $baseDir . '/lib/public/Config/BeforePreferenceSetEvent.php', + 'OCP\\Config\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/public/Config/Exceptions/IncorrectTypeException.php', + 'OCP\\Config\\Exceptions\\TypeConflictException' => $baseDir . '/lib/public/Config/Exceptions/TypeConflictException.php', + 'OCP\\Config\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/public/Config/Exceptions/UnknownKeyException.php', + 'OCP\\Config\\IUserPreferences' => $baseDir . '/lib/public/Config/IUserPreferences.php', + 'OCP\\Config\\ValueType' => $baseDir . '/lib/public/Config/ValueType.php', 'OCP\\Console\\ConsoleEvent' => $baseDir . '/lib/public/Console/ConsoleEvent.php', 'OCP\\Console\\ReservedOptions' => $baseDir . '/lib/public/Console/ReservedOptions.php', 'OCP\\Constants' => $baseDir . '/lib/public/Constants.php', @@ -839,13 +844,6 @@ 'OCP\\UserMigration\\ISizeEstimationMigrator' => $baseDir . '/lib/public/UserMigration/ISizeEstimationMigrator.php', 'OCP\\UserMigration\\TMigratorBasicVersionHandling' => $baseDir . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php', 'OCP\\UserMigration\\UserMigrationException' => $baseDir . '/lib/public/UserMigration/UserMigrationException.php', - 'OCP\\UserPreferences\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php', - 'OCP\\UserPreferences\\Exceptions\\TypeConflictException' => $baseDir . '/lib/public/UserPreferences/Exceptions/TypeConflictException.php', - 'OCP\\UserPreferences\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/public/UserPreferences/Exceptions/UnknownKeyException.php', - 'OCP\\UserPreferences\\Exceptions\\UserPreferencesException' => $baseDir . '/lib/public/UserPreferences/Exceptions/UserPreferencesException.php', - 'OCP\\UserPreferences\\IUserPreferences' => $baseDir . '/lib/public/UserPreferences/IUserPreferences.php', - 'OCP\\UserPreferences\\ValueType' => $baseDir . '/lib/public/UserPreferences/ValueType.php', - 'OCP\\UserPreferences\\ValueTypeDefinition' => $baseDir . '/lib/public/UserPreferences/ValueTypeDefinition.php', 'OCP\\UserStatus\\IManager' => $baseDir . '/lib/public/UserStatus/IManager.php', 'OCP\\UserStatus\\IProvider' => $baseDir . '/lib/public/UserStatus/IProvider.php', 'OCP\\UserStatus\\IUserStatus' => $baseDir . '/lib/public/UserStatus/IUserStatus.php', @@ -1125,6 +1123,7 @@ 'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php', 'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php', 'OC\\Config' => $baseDir . '/lib/private/Config.php', + 'OC\\Config\\UserPreferences' => $baseDir . '/lib/private/Config/UserPreferences.php', 'OC\\Console\\Application' => $baseDir . '/lib/private/Console/Application.php', 'OC\\Console\\TimestampFormatter' => $baseDir . '/lib/private/Console/TimestampFormatter.php', 'OC\\ContactsManager' => $baseDir . '/lib/private/ContactsManager.php', @@ -2005,7 +2004,6 @@ 'OC\\Updater\\Exceptions\\ReleaseMetadataException' => $baseDir . '/lib/private/Updater/Exceptions/ReleaseMetadataException.php', 'OC\\Updater\\ReleaseMetadata' => $baseDir . '/lib/private/Updater/ReleaseMetadata.php', 'OC\\Updater\\VersionCheck' => $baseDir . '/lib/private/Updater/VersionCheck.php', - 'OC\\UserPreferences' => $baseDir . '/lib/private/UserPreferences.php', 'OC\\UserStatus\\ISettableProvider' => $baseDir . '/lib/private/UserStatus/ISettableProvider.php', 'OC\\UserStatus\\Manager' => $baseDir . '/lib/private/UserStatus/Manager.php', 'OC\\User\\AvailabilityCoordinator' => $baseDir . '/lib/private/User/AvailabilityCoordinator.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 46e59b2584696..cf821ddeba058 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -264,6 +264,11 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Common\\Exception\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Common/Exception/NotFoundException.php', 'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceSetEvent.php', + 'OCP\\Config\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/IncorrectTypeException.php', + 'OCP\\Config\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/TypeConflictException.php', + 'OCP\\Config\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/UnknownKeyException.php', + 'OCP\\Config\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/public/Config/IUserPreferences.php', + 'OCP\\Config\\ValueType' => __DIR__ . '/../../..' . '/lib/public/Config/ValueType.php', 'OCP\\Console\\ConsoleEvent' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEvent.php', 'OCP\\Console\\ReservedOptions' => __DIR__ . '/../../..' . '/lib/public/Console/ReservedOptions.php', 'OCP\\Constants' => __DIR__ . '/../../..' . '/lib/public/Constants.php', @@ -880,13 +885,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\UserMigration\\ISizeEstimationMigrator' => __DIR__ . '/../../..' . '/lib/public/UserMigration/ISizeEstimationMigrator.php', 'OCP\\UserMigration\\TMigratorBasicVersionHandling' => __DIR__ . '/../../..' . '/lib/public/UserMigration/TMigratorBasicVersionHandling.php', 'OCP\\UserMigration\\UserMigrationException' => __DIR__ . '/../../..' . '/lib/public/UserMigration/UserMigrationException.php', - 'OCP\\UserPreferences\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php', - 'OCP\\UserPreferences\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/TypeConflictException.php', - 'OCP\\UserPreferences\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/UnknownKeyException.php', - 'OCP\\UserPreferences\\Exceptions\\UserPreferencesException' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/Exceptions/UserPreferencesException.php', - 'OCP\\UserPreferences\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/IUserPreferences.php', - 'OCP\\UserPreferences\\ValueType' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/ValueType.php', - 'OCP\\UserPreferences\\ValueTypeDefinition' => __DIR__ . '/../../..' . '/lib/public/UserPreferences/ValueTypeDefinition.php', 'OCP\\UserStatus\\IManager' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IManager.php', 'OCP\\UserStatus\\IProvider' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IProvider.php', 'OCP\\UserStatus\\IUserStatus' => __DIR__ . '/../../..' . '/lib/public/UserStatus/IUserStatus.php', @@ -1166,6 +1164,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php', 'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php', 'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php', + 'OC\\Config\\UserPreferences' => __DIR__ . '/../../..' . '/lib/private/Config/UserPreferences.php', 'OC\\Console\\Application' => __DIR__ . '/../../..' . '/lib/private/Console/Application.php', 'OC\\Console\\TimestampFormatter' => __DIR__ . '/../../..' . '/lib/private/Console/TimestampFormatter.php', 'OC\\ContactsManager' => __DIR__ . '/../../..' . '/lib/private/ContactsManager.php', @@ -2046,7 +2045,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Updater\\Exceptions\\ReleaseMetadataException' => __DIR__ . '/../../..' . '/lib/private/Updater/Exceptions/ReleaseMetadataException.php', 'OC\\Updater\\ReleaseMetadata' => __DIR__ . '/../../..' . '/lib/private/Updater/ReleaseMetadata.php', 'OC\\Updater\\VersionCheck' => __DIR__ . '/../../..' . '/lib/private/Updater/VersionCheck.php', - 'OC\\UserPreferences' => __DIR__ . '/../../..' . '/lib/private/UserPreferences.php', 'OC\\UserStatus\\ISettableProvider' => __DIR__ . '/../../..' . '/lib/private/UserStatus/ISettableProvider.php', 'OC\\UserStatus\\Manager' => __DIR__ . '/../../..' . '/lib/private/UserStatus/Manager.php', 'OC\\User\\AvailabilityCoordinator' => __DIR__ . '/../../..' . '/lib/private/User/AvailabilityCoordinator.php', diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index b2ebe322d9eef..8f7e5359c967f 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -6,13 +6,14 @@ */ namespace OC; +use OC\Config\UserPreferences; use OCP\Cache\CappedMemoryCache; +use OCP\Config\Exceptions\TypeConflictException; +use OCP\Config\IUserPreferences; +use OCP\Config\ValueType; use OCP\IConfig; use OCP\IDBConnection; use OCP\PreConditionNotMetException; -use OCP\UserPreferences\Exceptions\TypeConflictException; -use OCP\UserPreferences\IUserPreferences; -use OCP\UserPreferences\ValueType; /** * Class to combine all the configuration options ownCloud offers @@ -376,11 +377,11 @@ public function getUserValueForUsers($appName, $key, $userIds) { * @param string $appName the app to get the user for * @param string $key the key to get the user for * @param string $value the value to get the user for - * @return list of user IDs + * @return array of user IDs * @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly */ public function getUsersForUserValue($appName, $key, $value) { - return \OCP\Server::get(IUserPreferences::class)->searchUsersByValueDeprecated($appName, $key, $value); + return iterator_to_array(\OCP\Server::get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value)); } /** @@ -389,7 +390,7 @@ public function getUsersForUserValue($appName, $key, $value) { * @param string $appName the app to get the user for * @param string $key the key to get the user for * @param string $value the value to get the user for - * @return list of user IDs + * @return array of user IDs * @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly */ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) { @@ -397,7 +398,7 @@ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) { return $this->getUsersForUserValue($appName, $key, strtolower($value)); } - return \OCP\Server::get(IUserPreferences::class)->searchUsersByValueDeprecated($appName, $key, $value, true); + return iterator_to_array(\OCP\Server::get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value, true)); } public function getSystemConfig() { diff --git a/lib/private/UserPreferences.php b/lib/private/Config/UserPreferences.php similarity index 94% rename from lib/private/UserPreferences.php rename to lib/private/Config/UserPreferences.php index 22313292e94af..6dd9c63383671 100644 --- a/lib/private/UserPreferences.php +++ b/lib/private/Config/UserPreferences.php @@ -6,21 +6,22 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OC; +namespace OC\Config; +use Generator; use InvalidArgumentException; use JsonException; +use OCP\Config\Exceptions\IncorrectTypeException; +use OCP\Config\Exceptions\TypeConflictException; +use OCP\Config\Exceptions\UnknownKeyException; +use OCP\Config\IUserPreferences; +use OCP\Config\ValueType; use OCP\DB\Exception as DBException; use OCP\DB\IResult; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IConfig; use OCP\IDBConnection; use OCP\Security\ICrypto; -use OCP\UserPreferences\Exceptions\IncorrectTypeException; -use OCP\UserPreferences\Exceptions\TypeConflictException; -use OCP\UserPreferences\Exceptions\UnknownKeyException; -use OCP\UserPreferences\IUserPreferences; -use OCP\UserPreferences\ValueType; use Psr\Log\LoggerInterface; /** @@ -52,7 +53,7 @@ class UserPreferences implements IUserPreferences { private array $fastCache = []; // cache for normal preference keys /** @var array>> ['user_id' => ['app_id' => ['key' => 'value']]] */ private array $lazyCache = []; // cache for lazy preference keys - /** @var array|<'flags', int>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]] */ + /** @var array>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */ private array $valueDetails = []; // type for all preference values /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ private array $valueTypes = []; // type for all preference values @@ -358,7 +359,7 @@ public function getValuesByUsers( while ($row = $result->fetch()) { $value = $row['configvalue']; try { - $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int) $row['type'])); + $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type'])); } catch (IncorrectTypeException) { } $values[$row['userid']] = $value; @@ -393,29 +394,13 @@ public function getValuesByUsers( * @param string $value preference value * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): array { + public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator { return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); } - /** - * @inheritDoc - * - * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value - * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string - * @internal - * @deprecated since 31.0.0 - {@see } - * @return list - * @since 31.0.0 - */ - public function searchUsersByValueDeprecated(string $app, string $key, string $value, bool $caseInsensitive = false): array { - return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive, true); - } - /** * @inheritDoc * @@ -423,10 +408,10 @@ public function searchUsersByValueDeprecated(string $app, string $key, string $v * @param string $key preference key * @param int $value preference value * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValueInt(string $app, string $key, int $value): array { + public function searchUsersByValueInt(string $app, string $key, int $value): Generator { return $this->searchUsersByValueString($app, $key, (string)$value); } @@ -437,10 +422,10 @@ public function searchUsersByValueInt(string $app, string $key, int $value): arr * @param string $key preference key * @param array $values list of preference values * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValues(string $app, string $key, array $values): array { + public function searchUsersByValues(string $app, string $key, array $values): Generator { return $this->searchUsersByTypedValue($app, $key, $values); } @@ -451,10 +436,10 @@ public function searchUsersByValues(string $app, string $key, array $values): ar * @param string $key preference key * @param bool $value preference value * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValueBool(string $app, string $key, bool $value): array { + public function searchUsersByValueBool(string $app, string $key, bool $value): Generator { $values = ['0', 'off', 'false', 'no']; if ($value) { $values = ['1', 'on', 'true', 'yes']; @@ -470,11 +455,10 @@ public function searchUsersByValueBool(string $app, string $key, bool $value): a * @param string $key * @param string|array $value * @param bool $caseInsensitive - * @param bool $withinNotIndexedField DEPRECATED: should only be used to stay compatible with not-indexed/pre-31 preferences value * - * @return list + * @return Generator */ - private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): array { + private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator { $this->assertParams('', $app, $key, allowEmptyUser: true); $qb = $this->connection->getQueryBuilder(); @@ -483,14 +467,14 @@ private function searchUsersByTypedValue(string $app, string $key, string|array $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); - // search within 'indexed' OR 'configvalue' (but if 'flags' is not set as indexed) + // search within 'indexed' OR 'configvalue' only if 'flags' is set as not indexed // TODO: when implementing config lexicon remove the searches on 'configvalue' if value is set as indexed $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; if (is_array($value)) { $where = $qb->expr()->orX( $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)), $qb->expr()->andX( - $qb->createFunction('NOT ' . $qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED)), + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)) ) ); @@ -499,7 +483,7 @@ private function searchUsersByTypedValue(string $app, string $key, string|array $where = $qb->expr()->orX( $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value))), $qb->expr()->andX( - $qb->createFunction('NOT ' . $qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED)), + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value))) ) ); @@ -507,7 +491,7 @@ private function searchUsersByTypedValue(string $app, string $key, string|array $where = $qb->expr()->orX( $qb->expr()->eq('indexed', $qb->createNamedParameter($value)), $qb->expr()->andX( - $qb->createFunction('NOT ' . $qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED)), + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) ) ); @@ -515,15 +499,10 @@ private function searchUsersByTypedValue(string $app, string $key, string|array } $qb->andWhere($where); - - $userIds = []; $result = $qb->executeQuery(); - $rows = $result->fetchAll(); - foreach ($rows as $row) { - $userIds[] = $row['userid']; + while ($row = $result->fetch()) { + yield $row['userid']; } - - return $userIds; } /** @@ -726,7 +705,7 @@ private function getTypedValue( bool $lazy, ValueType $type, ): string { - $this->assertParams($userId, $app, $key, valueType: $type); + $this->assertParams($userId, $app, $key); $this->loadPreferences($userId, $lazy); /** @@ -793,7 +772,7 @@ public function getValueType(string $userId, string $app, string $key, ?bool $la * @param string $key preference key * @param bool $lazy lazy loading * - * @return ValueType type of the value + * @return int flags applied to value * @throws UnknownKeyException if preference key is not known * @throws IncorrectTypeException if preferences value type is not known * @since 31.0.0 @@ -978,7 +957,7 @@ public function setValueBool( string $key, bool $value, bool $lazy = false, - int $flags = 0 + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -1058,7 +1037,7 @@ private function setTypedValue( int $flags, ValueType $type, ): bool { - $this->assertParams($userId, $app, $key, valueType: $type); + $this->assertParams($userId, $app, $key); $this->loadPreferences($userId, $lazy); $inserted = $refreshCache = false; @@ -1074,7 +1053,7 @@ private function setTypedValue( if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) { if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) { $this->logger->warning('sensitive value are not to be indexed'); - } else if (strlen($value) > self::USER_MAX_LENGTH) { + } elseif (strlen($value) > self::USER_MAX_LENGTH) { $this->logger->warning('value is too lengthy to be indexed'); } else { $indexed = $value; @@ -1203,7 +1182,7 @@ private function setTypedValue( * @since 31.0.0 */ public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { - $this->assertParams($userId, $app, $key, valueType: $type); + $this->assertParams($userId, $app, $key); $this->loadPreferencesAll($userId); $this->isLazy($userId, $app, $key); // confirm key exists @@ -1348,11 +1327,11 @@ public function updateIndexed(string $userId, string $app, string $key, bool $in $update = $this->connection->getQueryBuilder(); $update->update('preferences') - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) - ->set('indexed', $update->createNamedParameter($indexed)) - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) + ->set('indexed', $update->createNamedParameter($indexed)) + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); $update->executeStatement(); $this->valueDetails[$userId][$app][$key]['flags'] = $flags; @@ -1634,7 +1613,6 @@ private function assertParams( string $prefKey = '', bool $allowEmptyUser = false, bool $allowEmptyApp = false, - ?ValueType $valueType = null, ): void { if (!$allowEmptyUser && $userId === '') { throw new InvalidArgumentException('userId cannot be an empty string'); @@ -1651,12 +1629,6 @@ private function assertParams( if (strlen($prefKey) > self::KEY_MAX_LENGTH) { throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); } - if ($valueType !== null) { -// $valueFlag = $valueType->value; -// if (ValueType::tryFrom($valueFlag) === null) { -// throw new InvalidArgumentException('Unknown value type'); -// } - } } private function loadPreferencesAll(string $userId): void { @@ -1697,7 +1669,7 @@ private function loadPreferences(string $userId, ?bool $lazy = false): void { } else { $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; } - $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int) $row['flags']]; + $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']]; } $result->closeCursor(); $this->setAsLoaded($userId, $lazy); diff --git a/lib/private/Server.php b/lib/private/Server.php index 10d6dee9d07f2..e109636f1fad4 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -138,6 +138,7 @@ use OCP\Collaboration\Reference\IReferenceManager; use OCP\Command\IBus; use OCP\Comments\ICommentsManager; +use OCP\Config\IUserPreferences; use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Contacts\ContactsMenu\IContactsStore; use OCP\Defaults; @@ -237,7 +238,6 @@ use OCP\User\Events\UserLoggedInWithCookieEvent; use OCP\User\Events\UserLoggedOutEvent; use OCP\User\IAvailabilityCoordinator; -use OCP\UserPreferences\IUserPreferences; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Log\LoggerInterface; @@ -568,7 +568,7 @@ public function __construct($webRoot, \OC\Config $config) { }); $this->registerAlias(IAppConfig::class, \OC\AppConfig::class); - $this->registerAlias(IUserPreferences::class, \OC\UserPreferences::class); + $this->registerAlias(IUserPreferences::class, \OC\Config\UserPreferences::class); $this->registerService(IFactory::class, function (Server $c) { return new \OC\L10N\Factory( diff --git a/lib/public/UserPreferences/Exceptions/UserPreferencesException.php b/lib/public/Config/Exceptions/IncorrectTypeException.php similarity index 68% rename from lib/public/UserPreferences/Exceptions/UserPreferencesException.php rename to lib/public/Config/Exceptions/IncorrectTypeException.php index 664181d420b7e..dafbbacff78d2 100644 --- a/lib/public/UserPreferences/Exceptions/UserPreferencesException.php +++ b/lib/public/Config/Exceptions/IncorrectTypeException.php @@ -6,12 +6,12 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\UserPreferences\Exceptions; +namespace OCP\Config\Exceptions; use Exception; /** * @since 31.0.0 */ -class UserPreferencesException extends Exception { +class IncorrectTypeException extends Exception { } diff --git a/lib/public/UserPreferences/Exceptions/TypeConflictException.php b/lib/public/Config/Exceptions/TypeConflictException.php similarity index 64% rename from lib/public/UserPreferences/Exceptions/TypeConflictException.php rename to lib/public/Config/Exceptions/TypeConflictException.php index b67113fbba757..0b3c903cb385f 100644 --- a/lib/public/UserPreferences/Exceptions/TypeConflictException.php +++ b/lib/public/Config/Exceptions/TypeConflictException.php @@ -6,10 +6,12 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\UserPreferences\Exceptions; +namespace OCP\Config\Exceptions; + +use Exception; /** * @since 31.0.0 */ -class TypeConflictException extends UserPreferencesException { +class TypeConflictException extends Exception { } diff --git a/lib/public/UserPreferences/Exceptions/UnknownKeyException.php b/lib/public/Config/Exceptions/UnknownKeyException.php similarity index 64% rename from lib/public/UserPreferences/Exceptions/UnknownKeyException.php rename to lib/public/Config/Exceptions/UnknownKeyException.php index 3df666899641c..2150246f1d2ad 100644 --- a/lib/public/UserPreferences/Exceptions/UnknownKeyException.php +++ b/lib/public/Config/Exceptions/UnknownKeyException.php @@ -6,10 +6,12 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\UserPreferences\Exceptions; +namespace OCP\Config\Exceptions; + +use Exception; /** * @since 31.0.0 */ -class UnknownKeyException extends UserPreferencesException { +class UnknownKeyException extends Exception { } diff --git a/lib/public/UserPreferences/IUserPreferences.php b/lib/public/Config/IUserPreferences.php similarity index 95% rename from lib/public/UserPreferences/IUserPreferences.php rename to lib/public/Config/IUserPreferences.php index f2635c0ffafbf..18c7fdcd66526 100644 --- a/lib/public/UserPreferences/IUserPreferences.php +++ b/lib/public/Config/IUserPreferences.php @@ -6,16 +6,34 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\UserPreferences; +namespace OCP\Config; -use OCP\UserPreferences\Exceptions\IncorrectTypeException; -use OCP\UserPreferences\Exceptions\UnknownKeyException; +use Generator; +use OCP\Config\Exceptions\IncorrectTypeException; +use OCP\Config\Exceptions\UnknownKeyException; /** + * This class provides an easy way for apps to store user preferences in the + * database. + * Supports **lazy loading** + * + * ### What is lazy loading ? + * In order to avoid loading useless user preferences into memory for each request, + * only non-lazy values are now loaded. + * + * Once a value that is lazy is requested, all lazy values will be loaded. + * + * Similarly, some methods from this class are marked with a warning about ignoring + * lazy loading. Use them wisely and only on parts of the code that are called + * during specific requests or actions to avoid loading the lazy values all the time. + * * @since 31.0.0 */ + interface IUserPreferences { + /** @since 31.0.0 */ public const FLAG_SENSITIVE = 1; // value is sensitive + /** @since 31.0.0 */ public const FLAG_INDEXED = 2; // value should be indexed /** @@ -191,10 +209,10 @@ public function getValuesByUsers(string $app, string $key, ?ValueType $typedAs = * @param string $value preference value * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): array; + public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator; /** * List all users storing a specific preference key/value pair. @@ -206,10 +224,10 @@ public function searchUsersByValueString(string $app, string $key, string $value * @param string $key preference key * @param int $value preference value * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValueInt(string $app, string $key, int $value): array; + public function searchUsersByValueInt(string $app, string $key, int $value): Generator; /** * List all users storing a specific preference key/value pair. @@ -221,10 +239,10 @@ public function searchUsersByValueInt(string $app, string $key, int $value): arr * @param string $key preference key * @param array $values list of possible preference values * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValues(string $app, string $key, array $values): array; + public function searchUsersByValues(string $app, string $key, array $values): Generator; /** * List all users storing a specific preference key/value pair. @@ -236,10 +254,10 @@ public function searchUsersByValues(string $app, string $key, array $values): ar * @param string $key preference key * @param bool $value preference value * - * @return list + * @return Generator * @since 31.0.0 */ - public function searchUsersByValueBool(string $app, string $key, bool $value): array; + public function searchUsersByValueBool(string $app, string $key, bool $value): Generator; /** * Get user preference assigned to a preference key. diff --git a/lib/public/UserPreferences/ValueType.php b/lib/public/Config/ValueType.php similarity index 87% rename from lib/public/UserPreferences/ValueType.php rename to lib/public/Config/ValueType.php index 280dc5a8b4248..caf510046c263 100644 --- a/lib/public/UserPreferences/ValueType.php +++ b/lib/public/Config/ValueType.php @@ -6,15 +6,14 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\UserPreferences; +namespace OCP\Config; -use OCP\UserPreferences\Exceptions\IncorrectTypeException; +use OCP\Config\Exceptions\IncorrectTypeException; use UnhandledMatchError; /** - * Listing of available value type for user preferences + * Listing of available value type for typed config value * - * @see IUserPreferences * @since 31.0.0 */ enum ValueType: int { @@ -50,7 +49,7 @@ public static function fromStringDefinition(string $definition): self { 'bool' => self::BOOL, 'array' => self::ARRAY }; - } catch (\UnhandledMatchError ) { + } catch (\UnhandledMatchError) { throw new IncorrectTypeException('unknown string definition'); } } diff --git a/lib/public/IConfig.php b/lib/public/IConfig.php index 3bc64c5e13337..f434f838d45c3 100644 --- a/lib/public/IConfig.php +++ b/lib/public/IConfig.php @@ -245,7 +245,7 @@ public function deleteAppFromAllUsers($appName); * @param string $appName the app to get the user for * @param string $key the key to get the user for * @param string $value the value to get the user for - * @return list of user IDs + * @return array of user IDs * @since 31.0.0 return type of `list` * @since 8.0.0 */ diff --git a/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php b/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php deleted file mode 100644 index 5c8f83dee5efe..0000000000000 --- a/lib/public/UserPreferences/Exceptions/IncorrectTypeException.php +++ /dev/null @@ -1,15 +0,0 @@ -connection, $this->logger, $this->crypto, @@ -775,7 +775,7 @@ public function testSearchUsersByValueString( array $result, ): void { $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueString($app, $key, $value, $ci)); + $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValueString($app, $key, $value, $ci))); } public function providerSearchValuesByValueInt(): array { @@ -796,7 +796,7 @@ public function testSearchUsersByValueInt( array $result, ): void { $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueInt($app, $key, $value)); + $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValueInt($app, $key, $value))); } public function providerSearchValuesByValues(): array { @@ -816,7 +816,7 @@ public function testSearchUsersByValues( array $result, ): void { $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValues($app, $key, $values)); + $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValues($app, $key, $values))); } public function providerSearchValuesByValueBool(): array { @@ -836,7 +836,7 @@ public function testSearchUsersByValueBool( array $result, ): void { $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, $preferences->searchUsersByValueBool($app, $key, $value)); + $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValueBool($app, $key, $value))); } public function providerGetValueMixed(): array { From 6afc8552b7c0dcd96c88533f47bc8b55cba327c9 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 12 Nov 2024 21:29:18 -0100 Subject: [PATCH 4/5] feat(user-prefs): switching to NCU/ Signed-off-by: Maxence Lange --- lib/composer/composer/autoload_classmap.php | 10 +++++----- lib/composer/composer/autoload_static.php | 10 +++++----- lib/private/AllConfig.php | 6 +++--- lib/private/Config/UserPreferences.php | 10 +++++----- lib/private/Server.php | 2 +- .../Config/Exceptions/IncorrectTypeException.php | 2 +- .../Config/Exceptions/TypeConflictException.php | 2 +- .../Config/Exceptions/UnknownKeyException.php | 2 +- lib/{public => unstable}/Config/IUserPreferences.php | 6 +++--- lib/{public => unstable}/Config/ValueType.php | 4 ++-- tests/lib/UserPreferencesTest.php | 8 ++++---- 11 files changed, 31 insertions(+), 31 deletions(-) rename lib/{public => unstable}/Config/Exceptions/IncorrectTypeException.php (88%) rename lib/{public => unstable}/Config/Exceptions/TypeConflictException.php (88%) rename lib/{public => unstable}/Config/Exceptions/UnknownKeyException.php (88%) rename lib/{public => unstable}/Config/IUserPreferences.php (99%) rename lib/{public => unstable}/Config/ValueType.php (95%) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index c3abaefd56c55..7ba1e0e4667b1 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -7,6 +7,11 @@ return array( 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', + 'NCU\\Config\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/unstable/Config/Exceptions/IncorrectTypeException.php', + 'NCU\\Config\\Exceptions\\TypeConflictException' => $baseDir . '/lib/unstable/Config/Exceptions/TypeConflictException.php', + 'NCU\\Config\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/unstable/Config/Exceptions/UnknownKeyException.php', + 'NCU\\Config\\IUserPreferences' => $baseDir . '/lib/unstable/Config/IUserPreferences.php', + 'NCU\\Config\\ValueType' => $baseDir . '/lib/unstable/Config/ValueType.php', 'OCP\\Accounts\\IAccount' => $baseDir . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccountManager' => $baseDir . '/lib/public/Accounts/IAccountManager.php', 'OCP\\Accounts\\IAccountProperty' => $baseDir . '/lib/public/Accounts/IAccountProperty.php', @@ -223,11 +228,6 @@ 'OCP\\Common\\Exception\\NotFoundException' => $baseDir . '/lib/public/Common/Exception/NotFoundException.php', 'OCP\\Config\\BeforePreferenceDeletedEvent' => $baseDir . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => $baseDir . '/lib/public/Config/BeforePreferenceSetEvent.php', - 'OCP\\Config\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/public/Config/Exceptions/IncorrectTypeException.php', - 'OCP\\Config\\Exceptions\\TypeConflictException' => $baseDir . '/lib/public/Config/Exceptions/TypeConflictException.php', - 'OCP\\Config\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/public/Config/Exceptions/UnknownKeyException.php', - 'OCP\\Config\\IUserPreferences' => $baseDir . '/lib/public/Config/IUserPreferences.php', - 'OCP\\Config\\ValueType' => $baseDir . '/lib/public/Config/ValueType.php', 'OCP\\Console\\ConsoleEvent' => $baseDir . '/lib/public/Console/ConsoleEvent.php', 'OCP\\Console\\ReservedOptions' => $baseDir . '/lib/public/Console/ReservedOptions.php', 'OCP\\Constants' => $baseDir . '/lib/public/Constants.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index cf821ddeba058..cf1d395fd4378 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -48,6 +48,11 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 public static $classMap = array ( 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', + 'NCU\\Config\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/IncorrectTypeException.php', + 'NCU\\Config\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/TypeConflictException.php', + 'NCU\\Config\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/UnknownKeyException.php', + 'NCU\\Config\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/unstable/Config/IUserPreferences.php', + 'NCU\\Config\\ValueType' => __DIR__ . '/../../..' . '/lib/unstable/Config/ValueType.php', 'OCP\\Accounts\\IAccount' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccountManager' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountManager.php', 'OCP\\Accounts\\IAccountProperty' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountProperty.php', @@ -264,11 +269,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Common\\Exception\\NotFoundException' => __DIR__ . '/../../..' . '/lib/public/Common/Exception/NotFoundException.php', 'OCP\\Config\\BeforePreferenceDeletedEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceDeletedEvent.php', 'OCP\\Config\\BeforePreferenceSetEvent' => __DIR__ . '/../../..' . '/lib/public/Config/BeforePreferenceSetEvent.php', - 'OCP\\Config\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/IncorrectTypeException.php', - 'OCP\\Config\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/TypeConflictException.php', - 'OCP\\Config\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/public/Config/Exceptions/UnknownKeyException.php', - 'OCP\\Config\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/public/Config/IUserPreferences.php', - 'OCP\\Config\\ValueType' => __DIR__ . '/../../..' . '/lib/public/Config/ValueType.php', 'OCP\\Console\\ConsoleEvent' => __DIR__ . '/../../..' . '/lib/public/Console/ConsoleEvent.php', 'OCP\\Console\\ReservedOptions' => __DIR__ . '/../../..' . '/lib/public/Console/ReservedOptions.php', 'OCP\\Constants' => __DIR__ . '/../../..' . '/lib/public/Constants.php', diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index 8f7e5359c967f..dbea65fdc2172 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -6,11 +6,11 @@ */ namespace OC; +use NCU\Config\Exceptions\TypeConflictException; +use NCU\Config\IUserPreferences; +use NCU\Config\ValueType; use OC\Config\UserPreferences; use OCP\Cache\CappedMemoryCache; -use OCP\Config\Exceptions\TypeConflictException; -use OCP\Config\IUserPreferences; -use OCP\Config\ValueType; use OCP\IConfig; use OCP\IDBConnection; use OCP\PreConditionNotMetException; diff --git a/lib/private/Config/UserPreferences.php b/lib/private/Config/UserPreferences.php index 6dd9c63383671..d9fd5f6a06499 100644 --- a/lib/private/Config/UserPreferences.php +++ b/lib/private/Config/UserPreferences.php @@ -11,11 +11,11 @@ use Generator; use InvalidArgumentException; use JsonException; -use OCP\Config\Exceptions\IncorrectTypeException; -use OCP\Config\Exceptions\TypeConflictException; -use OCP\Config\Exceptions\UnknownKeyException; -use OCP\Config\IUserPreferences; -use OCP\Config\ValueType; +use NCU\Config\Exceptions\IncorrectTypeException; +use NCU\Config\Exceptions\TypeConflictException; +use NCU\Config\Exceptions\UnknownKeyException; +use NCU\Config\IUserPreferences; +use NCU\Config\ValueType; use OCP\DB\Exception as DBException; use OCP\DB\IResult; use OCP\DB\QueryBuilder\IQueryBuilder; diff --git a/lib/private/Server.php b/lib/private/Server.php index e109636f1fad4..dc4b3c4f7dc7a 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -7,6 +7,7 @@ namespace OC; use bantu\IniGetWrapper\IniGetWrapper; +use NCU\Config\IUserPreferences; use OC\Accounts\AccountManager; use OC\App\AppManager; use OC\App\AppStore\Bundles\BundleFetcher; @@ -138,7 +139,6 @@ use OCP\Collaboration\Reference\IReferenceManager; use OCP\Command\IBus; use OCP\Comments\ICommentsManager; -use OCP\Config\IUserPreferences; use OCP\Contacts\ContactsMenu\IActionFactory; use OCP\Contacts\ContactsMenu\IContactsStore; use OCP\Defaults; diff --git a/lib/public/Config/Exceptions/IncorrectTypeException.php b/lib/unstable/Config/Exceptions/IncorrectTypeException.php similarity index 88% rename from lib/public/Config/Exceptions/IncorrectTypeException.php rename to lib/unstable/Config/Exceptions/IncorrectTypeException.php index dafbbacff78d2..b3eedde5300f9 100644 --- a/lib/public/Config/Exceptions/IncorrectTypeException.php +++ b/lib/unstable/Config/Exceptions/IncorrectTypeException.php @@ -6,7 +6,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\Config\Exceptions; +namespace NCU\Config\Exceptions; use Exception; diff --git a/lib/public/Config/Exceptions/TypeConflictException.php b/lib/unstable/Config/Exceptions/TypeConflictException.php similarity index 88% rename from lib/public/Config/Exceptions/TypeConflictException.php rename to lib/unstable/Config/Exceptions/TypeConflictException.php index 0b3c903cb385f..042991f8a57e5 100644 --- a/lib/public/Config/Exceptions/TypeConflictException.php +++ b/lib/unstable/Config/Exceptions/TypeConflictException.php @@ -6,7 +6,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\Config\Exceptions; +namespace NCU\Config\Exceptions; use Exception; diff --git a/lib/public/Config/Exceptions/UnknownKeyException.php b/lib/unstable/Config/Exceptions/UnknownKeyException.php similarity index 88% rename from lib/public/Config/Exceptions/UnknownKeyException.php rename to lib/unstable/Config/Exceptions/UnknownKeyException.php index 2150246f1d2ad..80626c6a6355a 100644 --- a/lib/public/Config/Exceptions/UnknownKeyException.php +++ b/lib/unstable/Config/Exceptions/UnknownKeyException.php @@ -6,7 +6,7 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\Config\Exceptions; +namespace NCU\Config\Exceptions; use Exception; diff --git a/lib/public/Config/IUserPreferences.php b/lib/unstable/Config/IUserPreferences.php similarity index 99% rename from lib/public/Config/IUserPreferences.php rename to lib/unstable/Config/IUserPreferences.php index 18c7fdcd66526..a2695a0d05392 100644 --- a/lib/public/Config/IUserPreferences.php +++ b/lib/unstable/Config/IUserPreferences.php @@ -6,11 +6,11 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\Config; +namespace NCU\Config; use Generator; -use OCP\Config\Exceptions\IncorrectTypeException; -use OCP\Config\Exceptions\UnknownKeyException; +use NCU\Config\Exceptions\IncorrectTypeException; +use NCU\Config\Exceptions\UnknownKeyException; /** * This class provides an easy way for apps to store user preferences in the diff --git a/lib/public/Config/ValueType.php b/lib/unstable/Config/ValueType.php similarity index 95% rename from lib/public/Config/ValueType.php rename to lib/unstable/Config/ValueType.php index caf510046c263..2c3c5851e3f5d 100644 --- a/lib/public/Config/ValueType.php +++ b/lib/unstable/Config/ValueType.php @@ -6,9 +6,9 @@ * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCP\Config; +namespace NCU\Config; -use OCP\Config\Exceptions\IncorrectTypeException; +use NCU\Config\Exceptions\IncorrectTypeException; use UnhandledMatchError; /** diff --git a/tests/lib/UserPreferencesTest.php b/tests/lib/UserPreferencesTest.php index 19e6d6161bfc8..c149af002d355 100644 --- a/tests/lib/UserPreferencesTest.php +++ b/tests/lib/UserPreferencesTest.php @@ -7,11 +7,11 @@ */ namespace lib; +use NCU\Config\Exceptions\TypeConflictException; +use NCU\Config\Exceptions\UnknownKeyException; +use NCU\Config\IUserPreferences; +use NCU\Config\ValueType; use OC\Config\UserPreferences; -use OCP\Config\Exceptions\TypeConflictException; -use OCP\Config\Exceptions\UnknownKeyException; -use OCP\Config\IUserPreferences; -use OCP\Config\ValueType; use OCP\IDBConnection; use OCP\Security\ICrypto; use Psr\Log\LoggerInterface; From 5b4f1904c0867b14237c5fcfb3e27e936f35ca0b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Mon, 18 Nov 2024 20:09:45 -0100 Subject: [PATCH 5/5] feat(user-prefs): renaming to IUserConfig Signed-off-by: Maxence Lange --- lib/composer/composer/autoload_classmap.php | 4 +- lib/composer/composer/autoload_static.php | 4 +- lib/private/AllConfig.php | 84 +++-- .../{UserPreferences.php => UserConfig.php} | 338 +++++++++--------- lib/private/Server.php | 4 +- lib/public/IConfig.php | 2 +- .../Exceptions/IncorrectTypeException.php | 1 + .../Exceptions/TypeConflictException.php | 1 + .../Config/Exceptions/UnknownKeyException.php | 1 + .../{IUserPreferences.php => IUserConfig.php} | 332 ++++++++--------- lib/unstable/Config/ValueType.php | 1 + .../UserConfigTest.php} | 330 ++++++++--------- 12 files changed, 559 insertions(+), 543 deletions(-) rename lib/private/Config/{UserPreferences.php => UserConfig.php} (83%) rename lib/unstable/Config/{IUserPreferences.php => IUserConfig.php} (57%) rename tests/lib/{UserPreferencesTest.php => Config/UserConfigTest.php} (82%) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 7ba1e0e4667b1..cc6b8be326c49 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -10,7 +10,7 @@ 'NCU\\Config\\Exceptions\\IncorrectTypeException' => $baseDir . '/lib/unstable/Config/Exceptions/IncorrectTypeException.php', 'NCU\\Config\\Exceptions\\TypeConflictException' => $baseDir . '/lib/unstable/Config/Exceptions/TypeConflictException.php', 'NCU\\Config\\Exceptions\\UnknownKeyException' => $baseDir . '/lib/unstable/Config/Exceptions/UnknownKeyException.php', - 'NCU\\Config\\IUserPreferences' => $baseDir . '/lib/unstable/Config/IUserPreferences.php', + 'NCU\\Config\\IUserConfig' => $baseDir . '/lib/unstable/Config/IUserConfig.php', 'NCU\\Config\\ValueType' => $baseDir . '/lib/unstable/Config/ValueType.php', 'OCP\\Accounts\\IAccount' => $baseDir . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccountManager' => $baseDir . '/lib/public/Accounts/IAccountManager.php', @@ -1123,7 +1123,7 @@ 'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php', 'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php', 'OC\\Config' => $baseDir . '/lib/private/Config.php', - 'OC\\Config\\UserPreferences' => $baseDir . '/lib/private/Config/UserPreferences.php', + 'OC\\Config\\UserConfig' => $baseDir . '/lib/private/Config/UserConfig.php', 'OC\\Console\\Application' => $baseDir . '/lib/private/Console/Application.php', 'OC\\Console\\TimestampFormatter' => $baseDir . '/lib/private/Console/TimestampFormatter.php', 'OC\\ContactsManager' => $baseDir . '/lib/private/ContactsManager.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index cf1d395fd4378..54959bc6b9164 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -51,7 +51,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'NCU\\Config\\Exceptions\\IncorrectTypeException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/IncorrectTypeException.php', 'NCU\\Config\\Exceptions\\TypeConflictException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/TypeConflictException.php', 'NCU\\Config\\Exceptions\\UnknownKeyException' => __DIR__ . '/../../..' . '/lib/unstable/Config/Exceptions/UnknownKeyException.php', - 'NCU\\Config\\IUserPreferences' => __DIR__ . '/../../..' . '/lib/unstable/Config/IUserPreferences.php', + 'NCU\\Config\\IUserConfig' => __DIR__ . '/../../..' . '/lib/unstable/Config/IUserConfig.php', 'NCU\\Config\\ValueType' => __DIR__ . '/../../..' . '/lib/unstable/Config/ValueType.php', 'OCP\\Accounts\\IAccount' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccountManager' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountManager.php', @@ -1164,7 +1164,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php', 'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php', 'OC\\Config' => __DIR__ . '/../../..' . '/lib/private/Config.php', - 'OC\\Config\\UserPreferences' => __DIR__ . '/../../..' . '/lib/private/Config/UserPreferences.php', + 'OC\\Config\\UserConfig' => __DIR__ . '/../../..' . '/lib/private/Config/UserConfig.php', 'OC\\Console\\Application' => __DIR__ . '/../../..' . '/lib/private/Console/Application.php', 'OC\\Console\\TimestampFormatter' => __DIR__ . '/../../..' . '/lib/private/Console/TimestampFormatter.php', 'OC\\ContactsManager' => __DIR__ . '/../../..' . '/lib/private/ContactsManager.php', diff --git a/lib/private/AllConfig.php b/lib/private/AllConfig.php index dbea65fdc2172..bb15adf31b4f0 100644 --- a/lib/private/AllConfig.php +++ b/lib/private/AllConfig.php @@ -7,9 +7,9 @@ namespace OC; use NCU\Config\Exceptions\TypeConflictException; -use NCU\Config\IUserPreferences; +use NCU\Config\IUserConfig; use NCU\Config\ValueType; -use OC\Config\UserPreferences; +use OC\Config\UserConfig; use OCP\Cache\CappedMemoryCache; use OCP\IConfig; use OCP\IDBConnection; @@ -230,20 +230,20 @@ public function deleteAppValues($appName) { * * @throws \OCP\PreConditionNotMetException if a precondition is specified and is not met * @throws \UnexpectedValueException when trying to store an unexpected value - * @deprecated 31.0.0 - use {@see IUserPreferences} directly - * @see IUserPreferences::getValueString - * @see IUserPreferences::getValueInt - * @see IUserPreferences::getValueFloat - * @see IUserPreferences::getValueArray - * @see IUserPreferences::getValueBool + * @deprecated 31.0.0 - use {@see IUserConfig} directly + * @see IUserConfig::getValueString + * @see IUserConfig::getValueInt + * @see IUserConfig::getValueFloat + * @see IUserConfig::getValueArray + * @see IUserConfig::getValueBool */ public function setUserValue($userId, $appName, $key, $value, $preCondition = null) { if (!is_int($value) && !is_float($value) && !is_string($value)) { throw new \UnexpectedValueException('Only integers, floats and strings are allowed as value'); } - /** @var UserPreferences $userPreferences */ - $userPreferences = \OCP\Server::get(IUserPreferences::class); + /** @var UserConfig $userPreferences */ + $userPreferences = \OCP\Server::get(IUserConfig::class); if ($preCondition !== null) { try { if ($userPreferences->getValueMixed($userId, $appName, $key) !== (string)$preCondition) { @@ -265,19 +265,19 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu * @param mixed $default the default value to be returned if the value isn't set * * @return string - * @deprecated 31.0.0 - use {@see IUserPreferences} directly - * @see IUserPreferences::getValueString - * @see IUserPreferences::getValueInt - * @see IUserPreferences::getValueFloat - * @see IUserPreferences::getValueArray - * @see IUserPreferences::getValueBool + * @deprecated 31.0.0 - use {@see IUserConfig} directly + * @see IUserConfig::getValueString + * @see IUserConfig::getValueInt + * @see IUserConfig::getValueFloat + * @see IUserConfig::getValueArray + * @see IUserConfig::getValueBool */ public function getUserValue($userId, $appName, $key, $default = '') { if ($userId === null || $userId === '') { return $default; } - /** @var UserPreferences $userPreferences */ - $userPreferences = \OCP\Server::get(IUserPreferences::class); + /** @var UserConfig $userPreferences */ + $userPreferences = \OCP\Server::get(IUserConfig::class); // because $default can be null ... if (!$userPreferences->hasKey($userId, $appName, $key)) { return $default; @@ -290,11 +290,12 @@ public function getUserValue($userId, $appName, $key, $default = '') { * * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under + * * @return string[] - * @deprecated 31.0.0 - use {@see IUserPreferences::getKeys} directly + * @deprecated 31.0.0 - use {@see IUserConfig::getKeys} directly */ public function getUserKeys($userId, $appName) { - return \OCP\Server::get(IUserPreferences::class)->getKeys($userId, $appName); + return \OCP\Server::get(IUserConfig::class)->getKeys($userId, $appName); } /** @@ -303,52 +304,56 @@ public function getUserKeys($userId, $appName) { * @param string $userId the userId of the user that we want to store the value under * @param string $appName the appName that we stored the value under * @param string $key the key under which the value is being stored - * @deprecated 31.0.0 - use {@see IUserPreferences::deletePreference} directly + * + * @deprecated 31.0.0 - use {@see IUserConfig::deleteUserConfig} directly */ public function deleteUserValue($userId, $appName, $key) { - \OCP\Server::get(IUserPreferences::class)->deletePreference($userId, $appName, $key); + \OCP\Server::get(IUserConfig::class)->deleteUserConfig($userId, $appName, $key); } /** * Delete all user values * * @param string $userId the userId of the user that we want to remove all values from - * @deprecated 31.0.0 - use {@see IUserPreferences::deleteAllPreferences} directly + * + * @deprecated 31.0.0 - use {@see IUserConfig::deleteAllUserConfig} directly */ public function deleteAllUserValues($userId) { if ($userId === null) { return; } - \OCP\Server::get(IUserPreferences::class)->deleteAllPreferences($userId); + \OCP\Server::get(IUserConfig::class)->deleteAllUserConfig($userId); } /** * Delete all user related values of one app * * @param string $appName the appName of the app that we want to remove all values from - * @deprecated 31.0.0 - use {@see IUserPreferences::deleteApp} directly + * + * @deprecated 31.0.0 - use {@see IUserConfig::deleteApp} directly */ public function deleteAppFromAllUsers($appName) { - \OCP\Server::get(IUserPreferences::class)->deleteApp($appName); + \OCP\Server::get(IUserConfig::class)->deleteApp($appName); } /** * Returns all user configs sorted by app of one user * * @param ?string $userId the user ID to get the app configs from + * * @psalm-return array> * @return array[] - 2 dimensional array with the following structure: * [ $appId => * [ $key => $value ] * ] - * @deprecated 31.0.0 - use {@see IUserPreferences::getAllValues} directly + * @deprecated 31.0.0 - use {@see IUserConfig::getAllValues} directly */ public function getAllUserValues(?string $userId): array { if ($userId === null || $userId === '') { return []; } - $values = \OCP\Server::get(IUserPreferences::class)->getAllValues($userId); + $values = \OCP\Server::get(IUserConfig::class)->getAllValues($userId); $result = []; foreach ($values as $app => $list) { foreach ($list as $key => $value) { @@ -364,11 +369,12 @@ public function getAllUserValues(?string $userId): array { * @param string $appName app to get the value for * @param string $key the key to get the value for * @param array $userIds the user IDs to fetch the values for + * * @return array Mapped values: userId => value - * @deprecated 31.0.0 - use {@see IUserPreferences::getValuesByUsers} directly + * @deprecated 31.0.0 - use {@see IUserConfig::getValuesByUsers} directly */ public function getUserValueForUsers($appName, $key, $userIds) { - return \OCP\Server::get(IUserPreferences::class)->getValuesByUsers($appName, $key, ValueType::MIXED, $userIds); + return \OCP\Server::get(IUserConfig::class)->getValuesByUsers($appName, $key, ValueType::MIXED, $userIds); } /** @@ -377,11 +383,14 @@ public function getUserValueForUsers($appName, $key, $userIds) { * @param string $appName the app to get the user for * @param string $key the key to get the user for * @param string $value the value to get the user for - * @return array of user IDs - * @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly + * + * @return list of user IDs + * @deprecated 31.0.0 - use {@see IUserConfig::searchUsersByValueString} directly */ public function getUsersForUserValue($appName, $key, $value) { - return iterator_to_array(\OCP\Server::get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value)); + /** @var list $result */ + $result = iterator_to_array(\OCP\Server::get(IUserConfig::class)->searchUsersByValueString($appName, $key, $value)); + return $result; } /** @@ -390,15 +399,18 @@ public function getUsersForUserValue($appName, $key, $value) { * @param string $appName the app to get the user for * @param string $key the key to get the user for * @param string $value the value to get the user for - * @return array of user IDs - * @deprecated 31.0.0 - use {@see IUserPreferences::searchUsersByValueString} directly + * + * @return list of user IDs + * @deprecated 31.0.0 - use {@see IUserConfig::searchUsersByValueString} directly */ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) { if ($appName === 'settings' && $key === 'email') { return $this->getUsersForUserValue($appName, $key, strtolower($value)); } - return iterator_to_array(\OCP\Server::get(IUserPreferences::class)->searchUsersByValueString($appName, $key, $value, true)); + /** @var list $result */ + $result = iterator_to_array(\OCP\Server::get(IUserConfig::class)->searchUsersByValueString($appName, $key, $value, true)); + return $result; } public function getSystemConfig() { diff --git a/lib/private/Config/UserPreferences.php b/lib/private/Config/UserConfig.php similarity index 83% rename from lib/private/Config/UserPreferences.php rename to lib/private/Config/UserConfig.php index d9fd5f6a06499..37e109b2121a5 100644 --- a/lib/private/Config/UserPreferences.php +++ b/lib/private/Config/UserConfig.php @@ -14,7 +14,7 @@ use NCU\Config\Exceptions\IncorrectTypeException; use NCU\Config\Exceptions\TypeConflictException; use NCU\Config\Exceptions\UnknownKeyException; -use NCU\Config\IUserPreferences; +use NCU\Config\IUserConfig; use NCU\Config\ValueType; use OCP\DB\Exception as DBException; use OCP\DB\IResult; @@ -25,12 +25,12 @@ use Psr\Log\LoggerInterface; /** - * This class provides an easy way for apps to store user preferences in the + * This class provides an easy way for apps to store user config in the * database. * Supports **lazy loading** * * ### What is lazy loading ? - * In order to avoid loading useless user preferences into memory for each request, + * In order to avoid loading useless user config into memory for each request, * only non-lazy values are now loaded. * * Once a value that is lazy is requested, all lazy values will be loaded. @@ -41,24 +41,24 @@ * * @since 31.0.0 */ -class UserPreferences implements IUserPreferences { +class UserConfig implements IUserConfig { private const USER_MAX_LENGTH = 64; private const APP_MAX_LENGTH = 32; private const KEY_MAX_LENGTH = 64; private const INDEX_MAX_LENGTH = 64; - private const ENCRYPTION_PREFIX = '$UserPreferencesEncryption$'; - private const ENCRYPTION_PREFIX_LENGTH = 27; // strlen(self::ENCRYPTION_PREFIX) + private const ENCRYPTION_PREFIX = '$UserConfigEncryption$'; + private const ENCRYPTION_PREFIX_LENGTH = 22; // strlen(self::ENCRYPTION_PREFIX) /** @var array>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */ - private array $fastCache = []; // cache for normal preference keys + private array $fastCache = []; // cache for normal config keys /** @var array>> ['user_id' => ['app_id' => ['key' => 'value']]] */ - private array $lazyCache = []; // cache for lazy preference keys + private array $lazyCache = []; // cache for lazy config keys /** @var array>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */ - private array $valueDetails = []; // type for all preference values + private array $valueDetails = []; // type for all config values /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ - private array $valueTypes = []; // type for all preference values + private array $valueTypes = []; // type for all config values /** @var array>> ['user_id' => ['app_id' => ['key' => bitflag]]] */ - private array $valueFlags = []; // type for all preference values + private array $valueFlags = []; // type for all config values /** @var array ['user_id' => bool] */ private array $fastLoaded = []; /** @var array ['user_id' => bool] */ @@ -108,7 +108,7 @@ public function getUserIds(string $appId = ''): array { */ public function getApps(string $userId): array { $this->assertParams($userId, allowEmptyApp: true); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? [])); sort($apps); @@ -121,13 +121,13 @@ public function getApps(string $userId): array { * @param string $userId id of the user * @param string $app id of the app * - * @return list list of stored preference keys + * @return list list of stored config keys * @since 31.0.0 */ public function getKeys(string $userId, string $app): array { $this->assertParams($userId, $app); - $this->loadPreferencesAll($userId); - // array_merge() will remove numeric keys (here preference keys), so addition arrays instead + $this->loadConfigAll($userId); + // array_merge() will remove numeric keys (here config keys), so addition arrays instead $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []))); sort($keys); @@ -139,15 +139,15 @@ public function getKeys(string $userId, string $app): array { * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences + * @param string $key config key + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config * * @return bool TRUE if key exists * @since 31.0.0 */ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); if ($lazy === null) { $appCache = $this->getValues($userId, $app); @@ -166,19 +166,19 @@ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = f * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences + * @param string $key config key + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config * * @return bool - * @throws UnknownKeyException if preference key is not known + * @throws UnknownKeyException if config key is not known * @since 31.0.0 */ public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); if (!isset($this->valueDetails[$userId][$app][$key])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']); @@ -189,19 +189,19 @@ public function isSensitive(string $userId, string $app, string $key, ?bool $laz * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool|null $lazy TRUE to search within lazy loaded preferences, NULL to search within all preferences + * @param string $key config key + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config * * @return bool - * @throws UnknownKeyException if preference key is not known + * @throws UnknownKeyException if config key is not known * @since 31.0.0 */ public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); if (!isset($this->valueDetails[$userId][$app][$key])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']); @@ -212,26 +212,26 @@ public function isIndexed(string $userId, string $app, string $key, ?bool $lazy * * @param string $userId id of the user * @param string $app if of the app - * @param string $key preference key + * @param string $key config key * - * @return bool TRUE if preference is lazy loaded - * @throws UnknownKeyException if preference key is not known - * @see IUserPreferences for details about lazy loading + * @return bool TRUE if config is lazy loaded + * @throws UnknownKeyException if config key is not known + * @see IUserConfig for details about lazy loading * @since 31.0.0 */ public function isLazy(string $userId, string $app, string $key): bool { - // there is a huge probability the non-lazy preferences are already loaded + // there is a huge probability the non-lazy config are already loaded // meaning that we can start by only checking if a current non-lazy key exists if ($this->hasKey($userId, $app, $key, false)) { return false; // meaning key is not lazy. } - // as key is not found as non-lazy, we load and search in the lazy preferences + // as key is not found as non-lazy, we load and search in the lazy config if ($this->hasKey($userId, $app, $key, true)) { return true; } - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } /** @@ -239,8 +239,8 @@ public function isLazy(string $userId, string $app, string $key): bool { * * @param string $userId id of the user * @param string $app id of the app - * @param string $prefix preference keys prefix to search - * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} + * @param string $prefix config keys prefix to search + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} * * @return array [key => value] * @since 31.0.0 @@ -253,8 +253,8 @@ public function getValues( ): array { $this->assertParams($userId, $app, $prefix); // if we want to filter values, we need to get sensitivity - $this->loadPreferencesAll($userId); - // array_merge() will remove numeric keys (here preference keys), so addition arrays instead + $this->loadConfigAll($userId); + // array_merge() will remove numeric keys (here config keys), so addition arrays instead $values = array_filter( $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered), function (string $key) use ($prefix): bool { @@ -269,18 +269,18 @@ function (string $key) use ($prefix): bool { * @inheritDoc * * @param string $userId id of the user - * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} * * @return array> [appId => [key => value]] * @since 31.0.0 */ public function getAllValues(string $userId, bool $filtered = false): array { $this->assertParams($userId, allowEmptyApp: true); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); $result = []; foreach ($this->getApps($userId) as $app) { - // array_merge() will remove numeric keys (here preference keys), so addition arrays instead + // array_merge() will remove numeric keys (here config keys), so addition arrays instead $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []); $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered); } @@ -292,8 +292,8 @@ public function getAllValues(string $userId, bool $filtered = false): array { * @inheritDoc * * @param string $userId id of the user - * @param string $key preference key - * @param bool $lazy search within lazy loaded preferences + * @param string $key config key + * @param bool $lazy search within lazy loaded config * @param ValueType|null $typedAs enforce type for the returned values * * @return array [appId => value] @@ -301,7 +301,7 @@ public function getAllValues(string $userId, bool $filtered = false): array { */ public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { $this->assertParams($userId, '', $key, allowEmptyApp: true); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); /** @var array> $cache */ if ($lazy) { @@ -331,7 +331,7 @@ public function getValuesByApps(string $userId, string $key, bool $lazy = false, * @inheritDoc * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param ValueType|null $typedAs enforce type for the returned values * @param array|null $userIds limit to a list of user ids * @@ -390,8 +390,8 @@ public function getValuesByUsers( * @inheritDoc * * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value + * @param string $key config key + * @param string $value config value * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string * * @return Generator @@ -405,8 +405,8 @@ public function searchUsersByValueString(string $app, string $key, string $value * @inheritDoc * * @param string $app id of the app - * @param string $key preference key - * @param int $value preference value + * @param string $key config key + * @param int $value config value * * @return Generator * @since 31.0.0 @@ -419,8 +419,8 @@ public function searchUsersByValueInt(string $app, string $key, int $value): Gen * @inheritDoc * * @param string $app id of the app - * @param string $key preference key - * @param array $values list of preference values + * @param string $key config key + * @param array $values list of config values * * @return Generator * @since 31.0.0 @@ -433,8 +433,8 @@ public function searchUsersByValues(string $app, string $key, array $values): Ge * @inheritDoc * * @param string $app id of the app - * @param string $key preference key - * @param bool $value preference value + * @param string $key config key + * @param bool $value config value * * @return Generator * @since 31.0.0 @@ -448,7 +448,7 @@ public function searchUsersByValueBool(string $app, string $key, bool $value): G } /** - * returns a list of users with preference key set to a specific value, or within the list of + * returns a list of users with config key set to a specific value, or within the list of * possible values * * @param string $app @@ -506,7 +506,7 @@ private function searchUsersByTypedValue(string $app, string $key, string|array } /** - * Get the preference value as string. + * Get the config value as string. * If the value does not exist the given default will be returned. * * Set lazy to `null` to ignore it and get the value from either source. @@ -515,15 +515,15 @@ private function searchUsersByTypedValue(string $app, string $key, string|array * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param string $default preference value - * @param null|bool $lazy get preference as lazy loaded or not. can be NULL + * @param string $key config key + * @param string $default config value + * @param null|bool $lazy get config as lazy loaded or not. can be NULL * * @return string the value or $default * @throws TypeConflictException * @internal * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see getValueString() * @see getValueInt() * @see getValueFloat() @@ -558,15 +558,15 @@ public function getValueMixed( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param string $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return string stored preference value or $default if not set in database + * @return string stored config value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function getValueString( string $userId, @@ -583,15 +583,15 @@ public function getValueString( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param int $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return int stored preference value or $default if not set in database + * @return int stored config value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function getValueInt( string $userId, @@ -608,15 +608,15 @@ public function getValueInt( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param float $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return float stored preference value or $default if not set in database + * @return float stored config value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function getValueFloat( string $userId, @@ -633,15 +633,15 @@ public function getValueFloat( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return bool stored preference value or $default if not set in database + * @return bool stored config value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function getValueBool( string $userId, @@ -659,15 +659,15 @@ public function getValueBool( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param array $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return array stored preference value or $default if not set in database + * @return array stored config value or $default if not set in database * @throws InvalidArgumentException if one of the argument format is invalid * @throws TypeConflictException in case of conflict with the value type set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function getValueArray( string $userId, @@ -689,9 +689,9 @@ public function getValueArray( /** * @param string $userId * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param string $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * @param ValueType $type value type * * @return string @@ -706,7 +706,7 @@ private function getTypedValue( ValueType $type, ): string { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); /** * We ignore check if mixed type is requested. @@ -727,7 +727,7 @@ private function getTypedValue( * - we should still return an existing non-lazy value even if current method * is called with $lazy is true * - * This way, lazyCache will be empty until the load for lazy preferences value is requested. + * This way, lazyCache will be empty until the load for lazy config value is requested. */ if (isset($this->lazyCache[$userId][$app][$key])) { $value = $this->lazyCache[$userId][$app][$key]; @@ -746,19 +746,19 @@ private function getTypedValue( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @return ValueType type of the value - * @throws UnknownKeyException if preference key is not known - * @throws IncorrectTypeException if preferences value type is not known + * @throws UnknownKeyException if config key is not known + * @throws IncorrectTypeException if config value type is not known * @since 31.0.0 */ public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); if (!isset($this->valueDetails[$userId][$app][$key]['type'])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } return $this->valueDetails[$userId][$app][$key]['type']; @@ -769,42 +769,42 @@ public function getValueType(string $userId, string $app, string $key, ?bool $la * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $lazy lazy loading * * @return int flags applied to value - * @throws UnknownKeyException if preference key is not known - * @throws IncorrectTypeException if preferences value type is not known + * @throws UnknownKeyException if config key is not known + * @throws IncorrectTypeException if config value type is not known * @since 31.0.0 */ public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); if (!isset($this->valueDetails[$userId][$app][$key])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } return $this->valueDetails[$userId][$app][$key]['flags']; } /** - * Store a preference key and its value in database as VALUE_MIXED + * Store a config key and its value in database as VALUE_MIXED * * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value - * @param bool $lazy set preference as lazy loaded - * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param string $key config key + * @param string $value config value + * @param bool $lazy set config as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing config values. * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED * @internal * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see setValueString() * @see setValueInt() * @see setValueFloat() @@ -836,15 +836,15 @@ public function setValueMixed( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value - * @param bool $lazy set preference as lazy loaded - * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param string $key config key + * @param string $value config value + * @param bool $lazy set config as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing config values. * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function setValueString( string $userId, @@ -870,15 +870,15 @@ public function setValueString( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param int $value preference value - * @param bool $lazy set preference as lazy loaded - * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param string $key config key + * @param int $value config value + * @param bool $lazy set config as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing config values. * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function setValueInt( string $userId, @@ -908,15 +908,15 @@ public function setValueInt( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param float $value preference value - * @param bool $lazy set preference as lazy loaded - * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param string $key config key + * @param float $value config value + * @param bool $lazy set config as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing config values. * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function setValueFloat( string $userId, @@ -942,14 +942,14 @@ public function setValueFloat( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool $value preference value - * @param bool $lazy set preference as lazy loaded + * @param string $key config key + * @param bool $value config value + * @param bool $lazy set config as lazy loaded * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function setValueBool( string $userId, @@ -975,16 +975,16 @@ public function setValueBool( * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param array $value preference value - * @param bool $lazy set preference as lazy loaded - * @param bool $sensitive if TRUE value will be hidden when listing preference values. + * @param string $key config key + * @param array $value config value + * @param bool $lazy set config as lazy loaded + * @param bool $sensitive if TRUE value will be hidden when listing config values. * * @return bool TRUE if value was different, therefor updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one * @throws JsonException * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ public function setValueArray( string $userId, @@ -1011,22 +1011,22 @@ public function setValueArray( } /** - * Store a preference key and its value in database + * Store a config key and its value in database * - * If preference key is already known with the exact same preference value and same sensitive/lazy status, the - * database is not updated. If preference value was previously stored as sensitive, status will not be + * If config key is already known with the exact same config value and same sensitive/lazy status, the + * database is not updated. If config value was previously stored as sensitive, status will not be * altered. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value - * @param bool $lazy preferences set as lazy loaded + * @param string $key config key + * @param string $value config value + * @param bool $lazy config set as lazy loaded * @param ValueType $type value type * * @return bool TRUE if value was updated in database * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading */ private function setTypedValue( string $userId, @@ -1038,14 +1038,14 @@ private function setTypedValue( ValueType $type, ): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferences($userId, $lazy); + $this->loadConfig($userId, $lazy); $inserted = $refreshCache = false; $origValue = $value; $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags); if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); - $flags |= UserPreferences::FLAG_SENSITIVE; + $flags |= UserConfig::FLAG_SENSITIVE; } // if requested, we fill the 'indexed' field with current value @@ -1100,7 +1100,7 @@ private function setTypedValue( if (!$inserted) { $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; if ($currType === null) { // this might happen when switching lazy loading status - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); $currType = $this->valueDetails[$userId][$app][$key]['type']; } @@ -1166,13 +1166,13 @@ private function setTypedValue( } /** - * Change the type of preference value. + * Change the type of config value. * * **WARNING:** Method is internal and **MUST** not be used as it may break things. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param ValueType $type value type * * @return bool TRUE if database update were necessary @@ -1183,7 +1183,7 @@ private function setTypedValue( */ public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); $this->isLazy($userId, $app, $key); // confirm key exists $update = $this->connection->getQueryBuilder(); @@ -1204,7 +1204,7 @@ public function updateType(string $userId, string $app, string $key, ValueType $ * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $sensitive TRUE to set as sensitive, FALSE to unset * * @return bool TRUE if entry was found in database and an update was necessary @@ -1212,7 +1212,7 @@ public function updateType(string $userId, string $app, string $key, ValueType $ */ public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); try { if ($sensitive === $this->isSensitive($userId, $app, $key, null)) { @@ -1230,7 +1230,7 @@ public function updateSensitive(string $userId, string $app, string $key, bool $ } if (!isset($cache[$userId][$app][$key])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } $value = $cache[$userId][$app][$key]; @@ -1295,7 +1295,7 @@ public function updateGlobalSensitive(string $app, string $key, bool $sensitive) */ public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); try { if ($indexed === $this->isIndexed($userId, $app, $key, null)) { @@ -1313,7 +1313,7 @@ public function updateIndexed(string $userId, string $app, string $key, bool $in } if (!isset($cache[$userId][$app][$key])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } $value = $cache[$userId][$app][$key]; @@ -1367,7 +1367,7 @@ public function updateGlobalIndexed(string $app, string $key, bool $indexed): vo * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset * * @return bool TRUE if entry was found in database and an update was necessary @@ -1375,7 +1375,7 @@ public function updateGlobalIndexed(string $app, string $key, bool $indexed): vo */ public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool { $this->assertParams($userId, $app, $key); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); try { if ($lazy === $this->isLazy($userId, $app, $key)) { @@ -1403,7 +1403,7 @@ public function updateLazy(string $userId, string $app, string $key, bool $lazy) * @inheritDoc * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset * * @since 31.0.0 @@ -1426,15 +1426,15 @@ public function updateGlobalLazy(string $app, string $key, bool $lazy): void { * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @return array - * @throws UnknownKeyException if preference key is not known in database + * @throws UnknownKeyException if config key is not known in database * @since 31.0.0 */ public function getDetails(string $userId, string $app, string $key): array { $this->assertParams($userId, $app, $key); - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); $lazy = $this->isLazy($userId, $app, $key); if ($lazy) { @@ -1452,7 +1452,7 @@ public function getDetails(string $userId, string $app, string $key): array { } if (!isset($cache[$app][$key])) { - throw new UnknownKeyException('unknown preference key'); + throw new UnknownKeyException('unknown config key'); } $value = $cache[$app][$key]; @@ -1476,11 +1476,11 @@ public function getDetails(string $userId, string $app, string $key): array { * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @since 31.0.0 */ - public function deletePreference(string $userId, string $app, string $key): void { + public function deleteUserConfig(string $userId, string $app, string $key): void { $this->assertParams($userId, $app, $key); $qb = $this->connection->getQueryBuilder(); $qb->delete('preferences') @@ -1497,7 +1497,7 @@ public function deletePreference(string $userId, string $app, string $key): void * @inheritDoc * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @since 31.0.0 */ @@ -1529,7 +1529,7 @@ public function deleteApp(string $app): void { $this->clearCacheAll(); } - public function deleteAllPreferences(string $userId): void { + public function deleteAllUserConfig(string $userId): void { $this->assertParams($userId, '', allowEmptyApp: true); $qb = $this->connection->getQueryBuilder(); $qb->delete('preferences') @@ -1556,7 +1556,7 @@ public function clearCache(string $userId, bool $reload = false): void { return; } - $this->loadPreferencesAll($userId); + $this->loadConfigAll($userId); } /** @@ -1602,7 +1602,7 @@ private function isFlagged(int $needle, int $flags): bool { * * @param string $userId * @param string $app assert $app fit in database - * @param string $prefKey assert preference key fit in database + * @param string $prefKey assert config key fit in database * @param bool $allowEmptyUser * @param bool $allowEmptyApp $app can be empty string * @param ValueType|null $valueType assert value type is only one type @@ -1631,22 +1631,22 @@ private function assertParams( } } - private function loadPreferencesAll(string $userId): void { - $this->loadPreferences($userId, null); + private function loadConfigAll(string $userId): void { + $this->loadConfig($userId, null); } /** - * Load normal preferences or preferences set as lazy loaded + * Load normal config or config set as lazy loaded * - * @param bool|null $lazy set to TRUE to load preferences set as lazy loaded, set to NULL to load all preferences + * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config */ - private function loadPreferences(string $userId, ?bool $lazy = false): void { + private function loadConfig(string $userId, ?bool $lazy = false): void { if ($this->isLoaded($userId, $lazy)) { return; } if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log - $this->logger->debug('The loading of lazy UserPreferences values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); + $this->logger->debug('The loading of lazy UserConfig values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); } $qb = $this->connection->getQueryBuilder(); @@ -1654,7 +1654,7 @@ private function loadPreferences(string $userId, ?bool $lazy = false): void { $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags'); $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); - // we only need value from lazy when loadPreferences does not specify it + // we only need value from lazy when loadConfig does not specify it if ($lazy !== null) { $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); } else { @@ -1677,9 +1677,9 @@ private function loadPreferences(string $userId, ?bool $lazy = false): void { /** * if $lazy is: - * - false: will returns true if fast preferences are loaded - * - true : will returns true if lazy preferences are loaded - * - null : will returns true if both preferences are loaded + * - false: will returns true if fast config are loaded + * - true : will returns true if lazy config are loaded + * - null : will returns true if both config are loaded * * @param string $userId * @param bool $lazy @@ -1696,9 +1696,9 @@ private function isLoaded(string $userId, ?bool $lazy): bool { /** * if $lazy is: - * - false: set fast preferences as loaded - * - true : set lazy preferences as loaded - * - null : set both preferences as loaded + * - false: set fast config as loaded + * - true : set lazy config as loaded + * - null : set both config as loaded * * @param string $userId * @param bool $lazy @@ -1726,7 +1726,7 @@ private function setAsLoaded(string $userId, ?bool $lazy): void { * * @param string $userId id of the user * @param string $app id of the app - * @param bool $filtered TRUE to hide sensitive preference values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} * * @return array */ diff --git a/lib/private/Server.php b/lib/private/Server.php index dc4b3c4f7dc7a..d57ddf61c0378 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -7,7 +7,7 @@ namespace OC; use bantu\IniGetWrapper\IniGetWrapper; -use NCU\Config\IUserPreferences; +use NCU\Config\IUserConfig; use OC\Accounts\AccountManager; use OC\App\AppManager; use OC\App\AppStore\Bundles\BundleFetcher; @@ -568,7 +568,7 @@ public function __construct($webRoot, \OC\Config $config) { }); $this->registerAlias(IAppConfig::class, \OC\AppConfig::class); - $this->registerAlias(IUserPreferences::class, \OC\Config\UserPreferences::class); + $this->registerAlias(IUserConfig::class, \OC\Config\UserConfig::class); $this->registerService(IFactory::class, function (Server $c) { return new \OC\L10N\Factory( diff --git a/lib/public/IConfig.php b/lib/public/IConfig.php index f434f838d45c3..3bc64c5e13337 100644 --- a/lib/public/IConfig.php +++ b/lib/public/IConfig.php @@ -245,7 +245,7 @@ public function deleteAppFromAllUsers($appName); * @param string $appName the app to get the user for * @param string $key the key to get the user for * @param string $value the value to get the user for - * @return array of user IDs + * @return list of user IDs * @since 31.0.0 return type of `list` * @since 8.0.0 */ diff --git a/lib/unstable/Config/Exceptions/IncorrectTypeException.php b/lib/unstable/Config/Exceptions/IncorrectTypeException.php index b3eedde5300f9..a5e4954cdb21c 100644 --- a/lib/unstable/Config/Exceptions/IncorrectTypeException.php +++ b/lib/unstable/Config/Exceptions/IncorrectTypeException.php @@ -11,6 +11,7 @@ use Exception; /** + * @experimental 31.0.0 * @since 31.0.0 */ class IncorrectTypeException extends Exception { diff --git a/lib/unstable/Config/Exceptions/TypeConflictException.php b/lib/unstable/Config/Exceptions/TypeConflictException.php index 042991f8a57e5..c192b2c4f9dff 100644 --- a/lib/unstable/Config/Exceptions/TypeConflictException.php +++ b/lib/unstable/Config/Exceptions/TypeConflictException.php @@ -11,6 +11,7 @@ use Exception; /** + * @experimental 31.0.0 * @since 31.0.0 */ class TypeConflictException extends Exception { diff --git a/lib/unstable/Config/Exceptions/UnknownKeyException.php b/lib/unstable/Config/Exceptions/UnknownKeyException.php index 80626c6a6355a..5f83800cafcb9 100644 --- a/lib/unstable/Config/Exceptions/UnknownKeyException.php +++ b/lib/unstable/Config/Exceptions/UnknownKeyException.php @@ -11,6 +11,7 @@ use Exception; /** + * @experimental 31.0.0 * @since 31.0.0 */ class UnknownKeyException extends Exception { diff --git a/lib/unstable/Config/IUserPreferences.php b/lib/unstable/Config/IUserConfig.php similarity index 57% rename from lib/unstable/Config/IUserPreferences.php rename to lib/unstable/Config/IUserConfig.php index a2695a0d05392..21e5b6fb014b8 100644 --- a/lib/unstable/Config/IUserPreferences.php +++ b/lib/unstable/Config/IUserConfig.php @@ -13,12 +13,12 @@ use NCU\Config\Exceptions\UnknownKeyException; /** - * This class provides an easy way for apps to store user preferences in the + * This class provides an easy way for apps to store user config in the * database. * Supports **lazy loading** * * ### What is lazy loading ? - * In order to avoid loading useless user preferences into memory for each request, + * In order to avoid loading useless user config into memory for each request, * only non-lazy values are now loaded. * * Once a value that is lazy is requested, all lazy values will be loaded. @@ -27,17 +27,17 @@ * lazy loading. Use them wisely and only on parts of the code that are called * during specific requests or actions to avoid loading the lazy values all the time. * + * @experimental 31.0.0 * @since 31.0.0 */ - -interface IUserPreferences { +interface IUserConfig { /** @since 31.0.0 */ public const FLAG_SENSITIVE = 1; // value is sensitive /** @since 31.0.0 */ public const FLAG_INDEXED = 2; // value should be indexed /** - * Get list of all userIds with preferences stored in database. + * Get list of all userIds with config stored in database. * If $appId is specified, will only limit the search to this value * * **WARNING:** ignore any cache and get data directly from database. @@ -50,10 +50,10 @@ interface IUserPreferences { public function getUserIds(string $appId = ''): array; /** - * Get list of all apps that have at least one preference + * Get list of all apps that have at least one config * value related to $userId stored in database * - * **WARNING:** ignore lazy filtering, all user preferences are loaded from database + * **WARNING:** ignore lazy filtering, all user config are loaded from database * * @param string $userId id of the user * @@ -66,23 +66,23 @@ public function getApps(string $userId): array; * Returns all keys stored in database, related to user+app. * Please note that the values are not returned. * - * **WARNING:** ignore lazy filtering, all user preferences are loaded from database + * **WARNING:** ignore lazy filtering, all user config are loaded from database * * @param string $userId id of the user * @param string $app id of the app * - * @return list list of stored preference keys + * @return list list of stored config keys * @since 31.0.0 */ public function getKeys(string $userId, string $app): array; /** - * Check if a key exists in the list of stored preference values. + * Check if a key exists in the list of stored config values. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool $lazy search within lazy loaded preferences + * @param string $key config key + * @param bool $lazy search within lazy loaded config * * @return bool TRUE if key exists * @since 31.0.0 @@ -94,11 +94,11 @@ public function hasKey(string $userId, string $app, string $key, ?bool $lazy = f * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool|null $lazy search within lazy loaded preferences + * @param string $key config key + * @param bool|null $lazy search within lazy loaded config * * @return bool TRUE if value is sensitive - * @throws UnknownKeyException if preference key is not known + * @throws UnknownKeyException if config key is not known * @since 31.0.0 */ public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool; @@ -113,41 +113,41 @@ public function isSensitive(string $userId, string $app, string $key, ?bool $laz * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool|null $lazy search within lazy loaded preferences + * @param string $key config key + * @param bool|null $lazy search within lazy loaded config * * @return bool TRUE if value is sensitive - * @throws UnknownKeyException if preference key is not known + * @throws UnknownKeyException if config key is not known * @since 31.0.0 */ public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool; /** - * Returns if the preference key stored in database is lazy loaded + * Returns if the config key stored in database is lazy loaded * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * - * @return bool TRUE if preference is lazy loaded - * @throws UnknownKeyException if preference key is not known - * @see IUserPreferences for details about lazy loading + * @return bool TRUE if config is lazy loaded + * @throws UnknownKeyException if config key is not known + * @see IUserConfig for details about lazy loading * @since 31.0.0 */ public function isLazy(string $userId, string $app, string $key): bool; /** - * List all preference values from an app with preference key starting with $key. - * Returns an array with preference key as key, stored value as value. + * List all config values from an app with config key starting with $key. + * Returns an array with config key as key, stored value as value. * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * * @param string $userId id of the user * @param string $app id of the app - * @param string $prefix preference keys prefix to search, can be empty. - * @param bool $filtered filter sensitive preference values + * @param string $prefix config keys prefix to search, can be empty. + * @param bool $filtered filter sensitive config values * * @return array [key => value] * @since 31.0.0 @@ -155,13 +155,13 @@ public function isLazy(string $userId, string $app, string $key): bool; public function getValues(string $userId, string $app, string $prefix = '', bool $filtered = false): array; /** - * List all preference values of a user. - * Returns an array with preference key as key, stored value as value. + * List all config values of a user. + * Returns an array with config key as key, stored value as value. * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * * @param string $userId id of the user - * @param bool $filtered filter sensitive preference values + * @param bool $filtered filter sensitive config values * * @return array [key => value] * @since 31.0.0 @@ -169,12 +169,12 @@ public function getValues(string $userId, string $app, string $prefix = '', bool public function getAllValues(string $userId, bool $filtered = false): array; /** - * List all apps storing a specific preference key and its stored value. + * List all apps storing a specific config key and its stored value. * Returns an array with appId as key, stored value as value. * * @param string $userId id of the user - * @param string $key preference key - * @param bool $lazy search within lazy loaded preferences + * @param string $key config key + * @param bool $lazy search within lazy loaded config * @param ValueType|null $typedAs enforce type for the returned values * * @return array [appId => value] @@ -183,13 +183,13 @@ public function getAllValues(string $userId, bool $filtered = false): array; public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array; /** - * List all users storing a specific preference key and its stored value. + * List all users storing a specific config key and its stored value. * Returns an array with userId as key, stored value as value. * * **WARNING:** no caching, generate a fresh request * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param ValueType|null $typedAs enforce type for the returned values * @param array|null $userIds limit the search to a list of user ids * @@ -199,14 +199,14 @@ public function getValuesByApps(string $userId, string $key, bool $lazy = false, public function getValuesByUsers(string $app, string $key, ?ValueType $typedAs = null, ?array $userIds = null): array; /** - * List all users storing a specific preference key/value pair. + * List all users storing a specific config key/value pair. * Returns a list of user ids. * * **WARNING:** no caching, generate a fresh request * * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value + * @param string $key config key + * @param string $value config value * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string * * @return Generator @@ -215,14 +215,14 @@ public function getValuesByUsers(string $app, string $key, ?ValueType $typedAs = public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator; /** - * List all users storing a specific preference key/value pair. + * List all users storing a specific config key/value pair. * Returns a list of user ids. * * **WARNING:** no caching, generate a fresh request * * @param string $app id of the app - * @param string $key preference key - * @param int $value preference value + * @param string $key config key + * @param int $value config value * * @return Generator * @since 31.0.0 @@ -230,14 +230,14 @@ public function searchUsersByValueString(string $app, string $key, string $value public function searchUsersByValueInt(string $app, string $key, int $value): Generator; /** - * List all users storing a specific preference key/value pair. + * List all users storing a specific config key/value pair. * Returns a list of user ids. * * **WARNING:** no caching, generate a fresh request * * @param string $app id of the app - * @param string $key preference key - * @param array $values list of possible preference values + * @param string $key config key + * @param array $values list of possible config values * * @return Generator * @since 31.0.0 @@ -245,14 +245,14 @@ public function searchUsersByValueInt(string $app, string $key, int $value): Gen public function searchUsersByValues(string $app, string $key, array $values): Generator; /** - * List all users storing a specific preference key/value pair. + * List all users storing a specific config key/value pair. * Returns a list of user ids. * * **WARNING:** no caching, generate a fresh request * * @param string $app id of the app - * @param string $key preference key - * @param bool $value preference value + * @param string $key config key + * @param bool $value config value * * @return Generator * @since 31.0.0 @@ -260,19 +260,19 @@ public function searchUsersByValues(string $app, string $key, array $values): Ge public function searchUsersByValueBool(string $app, string $key, bool $value): Generator; /** - * Get user preference assigned to a preference key. - * If preference key is not found in database, default value is returned. - * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * Get user config assigned to a config key. + * If config key is not found in database, default value is returned. + * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param string $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return string stored preference value or $default if not set in database + * @return string stored config value or $default if not set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see getValueInt() * @see getValueFloat() * @see getValueBool() @@ -281,19 +281,19 @@ public function searchUsersByValueBool(string $app, string $key, bool $value): G public function getValueString(string $userId, string $app, string $key, string $default = '', bool $lazy = false): string; /** - * Get preference value assigned to a preference key. - * If preference key is not found in database, default value is returned. - * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * Get config value assigned to a config key. + * If config key is not found in database, default value is returned. + * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param int $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return int stored preference value or $default if not set in database + * @return int stored config value or $default if not set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see getValueString() * @see getValueFloat() * @see getValueBool() @@ -302,19 +302,19 @@ public function getValueString(string $userId, string $app, string $key, string public function getValueInt(string $userId, string $app, string $key, int $default = 0, bool $lazy = false): int; /** - * Get preference value assigned to a preference key. - * If preference key is not found in database, default value is returned. - * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * Get config value assigned to a config key. + * If config key is not found in database, default value is returned. + * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param float $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return float stored preference value or $default if not set in database + * @return float stored config value or $default if not set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see getValueString() * @see getValueInt() * @see getValueBool() @@ -323,17 +323,17 @@ public function getValueInt(string $userId, string $app, string $key, int $defau public function getValueFloat(string $userId, string $app, string $key, float $default = 0, bool $lazy = false): float; /** - * Get preference value assigned to a preference key. - * If preference key is not found in database, default value is returned. - * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * Get config value assigned to a config key. + * If config key is not found in database, default value is returned. + * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $default default value - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return bool stored preference value or $default if not set in database + * @return bool stored config value or $default if not set in database * @since 31.0.0 * @see IUserPrefences for explanation about lazy loading * @see getValueString() @@ -344,19 +344,19 @@ public function getValueFloat(string $userId, string $app, string $key, float $d public function getValueBool(string $userId, string $app, string $key, bool $default = false, bool $lazy = false): bool; /** - * Get preference value assigned to a preference key. - * If preference key is not found in database, default value is returned. - * If preference key is set as lazy loaded, the $lazy argument needs to be set to TRUE. + * Get config value assigned to a config key. + * If config key is not found in database, default value is returned. + * If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param array $default default value` - * @param bool $lazy search within lazy loaded preferences + * @param bool $lazy search within lazy loaded config * - * @return array stored preference value or $default if not set in database + * @return array stored config value or $default if not set in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see getValueString() * @see getValueInt() * @see getValueFloat() @@ -365,59 +365,59 @@ public function getValueBool(string $userId, string $app, string $key, bool $def public function getValueArray(string $userId, string $app, string $key, array $default = [], bool $lazy = false): array; /** - * returns the type of preference value + * returns the type of config value * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * unless lazy is set to false * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool|null $lazy * * @return ValueType type of the value - * @throws UnknownKeyException if preference key is not known - * @throws IncorrectTypeException if preferences value type is not known + * @throws UnknownKeyException if config key is not known + * @throws IncorrectTypeException if config value type is not known * @since 31.0.0 */ public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType; /** - * returns a bitflag related to preference value + * returns a bitflag related to config value * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * unless lazy is set to false * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $lazy lazy loading * - * @return int a bitflag in relation to the preference value - * @throws UnknownKeyException if preference key is not known - * @throws IncorrectTypeException if preferences value type is not known + * @return int a bitflag in relation to the config value + * @throws UnknownKeyException if config key is not known + * @throws IncorrectTypeException if config value type is not known * @since 31.0.0 */ public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int; /** - * Store a preference key and its value in database + * Store a config key and its value in database * - * If preference key is already known with the exact same preference value, the database is not updated. - * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * If config key is already known with the exact same config value, the database is not updated. + * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. * - * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param string $value preference value - * @param bool $sensitive if TRUE value will be hidden when listing preference values. - * @param bool $lazy set preference as lazy loaded + * @param string $key config key + * @param string $value config value + * @param bool $sensitive if TRUE value will be hidden when listing config values. + * @param bool $lazy set config as lazy loaded * * @return bool TRUE if value was different, therefor updated in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see setValueInt() * @see setValueFloat() * @see setValueBool() @@ -426,28 +426,28 @@ public function getValueFlags(string $userId, string $app, string $key, bool $la public function setValueString(string $userId, string $app, string $key, string $value, bool $lazy = false, int $flags = 0): bool; /** - * Store a preference key and its value in database + * Store a config key and its value in database * * When handling huge value around and/or above 2,147,483,647, a debug log will be generated * on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers. * * When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()} * - * If preference key is already known with the exact same preference value, the database is not updated. - * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * If config key is already known with the exact same config value, the database is not updated. + * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. * - * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param int $value preference value - * @param bool $sensitive if TRUE value will be hidden when listing preference values. - * @param bool $lazy set preference as lazy loaded + * @param string $key config key + * @param int $value config value + * @param bool $sensitive if TRUE value will be hidden when listing config values. + * @param bool $lazy set config as lazy loaded * * @return bool TRUE if value was different, therefor updated in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see setValueString() * @see setValueFloat() * @see setValueBool() @@ -456,23 +456,23 @@ public function setValueString(string $userId, string $app, string $key, string public function setValueInt(string $userId, string $app, string $key, int $value, bool $lazy = false, int $flags = 0): bool; /** - * Store a preference key and its value in database. + * Store a config key and its value in database. * - * If preference key is already known with the exact same preference value, the database is not updated. - * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * If config key is already known with the exact same config value, the database is not updated. + * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. * - * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param float $value preference value - * @param bool $sensitive if TRUE value will be hidden when listing preference values. - * @param bool $lazy set preference as lazy loaded + * @param string $key config key + * @param float $value config value + * @param bool $sensitive if TRUE value will be hidden when listing config values. + * @param bool $lazy set config as lazy loaded * * @return bool TRUE if value was different, therefor updated in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see setValueString() * @see setValueInt() * @see setValueBool() @@ -481,22 +481,22 @@ public function setValueInt(string $userId, string $app, string $key, int $value public function setValueFloat(string $userId, string $app, string $key, float $value, bool $lazy = false, int $flags = 0): bool; /** - * Store a preference key and its value in database + * Store a config key and its value in database * - * If preference key is already known with the exact same preference value, the database is not updated. - * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * If config key is already known with the exact same config value, the database is not updated. + * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. * - * If preference value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first + * If config value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param bool $value preference value - * @param bool $lazy set preference as lazy loaded + * @param string $key config key + * @param bool $value config value + * @param bool $lazy set config as lazy loaded * * @return bool TRUE if value was different, therefor updated in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see setValueString() * @see setValueInt() * @see setValueFloat() @@ -505,23 +505,23 @@ public function setValueFloat(string $userId, string $app, string $key, float $v public function setValueBool(string $userId, string $app, string $key, bool $value, bool $lazy = false): bool; /** - * Store a preference key and its value in database + * Store a config key and its value in database * - * If preference key is already known with the exact same preference value, the database is not updated. - * If preference key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. + * If config key is already known with the exact same config value, the database is not updated. + * If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. * - * If preference value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first + * If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key - * @param array $value preference value - * @param bool $sensitive if TRUE value will be hidden when listing preference values. - * @param bool $lazy set preference as lazy loaded + * @param string $key config key + * @param array $value config value + * @param bool $sensitive if TRUE value will be hidden when listing config values. + * @param bool $lazy set config as lazy loaded * * @return bool TRUE if value was different, therefor updated in database * @since 31.0.0 - * @see IUserPreferences for explanation about lazy loading + * @see IUserConfig for explanation about lazy loading * @see setValueString() * @see setValueInt() * @see setValueFloat() @@ -530,13 +530,13 @@ public function setValueBool(string $userId, string $app, string $key, bool $val public function setValueArray(string $userId, string $app, string $key, array $value, bool $lazy = false, int $flags = 0): bool; /** - * switch sensitive status of a preference value + * switch sensitive status of a config value * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $sensitive TRUE to set as sensitive, FALSE to unset * * @return bool TRUE if database update were necessary @@ -545,12 +545,12 @@ public function setValueArray(string $userId, string $app, string $key, array $v public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool; /** - * switch sensitive loading status of a preference key for all users + * switch sensitive loading status of a config key for all users * * **Warning:** heavy on resources, MUST only be used on occ command or migrations * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $sensitive TRUE to set as sensitive, FALSE to unset * * @since 31.0.0 @@ -559,13 +559,13 @@ public function updateGlobalSensitive(string $app, string $key, bool $sensitive) /** - * switch indexed status of a preference value + * switch indexed status of a config value * - * **WARNING:** ignore lazy filtering, all preference values are loaded from database + * **WARNING:** ignore lazy filtering, all config values are loaded from database * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $indexed TRUE to set as indexed, FALSE to unset * * @return bool TRUE if database update were necessary @@ -574,23 +574,23 @@ public function updateGlobalSensitive(string $app, string $key, bool $sensitive) public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool; /** - * switch sensitive loading status of a preference key for all users + * switch sensitive loading status of a config key for all users * * **Warning:** heavy on resources, MUST only be used on occ command or migrations * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $indexed TRUE to set as indexed, FALSE to unset * @since 31.0.0 */ public function updateGlobalIndexed(string $app, string $key, bool $indexed): void; /** - * switch lazy loading status of a preference value + * switch lazy loading status of a config value * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset * * @return bool TRUE if database update was necessary @@ -599,19 +599,19 @@ public function updateGlobalIndexed(string $app, string $key, bool $indexed): vo public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool; /** - * switch lazy loading status of a preference key for all users + * switch lazy loading status of a config key for all users * * **Warning:** heavy on resources, MUST only be used on occ command or migrations * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset * @since 31.0.0 */ public function updateGlobalLazy(string $app, string $key, bool $lazy): void; /** - * returns an array contains details about a preference value + * returns an array contains details about a config value * * ``` * [ @@ -627,37 +627,37 @@ public function updateGlobalLazy(string $app, string $key, bool $lazy): void; * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @return array - * @throws UnknownKeyException if preference key is not known in database + * @throws UnknownKeyException if config key is not known in database * @since 31.0.0 */ public function getDetails(string $userId, string $app, string $key): array; /** - * Delete single preference key from database. + * Delete single config key from database. * * @param string $userId id of the user * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @since 31.0.0 */ - public function deletePreference(string $userId, string $app, string $key): void; + public function deleteUserConfig(string $userId, string $app, string $key): void; /** - * Delete preference values from all users linked to a specific preference keys + * Delete config values from all users linked to a specific config keys * * @param string $app id of the app - * @param string $key preference key + * @param string $key config key * * @since 31.0.0 */ public function deleteKey(string $app, string $key): void; /** - * delete all preference keys linked to an app + * delete all config keys linked to an app * * @param string $app id of the app * @since 31.0.0 @@ -665,17 +665,17 @@ public function deleteKey(string $app, string $key): void; public function deleteApp(string $app): void; /** - * delete all preference keys linked to a user + * delete all config keys linked to a user * * @param string $userId id of the user * @since 31.0.0 */ - public function deleteAllPreferences(string $userId): void; + public function deleteAllUserConfig(string $userId): void; /** * Clear the cache for a single user * - * The cache will be rebuilt only the next time a user preference is requested. + * The cache will be rebuilt only the next time a user config is requested. * * @param string $userId id of the user * @param bool $reload set to TRUE to refill cache instantly after clearing it @@ -686,7 +686,7 @@ public function clearCache(string $userId, bool $reload = false): void; /** * Clear the cache for all users. - * The cache will be rebuilt only the next time a user preference is requested. + * The cache will be rebuilt only the next time a user config is requested. * * @since 31.0.0 */ diff --git a/lib/unstable/Config/ValueType.php b/lib/unstable/Config/ValueType.php index 2c3c5851e3f5d..3e1e47e9d7c4f 100644 --- a/lib/unstable/Config/ValueType.php +++ b/lib/unstable/Config/ValueType.php @@ -14,6 +14,7 @@ /** * Listing of available value type for typed config value * + * @experimental 31.0.0 * @since 31.0.0 */ enum ValueType: int { diff --git a/tests/lib/UserPreferencesTest.php b/tests/lib/Config/UserConfigTest.php similarity index 82% rename from tests/lib/UserPreferencesTest.php rename to tests/lib/Config/UserConfigTest.php index c149af002d355..a79e1e59a7920 100644 --- a/tests/lib/UserPreferencesTest.php +++ b/tests/lib/Config/UserConfigTest.php @@ -5,13 +5,13 @@ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-only */ -namespace lib; +namespace lib\Config; use NCU\Config\Exceptions\TypeConflictException; use NCU\Config\Exceptions\UnknownKeyException; -use NCU\Config\IUserPreferences; +use NCU\Config\IUserConfig; use NCU\Config\ValueType; -use OC\Config\UserPreferences; +use OC\Config\UserConfig; use OCP\IDBConnection; use OCP\Security\ICrypto; use Psr\Log\LoggerInterface; @@ -24,7 +24,7 @@ * * @package Test */ -class UserPreferencesTest extends TestCase { +class UserConfigTest extends TestCase { protected IDBConnection $connection; private LoggerInterface $logger; private ICrypto $crypto; @@ -43,30 +43,30 @@ class UserPreferencesTest extends TestCase { 'fast_string' => ['fast_string', 'f_value', ValueType::STRING], 'lazy_string' => ['lazy_string', 'l_value', ValueType::STRING, true], 'fast_string_sensitive' => [ - 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE + 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE ], 'lazy_string_sensitive' => [ - 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE + 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, UserConfig::FLAG_SENSITIVE ], 'fast_int' => ['fast_int', 11, ValueType::INT], 'lazy_int' => ['lazy_int', 12, ValueType::INT, true], - 'fast_int_sensitive' => ['fast_int_sensitive', 2024, ValueType::INT, false, UserPreferences::FLAG_SENSITIVE], - 'lazy_int_sensitive' => ['lazy_int_sensitive', 2048, ValueType::INT, true, UserPreferences::FLAG_SENSITIVE], + 'fast_int_sensitive' => ['fast_int_sensitive', 2024, ValueType::INT, false, UserConfig::FLAG_SENSITIVE], + 'lazy_int_sensitive' => ['lazy_int_sensitive', 2048, ValueType::INT, true, UserConfig::FLAG_SENSITIVE], 'fast_float' => ['fast_float', 3.14, ValueType::FLOAT], 'lazy_float' => ['lazy_float', 3.14159, ValueType::FLOAT, true], 'fast_float_sensitive' => [ - 'fast_float_sensitive', 1.41, ValueType::FLOAT, false, UserPreferences::FLAG_SENSITIVE + 'fast_float_sensitive', 1.41, ValueType::FLOAT, false, UserConfig::FLAG_SENSITIVE ], 'lazy_float_sensitive' => [ - 'lazy_float_sensitive', 1.4142, ValueType::FLOAT, true, UserPreferences::FLAG_SENSITIVE + 'lazy_float_sensitive', 1.4142, ValueType::FLOAT, true, UserConfig::FLAG_SENSITIVE ], 'fast_array' => ['fast_array', ['year' => 2024], ValueType::ARRAY], 'lazy_array' => ['lazy_array', ['month' => 'October'], ValueType::ARRAY, true], 'fast_array_sensitive' => [ - 'fast_array_sensitive', ['password' => 'pwd'], ValueType::ARRAY, false, UserPreferences::FLAG_SENSITIVE + 'fast_array_sensitive', ['password' => 'pwd'], ValueType::ARRAY, false, UserConfig::FLAG_SENSITIVE ], 'lazy_array_sensitive' => [ - 'lazy_array_sensitive', ['password' => 'qwerty'], ValueType::ARRAY, true, UserPreferences::FLAG_SENSITIVE + 'lazy_array_sensitive', ['password' => 'qwerty'], ValueType::ARRAY, true, UserConfig::FLAG_SENSITIVE ], 'fast_boolean' => ['fast_boolean', true, ValueType::BOOL], 'fast_boolean_0' => ['fast_boolean_0', false, ValueType::BOOL], @@ -76,20 +76,20 @@ class UserPreferencesTest extends TestCase { 'app2' => [ 'key2' => ['key2', 'value2a', ValueType::STRING, false, 0, true], 'key3' => ['key3', 'value3', ValueType::STRING, true], - 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE], 'key8' => ['key8', 11, ValueType::INT, false, 0, true], 'key9' => ['key9', 'value9a', ValueType::STRING], ], 'app3' => [ 'key1' => ['key1', 'value123'], 'key3' => ['key3', 'value3'], - 'key8' => ['key8', 12, ValueType::INT, false, UserPreferences::FLAG_SENSITIVE, true], - 'key9' => ['key9', 'value9b', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key8' => ['key8', 12, ValueType::INT, false, UserConfig::FLAG_SENSITIVE, true], + 'key9' => ['key9', 'value9b', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE], 'key10' => ['key10', true, ValueType::BOOL, false, 0, true], ], 'only-lazy' => [ 'key1' => ['key1', 'value456', ValueType::STRING, true, 0, true], - 'key2' => ['key2', 'value2c', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE], + 'key2' => ['key2', 'value2c', ValueType::STRING, true, UserConfig::FLAG_SENSITIVE], 'key3' => ['key3', 42, ValueType::INT, true], 'key4' => ['key4', 17.42, ValueType::FLOAT, true], 'key5' => ['key5', true, ValueType::BOOL, true], @@ -99,16 +99,16 @@ class UserPreferencesTest extends TestCase { [ 'app1' => [ '1' => ['1', 'value1'], - '2' => ['2', 'value2', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE], + '2' => ['2', 'value2', ValueType::STRING, true, UserConfig::FLAG_SENSITIVE], '3' => ['3', 17, ValueType::INT, true], - '4' => ['4', 42, ValueType::INT, false, UserPreferences::FLAG_SENSITIVE], + '4' => ['4', 42, ValueType::INT, false, UserConfig::FLAG_SENSITIVE], '5' => ['5', 17.42, ValueType::FLOAT, false], '6' => ['6', true, ValueType::BOOL, false], ], 'app2' => [ 'key2' => ['key2', 'value2b', ValueType::STRING, false, 0, true], 'key3' => ['key3', 'value3', ValueType::STRING, true], - 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE], 'key8' => ['key8', 12, ValueType::INT, false, 0, true], ], 'app3' => [ @@ -123,12 +123,12 @@ class UserPreferencesTest extends TestCase { 'app2' => [ 'key2' => ['key2', 'value2c', ValueType::MIXED, false, 0, true], 'key3' => ['key3', 'value3', ValueType::STRING, true, ], - 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE], 'fast_string_sensitive' => [ - 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE + 'fast_string_sensitive', 'fs_value', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE ], 'lazy_string_sensitive' => [ - 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, UserPreferences::FLAG_SENSITIVE + 'lazy_string_sensitive', 'ls_value', ValueType::STRING, true, UserConfig::FLAG_SENSITIVE ], ], 'only-lazy' => [ @@ -141,7 +141,7 @@ class UserPreferencesTest extends TestCase { 'key1' => ['key1', 'value1'], 'key2' => ['key2', 'value2A', ValueType::MIXED, false, 0, true], 'key3' => ['key3', 'value3', ValueType::STRING, true,], - 'key4' => ['key4', 'value4', ValueType::STRING, false, UserPreferences::FLAG_SENSITIVE], + 'key4' => ['key4', 'value4', ValueType::STRING, false, UserConfig::FLAG_SENSITIVE], ], 'app3' => [ 'key10' => ['key10', true, ValueType::BOOL, false, 0, true], @@ -214,8 +214,8 @@ protected function setUp(): void { } $flags = $row[4] ?? 0; - if ((UserPreferences::FLAG_SENSITIVE & $flags) !== 0) { - $value = self::invokePrivate(UserPreferences::class, 'ENCRYPTION_PREFIX') + if ((UserConfig::FLAG_SENSITIVE & $flags) !== 0) { + $value = self::invokePrivate(UserConfig::class, 'ENCRYPTION_PREFIX') . $this->crypto->encrypt((string)$value); } else { $indexed = (($row[5] ?? false) === true) ? $value : ''; @@ -272,28 +272,28 @@ protected function tearDown(): void { /** * @param array $preLoading preload the 'fast' cache for a list of users) * - * @return IUserPreferences + * @return IUserConfig */ - private function generateUserPreferences(array $preLoading = []): IUserPreferences { - $preferences = new \OC\Config\UserPreferences( + private function generateUserConfig(array $preLoading = []): IUserConfig { + $userConfig = new \OC\Config\UserConfig( $this->connection, $this->logger, $this->crypto, ); - $msg = ' generateUserPreferences() failed to confirm cache status'; + $msg = ' generateUserConfig() failed to confirm cache status'; // confirm cache status - $status = $preferences->statusCache(); + $status = $userConfig->statusCache(); $this->assertSame([], $status['fastLoaded'], $msg); $this->assertSame([], $status['lazyLoaded'], $msg); $this->assertSame([], $status['fastCache'], $msg); $this->assertSame([], $status['lazyCache'], $msg); foreach ($preLoading as $preLoadUser) { // simple way to initiate the load of non-lazy preferences values in cache - $preferences->getValueString($preLoadUser, 'core', 'preload'); + $userConfig->getValueString($preLoadUser, 'core', 'preload'); // confirm cache status - $status = $preferences->statusCache(); + $status = $userConfig->statusCache(); $this->assertSame(true, $status['fastLoaded'][$preLoadUser], $msg); $this->assertSame(false, $status['lazyLoaded'][$preLoadUser], $msg); @@ -302,30 +302,30 @@ private function generateUserPreferences(array $preLoading = []): IUserPreferenc $this->assertSame([], array_keys($status['lazyCache'][$preLoadUser]), $msg); } - return $preferences; + return $userConfig; } public function testGetUserIdsEmpty(): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing(array_keys($this->basePreferences), $preferences->getUserIds()); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing(array_keys($this->basePreferences), $userConfig->getUserIds()); } public function testGetUserIds(): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing(['user1', 'user2', 'user5'], $preferences->getUserIds('app1')); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing(['user1', 'user2', 'user5'], $userConfig->getUserIds('app1')); } public function testGetApps(): void { - $preferences = $this->generateUserPreferences(); + $userConfig = $this->generateUserConfig(); $this->assertEqualsCanonicalizing( - array_keys($this->basePreferences['user1']), $preferences->getApps('user1') + array_keys($this->basePreferences['user1']), $userConfig->getApps('user1') ); } public function testGetKeys(): void { - $preferences = $this->generateUserPreferences(['user1']); + $userConfig = $this->generateUserConfig(['user1']); $this->assertEqualsCanonicalizing( - array_keys($this->basePreferences['user1']['app1']), $preferences->getKeys('user1', 'app1') + array_keys($this->basePreferences['user1']['app1']), $userConfig->getKeys('user1', 'app1') ); } @@ -350,8 +350,8 @@ public function providerHasKey(): array { * @dataProvider providerHasKey */ public function testHasKey(string $userId, string $appId, string $key, ?bool $lazy, bool $result): void { - $preferences = $this->generateUserPreferences(); - $this->assertEquals($result, $preferences->hasKey($userId, $appId, $key, $lazy)); + $userConfig = $this->generateUserConfig(); + $this->assertEquals($result, $userConfig->hasKey($userId, $appId, $key, $lazy)); } /** @@ -387,12 +387,12 @@ public function testIsSensitive( bool $result, bool $exception, ): void { - $preferences = $this->generateUserPreferences(); + $userConfig = $this->generateUserConfig(); if ($exception) { $this->expectException(UnknownKeyException::class); } - $this->assertEquals($result, $preferences->isSensitive($userId, $appId, $key, $lazy)); + $this->assertEquals($result, $userConfig->isSensitive($userId, $appId, $key, $lazy)); } /** @@ -420,12 +420,12 @@ public function testIsLazy( bool $result, bool $exception, ): void { - $preferences = $this->generateUserPreferences(); + $userConfig = $this->generateUserConfig(); if ($exception) { $this->expectException(UnknownKeyException::class); } - $this->assertEquals($result, $preferences->isLazy($userId, $appId, $key)); + $this->assertEquals($result, $userConfig->isLazy($userId, $appId, $key)); } public function providerGetValues(): array { @@ -555,9 +555,9 @@ public function testGetValues( bool $filtered, array $result, ): void { - $preferences = $this->generateUserPreferences(); + $userConfig = $this->generateUserConfig(); $this->assertJsonStringEqualsJsonString( - json_encode($result), json_encode($preferences->getValues($userId, $appId, $prefix, $filtered)) + json_encode($result), json_encode($userConfig->getValues($userId, $appId, $prefix, $filtered)) ); } @@ -654,8 +654,8 @@ public function testGetAllValues( bool $filtered, array $result, ): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, $preferences->getAllValues($userId, $filtered)); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing($result, $userConfig->getAllValues($userId, $filtered)); } public function providerSearchValuesByApps(): array { @@ -700,8 +700,8 @@ public function testSearchValuesByApps( ?ValueType $typedAs, array $result, ): void { - $preferences = $this->generateUserPreferences(); - $this->assertEquals($result, $preferences->getValuesByApps($userId, $key, $lazy, $typedAs)); + $userConfig = $this->generateUserConfig(); + $this->assertEquals($result, $userConfig->getValuesByApps($userId, $key, $lazy, $typedAs)); } public function providerSearchValuesByUsers(): array { @@ -750,9 +750,9 @@ public function testSearchValuesByUsers( ?array $userIds = null, array $result, ): void { - $preferences = $this->generateUserPreferences(); + $userConfig = $this->generateUserConfig(); $this->assertEqualsCanonicalizing( - $result, $preferences->getValuesByUsers($app, $key, $typedAs, $userIds) + $result, $userConfig->getValuesByUsers($app, $key, $typedAs, $userIds) ); } @@ -774,8 +774,8 @@ public function testSearchUsersByValueString( bool $ci, array $result, ): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValueString($app, $key, $value, $ci))); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing($result, iterator_to_array($userConfig->searchUsersByValueString($app, $key, $value, $ci))); } public function providerSearchValuesByValueInt(): array { @@ -795,8 +795,8 @@ public function testSearchUsersByValueInt( int $value, array $result, ): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValueInt($app, $key, $value))); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing($result, iterator_to_array($userConfig->searchUsersByValueInt($app, $key, $value))); } public function providerSearchValuesByValues(): array { @@ -815,8 +815,8 @@ public function testSearchUsersByValues( array $values, array $result, ): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValues($app, $key, $values))); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing($result, iterator_to_array($userConfig->searchUsersByValues($app, $key, $values))); } public function providerSearchValuesByValueBool(): array { @@ -835,8 +835,8 @@ public function testSearchUsersByValueBool( bool $value, array $result, ): void { - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing($result, iterator_to_array($preferences->searchUsersByValueBool($app, $key, $value))); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing($result, iterator_to_array($userConfig->searchUsersByValueBool($app, $key, $value))); } public function providerGetValueMixed(): array { @@ -919,8 +919,8 @@ public function testGetValueMixed( bool $lazy, string $result, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($result, $preferences->getValueMixed($userId, $app, $key, $default, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($result, $userConfig->getValueMixed($userId, $app, $key, $default, $lazy)); } /** @@ -935,8 +935,8 @@ public function testGetValueString( bool $lazy, string $result, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($result, $preferences->getValueString($userId, $app, $key, $default, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($result, $userConfig->getValueString($userId, $app, $key, $default, $lazy)); } public function providerGetValueInt(): array { @@ -977,8 +977,8 @@ public function testGetValueInt( bool $lazy, int $result, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($result, $preferences->getValueInt($userId, $app, $key, $default, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($result, $userConfig->getValueInt($userId, $app, $key, $default, $lazy)); } public function providerGetValueFloat(): array { @@ -1018,8 +1018,8 @@ public function testGetValueFloat( bool $lazy, float $result, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($result, $preferences->getValueFloat($userId, $app, $key, $default, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($result, $userConfig->getValueFloat($userId, $app, $key, $default, $lazy)); } public function providerGetValueBool(): array { @@ -1079,8 +1079,8 @@ public function testGetValueBool( bool $lazy, bool $result, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($result, $preferences->getValueBool($userId, $app, $key, $default, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($result, $userConfig->getValueBool($userId, $app, $key, $default, $lazy)); } public function providerGetValueArray(): array { @@ -1116,9 +1116,9 @@ public function testGetValueArray( bool $lazy, array $result, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); $this->assertEqualsCanonicalizing( - $result, $preferences->getValueArray($userId, $app, $key, $default, $lazy) + $result, $userConfig->getValueArray($userId, $app, $key, $default, $lazy) ); } @@ -1172,12 +1172,12 @@ public function testGetValueType( ?ValueType $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $type = $preferences->getValueType($userId, $app, $key, $lazy); + $type = $userConfig->getValueType($userId, $app, $key, $lazy); if ($exception === null) { $this->assertEquals($result->value, $type->value); } @@ -1233,12 +1233,12 @@ public function testSetValueMixed( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->setValueMixed($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); + $edited = $userConfig->setValueMixed($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception === null) { $this->assertEquals($result, $edited); @@ -1303,21 +1303,21 @@ public function testSetValueString( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->setValueString($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); + $edited = $userConfig->setValueString($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; } $this->assertEquals($result, $edited); if ($result) { - $this->assertEquals($value, $preferences->getValueString($userId, $app, $key, $value, $lazy)); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($value, $preferences->getValueString($userId, $app, $key, $value, $lazy)); + $this->assertEquals($value, $userConfig->getValueString($userId, $app, $key, $value, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($value, $userConfig->getValueString($userId, $app, $key, $value, $lazy)); } } @@ -1366,12 +1366,12 @@ public function testSetValueInt( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->setValueInt($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); + $edited = $userConfig->setValueInt($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; @@ -1379,9 +1379,9 @@ public function testSetValueInt( $this->assertEquals($result, $edited); if ($result) { - $this->assertEquals($value, $preferences->getValueInt($userId, $app, $key, $value, $lazy)); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($value, $preferences->getValueInt($userId, $app, $key, $value, $lazy)); + $this->assertEquals($value, $userConfig->getValueInt($userId, $app, $key, $value, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($value, $userConfig->getValueInt($userId, $app, $key, $value, $lazy)); } } @@ -1429,12 +1429,12 @@ public function testSetValueFloat( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->setValueFloat($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); + $edited = $userConfig->setValueFloat($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; @@ -1442,9 +1442,9 @@ public function testSetValueFloat( $this->assertEquals($result, $edited); if ($result) { - $this->assertEquals($value, $preferences->getValueFloat($userId, $app, $key, $value, $lazy)); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($value, $preferences->getValueFloat($userId, $app, $key, $value, $lazy)); + $this->assertEquals($value, $userConfig->getValueFloat($userId, $app, $key, $value, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($value, $userConfig->getValueFloat($userId, $app, $key, $value, $lazy)); } } @@ -1493,12 +1493,12 @@ public function testSetValueArray( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->setValueArray($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); + $edited = $userConfig->setValueArray($userId, $app, $key, $value, $lazy, ($sensitive) ? 1 : 0); if ($exception !== null) { return; @@ -1507,11 +1507,11 @@ public function testSetValueArray( $this->assertEquals($result, $edited); if ($result) { $this->assertEqualsCanonicalizing( - $value, $preferences->getValueArray($userId, $app, $key, $value, $lazy) + $value, $userConfig->getValueArray($userId, $app, $key, $value, $lazy) ); - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); $this->assertEqualsCanonicalizing( - $value, $preferences->getValueArray($userId, $app, $key, $value, $lazy) + $value, $userConfig->getValueArray($userId, $app, $key, $value, $lazy) ); } } @@ -1537,26 +1537,26 @@ public function testUpdateSensitive( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->updateSensitive($userId, $app, $key, $sensitive); + $edited = $userConfig->updateSensitive($userId, $app, $key, $sensitive); if ($exception !== null) { return; } $this->assertEquals($result, $edited); if ($result) { - $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key)); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key)); + $this->assertEquals($sensitive, $userConfig->isSensitive($userId, $app, $key)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($sensitive, $userConfig->isSensitive($userId, $app, $key)); if ($sensitive) { $this->assertEquals(true, str_starts_with( - $preferences->statusCache()['fastCache'][$userId][$app][$key] ?? - $preferences->statusCache()['lazyCache'][$userId][$app][$key], - '$UserPreferencesEncryption$') + $userConfig->statusCache()['fastCache'][$userId][$app][$key] ?? + $userConfig->statusCache()['lazyCache'][$userId][$app][$key], + '$UserConfigEncryption$') ); } } @@ -1570,7 +1570,7 @@ public function providerUpdateGlobalSensitive(): array { * @dataProvider providerUpdateGlobalSensitive */ public function testUpdateGlobalSensitive(bool $sensitive): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); $app = 'app2'; if ($sensitive) { $key = 'key2'; @@ -1580,28 +1580,28 @@ public function testUpdateGlobalSensitive(bool $sensitive): void { $value = 'value4'; } - $this->assertEquals($value, $preferences->getValueString('user1', $app, $key)); + $this->assertEquals($value, $userConfig->getValueString('user1', $app, $key)); foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { - $preferences->getValueString($userId, $app, $key); // cache loading for userId + $userConfig->getValueString($userId, $app, $key); // cache loading for userId $this->assertEquals( !$sensitive, str_starts_with( - $preferences->statusCache()['fastCache'][$userId][$app][$key] ?? - $preferences->statusCache()['lazyCache'][$userId][$app][$key], - '$UserPreferencesEncryption$' + $userConfig->statusCache()['fastCache'][$userId][$app][$key] ?? + $userConfig->statusCache()['lazyCache'][$userId][$app][$key], + '$UserConfigEncryption$' ) ); } - $preferences->updateGlobalSensitive($app, $key, $sensitive); + $userConfig->updateGlobalSensitive($app, $key, $sensitive); - $this->assertEquals($value, $preferences->getValueString('user1', $app, $key)); + $this->assertEquals($value, $userConfig->getValueString('user1', $app, $key)); foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { - $this->assertEquals($sensitive, $preferences->isSensitive($userId, $app, $key)); + $this->assertEquals($sensitive, $userConfig->isSensitive($userId, $app, $key)); // should only work if updateGlobalSensitive drop cache $this->assertEquals($sensitive, str_starts_with( - $preferences->statusCache()['fastCache'][$userId][$app][$key] ?? - $preferences->statusCache()['lazyCache'][$userId][$app][$key], - '$UserPreferencesEncryption$') + $userConfig->statusCache()['fastCache'][$userId][$app][$key] ?? + $userConfig->statusCache()['lazyCache'][$userId][$app][$key], + '$UserConfigEncryption$') ); } } @@ -1627,21 +1627,21 @@ public function testUpdateLazy( bool $result, ?string $exception = null, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); if ($exception !== null) { $this->expectException($exception); } - $edited = $preferences->updateLazy($userId, $app, $key, $lazy); + $edited = $userConfig->updateLazy($userId, $app, $key, $lazy); if ($exception !== null) { return; } $this->assertEquals($result, $edited); if ($result) { - $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key)); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key)); + $this->assertEquals($lazy, $userConfig->isLazy($userId, $app, $key)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals($lazy, $userConfig->isLazy($userId, $app, $key)); } } @@ -1653,7 +1653,7 @@ public function providerUpdateGlobalLazy(): array { * @dataProvider providerUpdateGlobalLazy */ public function testUpdateGlobalLazy(bool $lazy): void { - $preferences = $this->generateUserPreferences($preload ?? []); + $userConfig = $this->generateUserConfig($preload ?? []); $app = 'app2'; if ($lazy) { $key = 'key4'; @@ -1663,15 +1663,15 @@ public function testUpdateGlobalLazy(bool $lazy): void { $value = 'value3'; } - $this->assertEquals($value, $preferences->getValueString('user1', $app, $key, '', !$lazy)); + $this->assertEquals($value, $userConfig->getValueString('user1', $app, $key, '', !$lazy)); foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { - $this->assertEquals(!$lazy, $preferences->isLazy($userId, $app, $key)); + $this->assertEquals(!$lazy, $userConfig->isLazy($userId, $app, $key)); } - $preferences->updateGlobalLazy($app, $key, $lazy); - $this->assertEquals($value, $preferences->getValueString('user1', $app, $key, '', $lazy)); + $userConfig->updateGlobalLazy($app, $key, $lazy); + $this->assertEquals($value, $userConfig->getValueString('user1', $app, $key, '', $lazy)); foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { - $this->assertEquals($lazy, $preferences->isLazy($userId, $app, $key)); + $this->assertEquals($lazy, $userConfig->isLazy($userId, $app, $key)); } } @@ -1723,8 +1723,8 @@ public function providerGetDetails(): array { * @dataProvider providerGetDetails */ public function testGetDetails(string $userId, string $app, string $key, array $result): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEqualsCanonicalizing($result, $preferences->getDetails($userId, $app, $key)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEqualsCanonicalizing($result, $userConfig->getDetails($userId, $app, $key)); } @@ -1748,15 +1748,15 @@ public function testDeletePreference( string $app, string $key, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $lazy = $preferences->isLazy($userId, $app, $key); + $userConfig = $this->generateUserConfig($preload ?? []); + $lazy = $userConfig->isLazy($userId, $app, $key); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals(true, $preferences->hasKey($userId, $app, $key, $lazy)); - $preferences->deletePreference($userId, $app, $key); - $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, $lazy)); - $preferences = $this->generateUserPreferences($preload ?? []); - $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals(true, $userConfig->hasKey($userId, $app, $key, $lazy)); + $userConfig->deleteUserConfig($userId, $app, $key); + $this->assertEquals(false, $userConfig->hasKey($userId, $app, $key, $lazy)); + $userConfig = $this->generateUserConfig($preload ?? []); + $this->assertEquals(false, $userConfig->hasKey($userId, $app, $key, $lazy)); } public function providerDeleteKey(): array { @@ -1778,50 +1778,50 @@ public function testDeleteKey( string $app, string $key, ): void { - $preferences = $this->generateUserPreferences($preload ?? []); - $preferences->deleteKey($app, $key); + $userConfig = $this->generateUserConfig($preload ?? []); + $userConfig->deleteKey($app, $key); foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { - $this->assertEquals(false, $preferences->hasKey($userId, $app, $key, null)); - $preferencesTemp = $this->generateUserPreferences($preload ?? []); - $this->assertEquals(false, $preferencesTemp->hasKey($userId, $app, $key, null)); + $this->assertEquals(false, $userConfig->hasKey($userId, $app, $key, null)); + $userConfigTemp = $this->generateUserConfig($preload ?? []); + $this->assertEquals(false, $userConfigTemp->hasKey($userId, $app, $key, null)); } } public function testDeleteApp(): void { - $preferences = $this->generateUserPreferences(); - $preferences->deleteApp('only-lazy'); + $userConfig = $this->generateUserConfig(); + $userConfig->deleteApp('only-lazy'); foreach (['user1', 'user2', 'user3', 'user4'] as $userId) { - $this->assertEquals(false, in_array('only-lazy', $preferences->getApps($userId))); - $preferencesTemp = $this->generateUserPreferences(); - $this->assertEquals(false, in_array('only-lazy', $preferencesTemp->getApps($userId))); + $this->assertEquals(false, in_array('only-lazy', $userConfig->getApps($userId))); + $userConfigTemp = $this->generateUserConfig(); + $this->assertEquals(false, in_array('only-lazy', $userConfigTemp->getApps($userId))); } } public function testDeleteAllPreferences(): void { - $preferences = $this->generateUserPreferences(); - $preferences->deleteAllPreferences('user1'); + $userConfig = $this->generateUserConfig(); + $userConfig->deleteAllUserConfig('user1'); - $this->assertEqualsCanonicalizing([], $preferences->getApps('user1')); - $preferences = $this->generateUserPreferences(); - $this->assertEqualsCanonicalizing([], $preferences->getApps('user1')); + $this->assertEqualsCanonicalizing([], $userConfig->getApps('user1')); + $userConfig = $this->generateUserConfig(); + $this->assertEqualsCanonicalizing([], $userConfig->getApps('user1')); } public function testClearCache(): void { - $preferences = $this->generateUserPreferences(['user1', 'user2']); - $preferences->clearCache('user1'); + $userConfig = $this->generateUserConfig(['user1', 'user2']); + $userConfig->clearCache('user1'); - $this->assertEquals(true, $preferences->statusCache()['fastLoaded']['user2']); - $this->assertEquals(false, $preferences->statusCache()['fastLoaded']['user1']); - $this->assertEquals('value2a', $preferences->getValueString('user1', 'app2', 'key2')); - $this->assertEquals(false, $preferences->statusCache()['lazyLoaded']['user1']); - $this->assertEquals(true, $preferences->statusCache()['fastLoaded']['user1']); + $this->assertEquals(true, $userConfig->statusCache()['fastLoaded']['user2']); + $this->assertEquals(false, $userConfig->statusCache()['fastLoaded']['user1']); + $this->assertEquals('value2a', $userConfig->getValueString('user1', 'app2', 'key2')); + $this->assertEquals(false, $userConfig->statusCache()['lazyLoaded']['user1']); + $this->assertEquals(true, $userConfig->statusCache()['fastLoaded']['user1']); } public function testClearCacheAll(): void { - $preferences = $this->generateUserPreferences(['user1', 'user2']); - $preferences->clearCacheAll(); + $userConfig = $this->generateUserConfig(['user1', 'user2']); + $userConfig->clearCacheAll(); $this->assertEqualsCanonicalizing( [ 'fastLoaded' => [], @@ -1830,7 +1830,7 @@ public function testClearCacheAll(): void { 'lazyCache' => [], 'valueTypes' => [], ], - $preferences->statusCache() + $userConfig->statusCache() ); } }