fix(user-prefs): adding sensitive and indexed as flags

Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
This commit is contained in:
Maxence Lange 2024-10-17 08:48:09 -01:00
parent 65e24f7def
commit e73513bdd1
7 changed files with 481 additions and 287 deletions

View file

@ -14,6 +14,7 @@ use OCP\DB\Types;
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 @@ use OCP\Migration\SimpleMigrationStep;
*/
#[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 @@ class Version31000Date20240814184402 extends SimpleMigrationStep {
$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;
}

View file

@ -226,9 +226,15 @@ class AllConfig implements IConfig {
* @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 @@ class AllConfig implements IConfig {
}
/** @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 @@ class AllConfig implements IConfig {
* @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 @@ class AllConfig implements IConfig {
* @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 @@ class AllConfig implements IConfig {
* @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 @@ class AllConfig implements IConfig {
* [ $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 @@ class AllConfig implements IConfig {
* @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 @@ class AllConfig implements IConfig {
* @param string $key the key to get the user for
* @param string $value the value to get the user for
* @return list<string> 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 @@ class AllConfig implements IConfig {
* @param string $key the key to get the user for
* @param string $value the value to get the user for
* @return list<string> 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() {

View file

@ -22,7 +22,6 @@ use OCP\UserPreferences\Exceptions\UnknownKeyException;
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<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */
/** @var array<string, array<string, array<string, mixed>>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */
private array $fastCache = []; // cache for normal preference keys
/** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */
private array $lazyCache = []; // cache for lazy preference keys
/** @var array<string, array<string, array<string, int>>> ['user_id' => ['app_id' => ['key' => bitflag]]] */
/** @var array<string, array<string, array<string, <'type', ValueType>|<'flags', int>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]] */
private array $valueDetails = []; // type for all preference values
/** @var array<string, array<string, array<string, ValueType>>> ['user_id' => ['app_id' => ['key' => bitflag]]] */
private array $valueTypes = []; // type for all preference values
/** @var array<string, array<string, array<string, int>>> ['user_id' => ['app_id' => ['key' => bitflag]]] */
private array $valueFlags = []; // type for all preference values
/** @var array<string, boolean> ['user_id' => bool] */
private array $fastLoaded = [];
/** @var array<string, boolean> ['user_id' => bool] */
@ -157,6 +161,8 @@ class UserPreferences implements IUserPreferences {
}
/**
* @inheritDoc
*
* @param string $userId id of the user
* @param string $app id of the app
* @param string $key preference key
@ -164,17 +170,40 @@ class UserPreferences implements IUserPreferences {
*
* @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 @@ class UserPreferences implements IUserPreferences {
* @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 @@ class UserPreferences implements IUserPreferences {
* @return array<string, string|int|float|bool|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 @@ class UserPreferences implements IUserPreferences {
* @return array<string, string|int|float|bool|array> [userId => value]
* @since 31.0.0
*/
public function searchValuesByUsers(
public function getValuesByUsers(
string $app,
string $key,
?ValueType $typedAs = null,
@ -328,7 +358,7 @@ class UserPreferences implements IUserPreferences {
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 @@ class UserPreferences implements IUserPreferences {
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<string>
* @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 @@ class UserPreferences implements IUserPreferences {
* @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<string>
*/
@ -436,20 +483,39 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
?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 @@ class UserPreferences implements IUserPreferences {
* @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 @@ class UserPreferences implements IUserPreferences {
* @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 @@ class UserPreferences implements IUserPreferences {
* @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 @@ class UserPreferences implements IUserPreferences {
* 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 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
* @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 @@ class UserPreferences implements IUserPreferences {
string $key,
string $value,
bool $lazy = false,
bool $sensitive = false,
int $flags = 0,
): bool {
return $this->setTypedValue(
$userId,
@ -774,7 +846,7 @@ class UserPreferences implements IUserPreferences {
$key,
$value,
$lazy,
$sensitive,
$flags,
ValueType::MIXED
);
}
@ -792,7 +864,7 @@ class UserPreferences implements IUserPreferences {
*
* @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 @@ class UserPreferences implements IUserPreferences {
string $key,
string $value,
bool $lazy = false,
bool $sensitive = false,
int $flags = 0,
): bool {
return $this->setTypedValue(
$userId,
@ -809,7 +881,7 @@ class UserPreferences implements IUserPreferences {
$key,
$value,
$lazy,
$sensitive,
$flags,
ValueType::STRING
);
}
@ -826,7 +898,7 @@ class UserPreferences implements IUserPreferences {
*
* @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 @@ class UserPreferences implements IUserPreferences {
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 @@ class UserPreferences implements IUserPreferences {
$key,
(string)$value,
$lazy,
$sensitive,
$flags,
ValueType::INT
);
}
@ -864,7 +936,7 @@ class UserPreferences implements IUserPreferences {
*
* @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 @@ class UserPreferences implements IUserPreferences {
string $key,
float $value,
bool $lazy = false,
bool $sensitive = false,
int $flags = 0,
): bool {
return $this->setTypedValue(
$userId,
@ -881,7 +953,7 @@ class UserPreferences implements IUserPreferences {
$key,
(string)$value,
$lazy,
$sensitive,
$flags,
ValueType::FLOAT
);
}
@ -897,7 +969,7 @@ class UserPreferences implements IUserPreferences {
*
* @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 @@ class UserPreferences implements IUserPreferences {
string $key,
bool $value,
bool $lazy = false,
int $flags = 0
): bool {
return $this->setTypedValue(
$userId,
@ -913,7 +986,7 @@ class UserPreferences implements IUserPreferences {
$key,
($value) ? '1' : '0',
$lazy,
false,
$flags,
ValueType::BOOL
);
}
@ -931,7 +1004,7 @@ class UserPreferences implements IUserPreferences {
* @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 @@ class UserPreferences implements IUserPreferences {
string $key,
array $value,
bool $lazy = false,
bool $sensitive = false,
int $flags = 0,
): bool {
try {
return $this->setTypedValue(
@ -949,7 +1022,7 @@ class UserPreferences implements IUserPreferences {
$key,
json_encode($value, JSON_THROW_ON_ERROR),
$lazy,
$sensitive,
$flags,
ValueType::ARRAY
);
} catch (JsonException $e) {
@ -982,7 +1055,7 @@ class UserPreferences implements IUserPreferences {
string $key,
string $value,
bool $lazy,
bool $sensitive,
int $flags,
ValueType $type,
): bool {
$this->assertParams($userId, $app, $key, valueType: $type);
@ -990,10 +1063,22 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
->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 @@ class UserPreferences implements IUserPreferences {
* 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 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
} 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 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
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 @@ class UserPreferences implements IUserPreferences {
*/
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 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
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 @@ class UserPreferences implements IUserPreferences {
*/
public function clearCacheAll(): void {
$this->lazyLoaded = $this->fastLoaded = [];
$this->lazyCache = $this->fastCache = $this->valueTypes = [];
$this->lazyCache = $this->fastCache = $this->valueDetails = [];
}
/**
@ -1444,18 +1604,18 @@ class UserPreferences implements IUserPreferences {
'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 @@ class UserPreferences implements IUserPreferences {
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 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
$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 @@ class UserPreferences implements IUserPreferences {
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 @@ class UserPreferences implements IUserPreferences {
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;
}

View file

@ -15,6 +15,9 @@ use OCP\UserPreferences\Exceptions\UnknownKeyException;
* @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 @@ interface IUserPreferences {
*/
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 @@ interface IUserPreferences {
* @return array<string, string|int|float|bool|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 @@ interface IUserPreferences {
* @return array<string, string|int|float|bool|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 @@ interface IUserPreferences {
*/
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 @@ interface IUserPreferences {
* @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 @@ interface IUserPreferences {
* @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 @@ interface IUserPreferences {
* @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 @@ interface IUserPreferences {
* @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 @@ interface IUserPreferences {
*
* **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 @@ interface IUserPreferences {
*/
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
*

View file

@ -10,7 +10,6 @@ namespace OCP\UserPreferences;
use OCP\UserPreferences\Exceptions\IncorrectTypeException;
use UnhandledMatchError;
use ValueError;
/**
* Listing of available value type for user preferences
@ -20,22 +19,20 @@ use ValueError;
*/
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 @@ enum ValueType: int {
* @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);

View file

@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\UserPreferences;
/**
* Listing of value type definition for user preferences
*
* @see IUserPreferences
* @since 31.0.0
*/
enum ValueTypeDefinition: string {
/** @since 30.0.0 */
case MIXED = 'mixed';
/** @since 30.0.0 */
case STRING = 'string';
/** @since 30.0.0 */
case INT = 'int';
/** @since 30.0.0 */
case FLOAT = 'float';
/** @since 30.0.0 */
case BOOL = 'bool';
/** @since 30.0.0 */
case ARRAY = 'array';
}

View file

@ -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, 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 @@ class UserPreferencesTest extends TestCase {
'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 @@ class UserPreferencesTest extends TestCase {
$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 @@ class UserPreferencesTest extends TestCase {
'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 @@ class UserPreferencesTest extends TestCase {
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 @@ class UserPreferencesTest extends TestCase {
): void {
$preferences = $this->generateUserPreferences();
$this->assertEqualsCanonicalizing(
$result, $preferences->searchValuesByUsers($app, $key, $typedAs, $userIds)
$result, $preferences->getValuesByUsers($app, $key, $typedAs, $userIds)
);
}
@ -770,9 +775,7 @@ class UserPreferencesTest extends TestCase {
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 @@ class UserPreferencesTest extends TestCase {
$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 @@ class UserPreferencesTest extends TestCase {
$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 @@ class UserPreferencesTest extends TestCase {
$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 @@ class UserPreferencesTest extends TestCase {
$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 @@ class UserPreferencesTest extends TestCase {
$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 @@ class UserPreferencesTest extends TestCase {
'app' => 'app2',
'key' => 'key2',
'value' => 'value2c',
'type' => 2,
'type' => 0,
'lazy' => false,
'typeString' => 'mixed',
'sensitive' => false
@ -1694,7 +1697,7 @@ class UserPreferencesTest extends TestCase {
'app' => 'app1',
'key' => 'lazy_int',
'value' => 12,
'type' => 8,
'type' => 2,
'lazy' => true,
'typeString' => 'int',
'sensitive' => false
@ -1707,7 +1710,7 @@ class UserPreferencesTest extends TestCase {
'app' => 'app1',
'key' => 'fast_float_sensitive',
'value' => 1.41,
'type' => 16,
'type' => 3,
'lazy' => false,
'typeString' => 'float',
'sensitive' => true