Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add command to get user objectstore config mappings
Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Aug 14, 2025
commit 2d4bba7b0c64bb20a27319bceb0bc7765eda8490
1 change: 1 addition & 0 deletions apps/files/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<command>OCA\Files\Command\Object\Info</command>
<command>OCA\Files\Command\Object\ListObject</command>
<command>OCA\Files\Command\Object\Orphans</command>
<command>OCA\Files\Command\Object\Multi\Users</command>
<command>OCA\Files\Command\WindowsCompatibleFilenames</command>
</commands>

Expand Down
1 change: 1 addition & 0 deletions apps/files/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'OCA\\Files\\Command\\Object\\Get' => $baseDir . '/../lib/Command/Object/Get.php',
'OCA\\Files\\Command\\Object\\Info' => $baseDir . '/../lib/Command/Object/Info.php',
'OCA\\Files\\Command\\Object\\ListObject' => $baseDir . '/../lib/Command/Object/ListObject.php',
'OCA\\Files\\Command\\Object\\Multi\\Users' => $baseDir . '/../lib/Command/Object/Multi/Users.php',
'OCA\\Files\\Command\\Object\\ObjectUtil' => $baseDir . '/../lib/Command/Object/ObjectUtil.php',
'OCA\\Files\\Command\\Object\\Orphans' => $baseDir . '/../lib/Command/Object/Orphans.php',
'OCA\\Files\\Command\\Object\\Put' => $baseDir . '/../lib/Command/Object/Put.php',
Expand Down
1 change: 1 addition & 0 deletions apps/files/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class ComposerStaticInitFiles
'OCA\\Files\\Command\\Object\\Get' => __DIR__ . '/..' . '/../lib/Command/Object/Get.php',
'OCA\\Files\\Command\\Object\\Info' => __DIR__ . '/..' . '/../lib/Command/Object/Info.php',
'OCA\\Files\\Command\\Object\\ListObject' => __DIR__ . '/..' . '/../lib/Command/Object/ListObject.php',
'OCA\\Files\\Command\\Object\\Multi\\Users' => __DIR__ . '/..' . '/../lib/Command/Object/Multi/Users.php',
'OCA\\Files\\Command\\Object\\ObjectUtil' => __DIR__ . '/..' . '/../lib/Command/Object/ObjectUtil.php',
'OCA\\Files\\Command\\Object\\Orphans' => __DIR__ . '/..' . '/../lib/Command/Object/Orphans.php',
'OCA\\Files\\Command\\Object\\Put' => __DIR__ . '/..' . '/../lib/Command/Object/Put.php',
Expand Down
98 changes: 98 additions & 0 deletions apps/files/lib/Command/Object/Multi/Users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Robin Appelman <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Files\Command\Object\Multi;

use OC\Core\Command\Base;
use OC\Files\ObjectStore\PrimaryObjectStoreConfig;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Users extends Base {
public function __construct(
private readonly IUserManager $userManager,
private readonly PrimaryObjectStoreConfig $objectStoreConfig,
private readonly IConfig $config,
) {
parent::__construct();
}

protected function configure(): void {
parent::configure();
$this
->setName('files:object:multi:users')
->setDescription('Get the mapping between users and object store buckets')
->addOption('bucket', 'b', InputOption::VALUE_REQUIRED, 'Only list users using the specified bucket')
->addOption('object-store', 'o', InputOption::VALUE_REQUIRED, 'Only list users using the specified object store configuration')
->addOption('user', 'u', InputOption::VALUE_REQUIRED, 'Only show the mapping for the specified user, ignores all other options');
}

public function execute(InputInterface $input, OutputInterface $output): int {
if ($userId = $input->getOption('user')) {
$user = $this->userManager->get($userId);
if (!$user) {
$output->writeln("<error>User $userId not found</error>");
return 1;
}
$users = new \ArrayIterator([$user]);
} else {
$bucket = (string)$input->getOption('bucket');
$objectStore = (string)$input->getOption('object-store');
if ($bucket !== '' && $objectStore === '') {
$users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket));
} elseif ($bucket === '' && $objectStore !== '') {
$users = $this->getUsers($this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore));
} elseif ($bucket) {
$users = $this->getUsers(array_intersect(
$this->config->getUsersForUserValue('homeobjectstore', 'bucket', $bucket),
$this->config->getUsersForUserValue('homeobjectstore', 'objectstore', $objectStore)
));
} else {
$users = $this->userManager->getSeenUsers();
}
}

$this->writeStreamingTableInOutputFormat($input, $output, $this->infoForUsers($users), 100);
return 0;
}

/**
* @param string[] $userIds
* @return \Iterator<IUser>
*/
private function getUsers(array $userIds): \Iterator {
foreach ($userIds as $userId) {
$user = $this->userManager->get($userId);
if ($user) {
yield $user;
}
}
}

/**
* @param \Iterator<IUser> $users
* @return \Iterator<array>
*/
private function infoForUsers(\Iterator $users): \Iterator {
foreach ($users as $user) {
yield $this->infoForUser($user);
}
}

private function infoForUser(IUser $user): array {
return [
'user' => $user->getUID(),
'object-store' => $this->objectStoreConfig->getObjectStoreForUser($user),
'bucket' => $this->objectStoreConfig->getSetBucketForUser($user) ?? 'unset',
];
}
}
21 changes: 14 additions & 7 deletions lib/private/Files/ObjectStore/PrimaryObjectStoreConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ public function getObjectStoreConfigForUser(IUser $user): ?array {
return null;
}

$store = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'objectstore', null);
$store = $this->getObjectStoreForUser($user);

if ($store) {
$config = $configs[$store];
} else {
$config = $configs['default'];
if (!isset($configs[$store])) {
throw new \Exception("Object store configuration for '{$store}' not found");
}
$config = $configs[$store];

if ($config['arguments']['multibucket']) {
$config['arguments']['bucket'] = $this->getBucketForUser($user, $config);
Expand Down Expand Up @@ -141,8 +140,8 @@ private function validateObjectStoreConfig(array $config) {
];
}

private function getBucketForUser(IUser $user, array $config): string {
$bucket = $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
public function getBucketForUser(IUser $user, array $config): string {
$bucket = $this->getSetBucketForUser($user);

if ($bucket === null) {
/*
Expand All @@ -161,4 +160,12 @@ private function getBucketForUser(IUser $user, array $config): string {

return $bucket;
}

public function getSetBucketForUser(IUser $user): ?string {
return $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'bucket', null);
}

public function getObjectStoreForUser(IUser $user): string {
return $this->config->getUserValue($user->getUID(), 'homeobjectstore', 'objectstore', 'default');
}
}