Skip to content

Commit 4475658

Browse files
authored
Merge pull request #30739 from nextcloud/enhancement/expose_config_getalluservalues
Add IConfig::getAllUserValues to get user settings for all applications
2 parents 8e92b3d + ddae16d commit 4475658

4 files changed

Lines changed: 30 additions & 30 deletions

File tree

core/Command/User/Setting.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
use OC\Core\Command\Base;
2929
use OCP\IConfig;
30-
use OCP\IDBConnection;
3130
use OCP\IUser;
3231
use OCP\IUserManager;
3332
use Symfony\Component\Console\Input\InputArgument;
@@ -42,19 +41,14 @@ class Setting extends Base {
4241
/** @var IConfig */
4342
protected $config;
4443

45-
/** @var IDBConnection */
46-
protected $connection;
47-
4844
/**
4945
* @param IUserManager $userManager
5046
* @param IConfig $config
51-
* @param IDBConnection $connection
5247
*/
53-
public function __construct(IUserManager $userManager, IConfig $config, IDBConnection $connection) {
48+
public function __construct(IUserManager $userManager, IConfig $config) {
5449
parent::__construct();
5550
$this->userManager = $userManager;
5651
$this->config = $config;
57-
$this->connection = $connection;
5852
}
5953

6054
protected function configure() {
@@ -247,26 +241,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
247241
}
248242

249243
protected function getUserSettings($uid, $app) {
250-
$query = $this->connection->getQueryBuilder();
251-
$query->select('*')
252-
->from('preferences')
253-
->where($query->expr()->eq('userid', $query->createNamedParameter($uid)));
254-
244+
$settings = $this->config->getAllUserValues($uid);
255245
if ($app !== '') {
256-
$query->andWhere($query->expr()->eq('appid', $query->createNamedParameter($app)));
257-
}
258-
259-
$result = $query->execute();
260-
$settings = [];
261-
while ($row = $result->fetch()) {
262-
$settings[$row['appid']][$row['configkey']] = $row['configvalue'];
246+
if (isset($settings[$app])) {
247+
$settings = [$app => $settings[$app]];
248+
} else {
249+
$settings = [];
250+
}
263251
}
264252

265253
$user = $this->userManager->get($uid);
266254
$settings['settings']['display_name'] = $user->getDisplayName();
267255

268-
$result->closeCursor();
269-
270256
return $settings;
271257
}
272258
}

core/register_command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
$application->add(new OC\Core\Command\User\LastSeen(\OC::$server->getUserManager()));
185185
$application->add(\OC::$server->get(\OC\Core\Command\User\Report::class));
186186
$application->add(new OC\Core\Command\User\ResetPassword(\OC::$server->getUserManager()));
187-
$application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig(), \OC::$server->getDatabaseConnection()));
187+
$application->add(new OC\Core\Command\User\Setting(\OC::$server->getUserManager(), \OC::$server->getConfig()));
188188
$application->add(new OC\Core\Command\User\ListCommand(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
189189
$application->add(new OC\Core\Command\User\Info(\OC::$server->getUserManager(), \OC::$server->getGroupManager()));
190190
$application->add(new OC\Core\Command\User\AddAppPassword(\OC::$server->get(\OCP\IUserManager::class), \OC::$server->get(\OC\Authentication\Token\IProvider::class), \OC::$server->get(\OCP\Security\ISecureRandom::class), \OC::$server->get(\OCP\Security\ICrypto::class)));

lib/private/AllConfig.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu
312312
/**
313313
* Getting a user defined value
314314
*
315-
* @param string $userId the userId of the user that we want to store the value under
315+
* @param ?string $userId the userId of the user that we want to store the value under
316316
* @param string $appName the appName that we stored the value under
317317
* @param string $key the key under which the value is being stored
318318
* @param mixed $default the default value to be returned if the value isn't set
319319
* @return string
320320
*/
321321
public function getUserValue($userId, $appName, $key, $default = '') {
322-
$data = $this->getUserValues($userId);
322+
$data = $this->getAllUserValues($userId);
323323
if (isset($data[$appName][$key])) {
324324
return $data[$appName][$key];
325325
} else {
@@ -335,7 +335,7 @@ public function getUserValue($userId, $appName, $key, $default = '') {
335335
* @return string[]
336336
*/
337337
public function getUserKeys($userId, $appName) {
338-
$data = $this->getUserValues($userId);
338+
$data = $this->getAllUserValues($userId);
339339
if (isset($data[$appName])) {
340340
return array_keys($data[$appName]);
341341
} else {
@@ -400,19 +400,20 @@ public function deleteAppFromAllUsers($appName) {
400400
/**
401401
* Returns all user configs sorted by app of one user
402402
*
403-
* @param string $userId the user ID to get the app configs from
403+
* @param ?string $userId the user ID to get the app configs from
404+
* @psalm-return array<string, array<string, string>>
404405
* @return array[] - 2 dimensional array with the following structure:
405406
* [ $appId =>
406407
* [ $key => $value ]
407408
* ]
408409
*/
409-
private function getUserValues($userId) {
410+
public function getAllUserValues(?string $userId): array {
410411
if (isset($this->userCache[$userId])) {
411412
return $this->userCache[$userId];
412413
}
413414
if ($userId === null || $userId === '') {
414-
$this->userCache[$userId] = [];
415-
return $this->userCache[$userId];
415+
$this->userCache[''] = [];
416+
return $this->userCache[''];
416417
}
417418

418419
// TODO - FIXME

lib/public/IConfig.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function setUserValue($userId, $appName, $key, $value, $preCondition = nu
186186
/**
187187
* Shortcut for getting a user defined value
188188
*
189-
* @param string $userId the userId of the user that we want to store the value under
189+
* @param ?string $userId the userId of the user that we want to store the value under
190190
* @param string $appName the appName that we stored the value under
191191
* @param string $key the key under which the value is being stored
192192
* @param mixed $default the default value to be returned if the value isn't set
@@ -216,6 +216,19 @@ public function getUserValueForUsers($appName, $key, $userIds);
216216
*/
217217
public function getUserKeys($userId, $appName);
218218

219+
/**
220+
* Get all user configs sorted by app of one user
221+
*
222+
* @param string $userId the userId of the user that we want to get all values from
223+
* @psalm-return array<string, array<string, string>>
224+
* @return array[] - 2 dimensional array with the following structure:
225+
* [ $appId =>
226+
* [ $key => $value ]
227+
* ]
228+
* @since 24.0.0
229+
*/
230+
public function getAllUserValues(string $userId): array;
231+
219232
/**
220233
* Delete a user value
221234
*

0 commit comments

Comments
 (0)