From 6e0abf92fd7507b8eb16a4fbba10bbbef406feec Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Thu, 10 Oct 2024 11:24:51 -0100 Subject: [PATCH] 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 | 340 ++++++++++++------ .../UserPreferences/IUserPreferences.php | 79 +++- lib/public/UserPreferences/ValueType.php | 76 ++-- .../UserPreferences/ValueTypeDefinition.php | 30 -- tests/lib/UserPreferencesTest.php | 121 ++++--- 7 files changed, 424 insertions(+), 289 deletions(-) delete mode 100644 lib/public/UserPreferences/ValueTypeDefinition.php diff --git a/core/Migrations/Version31000Date20240814184402.php b/core/Migrations/Version31000Date20240814184402.php index 20804b1b19efe..c9da60f7856e7 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'], 'prefs_app_key_ind_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..add69a692e5b7 100644 --- a/lib/private/UserPreferences.php +++ b/lib/private/UserPreferences.php @@ -45,15 +45,23 @@ 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']]] */ + public const FLAG_SENSITIVE = 1; // value is sensitive + public const FLAG_INDEXED = 2; // value should be indexed + + /** @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 +165,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 +174,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 +220,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 +302,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 +341,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 +362,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 +404,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,10 +474,11 @@ 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 */ - 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, bool $withinNotIndexedField = false): array { $this->assertParams('', $app, $key, allowEmptyUser: true); $qb = $this->connection->getQueryBuilder(); @@ -436,20 +487,51 @@ 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))); + // TODO: remove $configValueColumn once $withinNotIndexedField is fully deprecated $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()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)); + + // TODO: remove this condition once $withinNotIndexedField is fully deprecated + if ($withinNotIndexedField) { + // search within indexed OR configvalue + $where = $qb->expr()->orX( + $where, + $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))); + $where = $qb->expr()->eq( + $qb->func()->lower('indexed'), + $qb->createNamedParameter(strtolower($value))); + + // TODO: remove this condition once $withinNotIndexedField is fully deprecated + if ($withinNotIndexedField) { + $where = $qb->expr()->orX( + $where, + $qb->expr()->eq( + $qb->func()->lower($configValueColumn), + $qb->createNamedParameter(strtolower($value))) + ); + } + + } else { + $where = $qb->expr()->eq('indexed', $qb->createNamedParameter($value)); + + // TODO: remove this condition once $withinNotIndexedField is fully deprecated + if ($withinNotIndexedField) { + $where = $qb->expr()->orX( + $where, + $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) + ); + } } } + $qb->andWhere($where); + $userIds = []; $result = $qb->executeQuery(); $rows = $result->fetchAll(); @@ -493,7 +575,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 +652,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 +677,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 +703,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 +750,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 +794,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 +840,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 +854,7 @@ public function setValueMixed( string $key, string $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -774,7 +862,7 @@ public function setValueMixed( $key, $value, $lazy, - $sensitive, + $flags, ValueType::MIXED ); } @@ -792,7 +880,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 +889,7 @@ public function setValueString( string $key, string $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -809,7 +897,7 @@ public function setValueString( $key, $value, $lazy, - $sensitive, + $flags, ValueType::STRING ); } @@ -826,7 +914,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 +923,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 +935,7 @@ public function setValueInt( $key, (string)$value, $lazy, - $sensitive, + $flags, ValueType::INT ); } @@ -864,7 +952,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 +961,7 @@ public function setValueFloat( string $key, float $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { return $this->setTypedValue( $userId, @@ -881,7 +969,7 @@ public function setValueFloat( $key, (string)$value, $lazy, - $sensitive, + $flags, ValueType::FLOAT ); } @@ -897,7 +985,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 +994,7 @@ public function setValueBool( string $key, bool $value, bool $lazy = false, + int $flags = 0 ): bool { return $this->setTypedValue( $userId, @@ -913,7 +1002,7 @@ public function setValueBool( $key, ($value) ? '1' : '0', $lazy, - false, + $flags, ValueType::BOOL ); } @@ -931,7 +1020,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 +1029,7 @@ public function setValueArray( string $key, array $value, bool $lazy = false, - bool $sensitive = false, + int $flags = 0, ): bool { try { return $this->setTypedValue( @@ -949,7 +1038,7 @@ public function setValueArray( $key, json_encode($value, JSON_THROW_ON_ERROR), $lazy, - $sensitive, + $flags, ValueType::ARRAY ); } catch (JsonException $e) { @@ -982,7 +1071,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 +1079,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 +1117,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 +1135,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 +1173,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 +1194,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 +1222,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,33 +1270,40 @@ 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; + } + + public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { return true; } + + public function updateGlobalIndexed(string $app, string $key, bool $indexed): void { + + } + /** * @inheritDoc * @@ -1212,7 +1315,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) { @@ -1411,7 +1514,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 +1530,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 +1547,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 +1595,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 +1622,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 +1636,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 +1710,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 +1754,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..46e5336c18ef0 100644 --- a/lib/public/UserPreferences/IUserPreferences.php +++ b/lib/public/UserPreferences/IUserPreferences.php @@ -82,6 +82,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 +159,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 +175,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 +361,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 +402,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 +432,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 +457,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 +506,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 +528,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 +536,35 @@ 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