mirror of
https://github.com/nextcloud/server.git
synced 2026-04-15 22:11:17 -04:00
feat: add command to get user objectstore config mappings
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
385dd36ff8
commit
2d4bba7b0c
5 changed files with 115 additions and 7 deletions
|
|
@ -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>
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@ return array(
|
|||
'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',
|
||||
|
|
|
|||
|
|
@ -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',
|
||||
|
|
|
|||
98
apps/files/lib/Command/Object/Multi/Users.php
Normal file
98
apps/files/lib/Command/Object/Multi/Users.php
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* SPDX-FileCopyrightText: 2025 Robin Appelman <robin@icewind.nl>
|
||||
* 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',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -61,13 +61,12 @@ class PrimaryObjectStoreConfig {
|
|||
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);
|
||||
|
|
@ -141,8 +140,8 @@ class PrimaryObjectStoreConfig {
|
|||
];
|
||||
}
|
||||
|
||||
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) {
|
||||
/*
|
||||
|
|
@ -161,4 +160,12 @@ class PrimaryObjectStoreConfig {
|
|||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue