Skip to content

Commit ef5e014

Browse files
committed
feat: add command to list mounts for user
Signed-off-by: Robin Appelman <[email protected]>
1 parent cfb7f8c commit ef5e014

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

apps/files/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
<command>OCA\Files\Command\Move</command>
4949
<command>OCA\Files\Command\SanitizeFilenames</command>
5050
<command>OCA\Files\Command\Mount\Refresh</command>
51+
<command>OCA\Files\Command\Mount\ListMounts</command>
5152
<command>OCA\Files\Command\Object\Delete</command>
5253
<command>OCA\Files\Command\Object\Get</command>
5354
<command>OCA\Files\Command\Object\Put</command>

apps/files/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'OCA\\Files\\Command\\Delete' => $baseDir . '/../lib/Command/Delete.php',
3434
'OCA\\Files\\Command\\DeleteOrphanedFiles' => $baseDir . '/../lib/Command/DeleteOrphanedFiles.php',
3535
'OCA\\Files\\Command\\Get' => $baseDir . '/../lib/Command/Get.php',
36+
'OCA\\Files\\Command\\Mount\\ListMounts' => $baseDir . '/../lib/Command/Mount/ListMounts.php',
3637
'OCA\\Files\\Command\\Mount\\Refresh' => $baseDir . '/../lib/Command/Mount/Refresh.php',
3738
'OCA\\Files\\Command\\Move' => $baseDir . '/../lib/Command/Move.php',
3839
'OCA\\Files\\Command\\Object\\Delete' => $baseDir . '/../lib/Command/Object/Delete.php',

apps/files/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class ComposerStaticInitFiles
4848
'OCA\\Files\\Command\\Delete' => __DIR__ . '/..' . '/../lib/Command/Delete.php',
4949
'OCA\\Files\\Command\\DeleteOrphanedFiles' => __DIR__ . '/..' . '/../lib/Command/DeleteOrphanedFiles.php',
5050
'OCA\\Files\\Command\\Get' => __DIR__ . '/..' . '/../lib/Command/Get.php',
51+
'OCA\\Files\\Command\\Mount\\ListMounts' => __DIR__ . '/..' . '/../lib/Command/Mount/ListMounts.php',
5152
'OCA\\Files\\Command\\Mount\\Refresh' => __DIR__ . '/..' . '/../lib/Command/Mount/Refresh.php',
5253
'OCA\\Files\\Command\\Move' => __DIR__ . '/..' . '/../lib/Command/Move.php',
5354
'OCA\\Files\\Command\\Object\\Delete' => __DIR__ . '/..' . '/../lib/Command/Object/Delete.php',
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Files\Command\Mount;
10+
11+
use OCP\Files\Config\ICachedMountInfo;
12+
use OCP\Files\Config\IMountProviderCollection;
13+
use OCP\Files\Config\IUserMountCache;
14+
use OCP\Files\Mount\IMountPoint;
15+
use OCP\IUserManager;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Input\InputArgument;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
class ListMounts extends Command {
22+
public function __construct(
23+
private readonly IUserManager $userManager,
24+
private readonly IUserMountCache $userMountCache,
25+
private readonly IMountProviderCollection $mountProviderCollection,
26+
) {
27+
parent::__construct();
28+
}
29+
30+
protected function configure(): void {
31+
$this
32+
->setName('files:mount:list')
33+
->setDescription('List of mounts for a user')
34+
->addArgument('user', InputArgument::REQUIRED, 'User to list mounts for');
35+
}
36+
37+
public function execute(InputInterface $input, OutputInterface $output): int {
38+
$userId = $input->getArgument('user');
39+
$user = $this->userManager->get($userId);
40+
if (!$user) {
41+
$output->writeln("<error>User $userId not found</error>");
42+
return 1;
43+
}
44+
45+
$mounts = $this->mountProviderCollection->getMountsForUser($user);
46+
$mounts[] = $this->mountProviderCollection->getHomeMountForUser($user);
47+
/** @var array<string, IMountPoint> $cachedByMountpoint */
48+
$mountsByMountpoint = array_combine(array_map(fn (IMountPoint $mount) => $mount->getMountPoint(), $mounts), $mounts);
49+
usort($mounts, fn (IMountPoint $a, IMountPoint $b) => $a->getMountPoint() <=> $b->getMountPoint());
50+
51+
$cachedMounts = $this->userMountCache->getMountsForUser($user);
52+
usort($cachedMounts, fn (ICachedMountInfo $a, ICachedMountInfo $b) => $a->getMountPoint() <=> $b->getMountPoint());
53+
/** @var array<string, ICachedMountInfo> $cachedByMountpoint */
54+
$cachedByMountpoint = array_combine(array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts), $cachedMounts);
55+
56+
foreach ($mounts as $mount) {
57+
$output->writeln('<info>' . $mount->getMountPoint() . '</info>: ' . $mount->getStorageId());
58+
if (isset($cachedByMountpoint[$mount->getMountPoint()])) {
59+
$cached = $cachedByMountpoint[$mount->getMountPoint()];
60+
$output->writeln("\t- provider: " . $cached->getMountProvider());
61+
$output->writeln("\t- storage id: " . $cached->getStorageId());
62+
$output->writeln("\t- root id: " . $cached->getRootId());
63+
} else {
64+
$output->writeln("\t<error>not registered</error>");
65+
}
66+
}
67+
foreach ($cachedMounts as $cachedMount) {
68+
if (!isset($mountsByMountpoint[$cachedMount->getMountPoint()])) {
69+
$output->writeln('<info>' . $cachedMount->getMountPoint() . '</info>:');
70+
$output->writeln("\t<error>registered but no longer provided</error>");
71+
$output->writeln("\t- provider: " . $cachedMount->getMountProvider());
72+
$output->writeln("\t- storage id: " . $cachedMount->getStorageId());
73+
$output->writeln("\t- root id: " . $cachedMount->getRootId());
74+
}
75+
}
76+
77+
return 0;
78+
}
79+
80+
}

0 commit comments

Comments
 (0)