Commit 04e3da0c authored by Vincent Petry's avatar Vincent Petry Committed by GitHub
Browse files

Merge pull request #25171 from owncloud/files_external-list-all

Add option to `occ files_external:list` to show all configured mounts
parents dc78d26f bac1a3a6
...@@ -41,6 +41,11 @@ class Export extends ListCommand { ...@@ -41,6 +41,11 @@ class Export extends ListCommand {
'user_id', 'user_id',
InputArgument::OPTIONAL, InputArgument::OPTIONAL,
'user id to export the personal mounts for, if no user is provided admin mounts will be exported' 'user id to export the personal mounts for, if no user is provided admin mounts will be exported'
)->addOption(
'all',
'a',
InputOption::VALUE_NONE,
'show both system wide mounts and all personal mounts'
); );
} }
...@@ -48,6 +53,7 @@ class Export extends ListCommand { ...@@ -48,6 +53,7 @@ class Export extends ListCommand {
$listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager);
$listInput = new ArrayInput([], $listCommand->getDefinition()); $listInput = new ArrayInput([], $listCommand->getDefinition());
$listInput->setArgument('user_id', $input->getArgument('user_id')); $listInput->setArgument('user_id', $input->getArgument('user_id'));
$listInput->setOption('all', $input->getOption('all'));
$listInput->setOption('output', 'json_pretty'); $listInput->setOption('output', 'json_pretty');
$listInput->setOption('show-password', true); $listInput->setOption('show-password', true);
$listInput->setOption('full', true); $listInput->setOption('full', true);
......
...@@ -56,6 +56,8 @@ class ListCommand extends Base { ...@@ -56,6 +56,8 @@ class ListCommand extends Base {
*/ */
protected $userManager; protected $userManager;
const ALL = -1;
function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) { function __construct(GlobalStoragesService $globalService, UserStoragesService $userService, IUserSession $userSession, IUserManager $userManager) {
parent::__construct(); parent::__construct();
$this->globalService = $globalService; $this->globalService = $globalService;
...@@ -67,7 +69,7 @@ class ListCommand extends Base { ...@@ -67,7 +69,7 @@ class ListCommand extends Base {
protected function configure() { protected function configure() {
$this $this
->setName('files_external:list') ->setName('files_external:list')
->setDescription('List configured mounts') ->setDescription('List configured admin or personal mounts')
->addArgument( ->addArgument(
'user_id', 'user_id',
InputArgument::OPTIONAL, InputArgument::OPTIONAL,
...@@ -82,16 +84,27 @@ class ListCommand extends Base { ...@@ -82,16 +84,27 @@ class ListCommand extends Base {
null, null,
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
'don\'t truncate long values in table output' 'don\'t truncate long values in table output'
)->addOption(
'all',
'a',
InputOption::VALUE_NONE,
'show both system wide mounts and all personal mounts'
); );
parent::configure(); parent::configure();
} }
protected function execute(InputInterface $input, OutputInterface $output) { protected function execute(InputInterface $input, OutputInterface $output) {
$userId = $input->getArgument('user_id'); if ($input->getOption('all')) {
$storageService = $this->getStorageService($userId); /** @var $mounts StorageConfig[] */
$mounts = $this->globalService->getStorageForAllUsers();
$userId = self::ALL;
} else {
$userId = $input->getArgument('user_id');
$storageService = $this->getStorageService($userId);
/** @var $mounts StorageConfig[] */ /** @var $mounts StorageConfig[] */
$mounts = $storageService->getAllStorages(); $mounts = $storageService->getAllStorages();
}
$this->listMounts($userId, $mounts, $input, $output); $this->listMounts($userId, $mounts, $input, $output);
} }
...@@ -102,13 +115,15 @@ class ListCommand extends Base { ...@@ -102,13 +115,15 @@ class ListCommand extends Base {
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
*/ */
public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output){ public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output) {
$outputType = $input->getOption('output'); $outputType = $input->getOption('output');
if (count($mounts) === 0) { if (count($mounts) === 0) {
if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) {
$output->writeln('[]'); $output->writeln('[]');
} else { } else {
if ($userId) { if ($userId === self::ALL) {
$output->writeln("<info>No mounts configured</info>");
} else if ($userId) {
$output->writeln("<info>No mounts configured by $userId</info>"); $output->writeln("<info>No mounts configured by $userId</info>");
} else { } else {
$output->writeln("<info>No admin mounts configured</info>"); $output->writeln("<info>No admin mounts configured</info>");
...@@ -119,10 +134,13 @@ class ListCommand extends Base { ...@@ -119,10 +134,13 @@ class ListCommand extends Base {
$headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options'];
if (!$userId) { if (!$userId || $userId === self::ALL) {
$headers[] = 'Applicable Users'; $headers[] = 'Applicable Users';
$headers[] = 'Applicable Groups'; $headers[] = 'Applicable Groups';
} }
if ($userId === self::ALL) {
$headers[] = 'Type';
}
if (!$input->getOption('show-password')) { if (!$input->getOption('show-password')) {
$hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key'];
...@@ -150,10 +168,13 @@ class ListCommand extends Base { ...@@ -150,10 +168,13 @@ class ListCommand extends Base {
$config->getBackendOptions(), $config->getBackendOptions(),
$config->getMountOptions() $config->getMountOptions()
]; ];
if (!$userId) { if (!$userId || $userId === self::ALL) {
$values[] = $config->getApplicableUsers(); $values[] = $config->getApplicableUsers();
$values[] = $config->getApplicableGroups(); $values[] = $config->getApplicableGroups();
} }
if ($userId === self::ALL) {
$values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal';
}
return array_combine($keys, $values); return array_combine($keys, $values);
}, $mounts); }, $mounts);
...@@ -215,7 +236,7 @@ class ListCommand extends Base { ...@@ -215,7 +236,7 @@ class ListCommand extends Base {
$optionsString $optionsString
]; ];
if (!$userId) { if (!$userId || $userId === self::ALL) {
$applicableUsers = implode(', ', $config->getApplicableUsers()); $applicableUsers = implode(', ', $config->getApplicableUsers());
$applicableGroups = implode(', ', $config->getApplicableGroups()); $applicableGroups = implode(', ', $config->getApplicableGroups());
if ($applicableUsers === '' && $applicableGroups === '') { if ($applicableUsers === '' && $applicableGroups === '') {
...@@ -224,6 +245,9 @@ class ListCommand extends Base { ...@@ -224,6 +245,9 @@ class ListCommand extends Base {
$values[] = $applicableUsers; $values[] = $applicableUsers;
$values[] = $applicableGroups; $values[] = $applicableGroups;
} }
if ($userId === self::ALL) {
$values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal';
}
return $values; return $values;
}, $mounts); }, $mounts);
......
...@@ -76,6 +76,18 @@ class DBConfigService { ...@@ -76,6 +76,18 @@ class DBConfigService {
} }
} }
/**
* Get all configured mounts
*
* @return array
*/
public function getAllMounts() {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type'])
->from('external_mounts');
return $this->getMountsFromQuery($query);
}
/** /**
* Get admin defined mounts * Get admin defined mounts
* *
......
...@@ -162,4 +162,23 @@ class GlobalStoragesService extends StoragesService { ...@@ -162,4 +162,23 @@ class GlobalStoragesService extends StoragesService {
protected function isApplicable(StorageConfig $config) { protected function isApplicable(StorageConfig $config) {
return true; return true;
} }
/**
* Get all configured admin and personal mounts
*
* @return array map of storage id to storage config
*/
public function getStorageForAllUsers() {
$mounts = $this->dbConfig->getAllMounts();
$configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts);
$configs = array_filter($configs, function ($config) {
return $config instanceof StorageConfig;
});
$keys = array_map(function (StorageConfig $config) {
return $config->getId();
}, $configs);
return array_combine($keys, $configs);
}
} }
...@@ -282,4 +282,14 @@ class DBConfigServiceTest extends TestCase { ...@@ -282,4 +282,14 @@ class DBConfigServiceTest extends TestCase {
$this->assertCount(1, $mounts); $this->assertCount(1, $mounts);
$this->assertEquals($id1, $mounts[0]['mount_id']); $this->assertEquals($id1, $mounts[0]['mount_id']);
} }
public function testGetAllMounts() {
$id1 = $this->addMount('/test', 'foo', 'bar', 100, DBConfigService::MOUNT_TYPE_ADMIN);
$id2 = $this->addMount('/test2', 'foo2', 'bar2', 100, DBConfigService::MOUNT_TYPE_PERSONAl);
$mounts = $this->dbConfig->getAllMounts();
$this->assertCount(2, $mounts);
$this->assertEquals($id1, $mounts[0]['mount_id']);
$this->assertEquals($id2, $mounts[1]['mount_id']);
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment