@@ -15,88 +15,88 @@ |
||
| 15 | 15 | use OCP\IDBConnection; |
| 16 | 16 | |
| 17 | 17 | class OrphanHelper { |
| 18 | - public function __construct( |
|
| 19 | - private IDBConnection $connection, |
|
| 20 | - private IRootFolder $rootFolder, |
|
| 21 | - private IUserMountCache $userMountCache, |
|
| 22 | - ) { |
|
| 23 | - } |
|
| 18 | + public function __construct( |
|
| 19 | + private IDBConnection $connection, |
|
| 20 | + private IRootFolder $rootFolder, |
|
| 21 | + private IUserMountCache $userMountCache, |
|
| 22 | + ) { |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - public function isShareValid(string $owner, int $fileId): bool { |
|
| 26 | - try { |
|
| 27 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 28 | - } catch (NoUserException $e) { |
|
| 29 | - return false; |
|
| 30 | - } |
|
| 31 | - $node = $userFolder->getFirstNodeById($fileId); |
|
| 32 | - return $node !== null; |
|
| 33 | - } |
|
| 25 | + public function isShareValid(string $owner, int $fileId): bool { |
|
| 26 | + try { |
|
| 27 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 28 | + } catch (NoUserException $e) { |
|
| 29 | + return false; |
|
| 30 | + } |
|
| 31 | + $node = $userFolder->getFirstNodeById($fileId); |
|
| 32 | + return $node !== null; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * @param int[] $ids |
|
| 37 | - * @return void |
|
| 38 | - */ |
|
| 39 | - public function deleteShares(array $ids): void { |
|
| 40 | - $query = $this->connection->getQueryBuilder(); |
|
| 41 | - $query->delete('share') |
|
| 42 | - ->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 43 | - $query->executeStatement(); |
|
| 44 | - } |
|
| 35 | + /** |
|
| 36 | + * @param int[] $ids |
|
| 37 | + * @return void |
|
| 38 | + */ |
|
| 39 | + public function deleteShares(array $ids): void { |
|
| 40 | + $query = $this->connection->getQueryBuilder(); |
|
| 41 | + $query->delete('share') |
|
| 42 | + ->where($query->expr()->in('id', $query->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); |
|
| 43 | + $query->executeStatement(); |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function fileExists(int $fileId): bool { |
|
| 47 | - $query = $this->connection->getQueryBuilder(); |
|
| 48 | - $query->select('fileid') |
|
| 49 | - ->from('filecache') |
|
| 50 | - ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 51 | - return $query->executeQuery()->fetchOne() !== false; |
|
| 52 | - } |
|
| 46 | + public function fileExists(int $fileId): bool { |
|
| 47 | + $query = $this->connection->getQueryBuilder(); |
|
| 48 | + $query->select('fileid') |
|
| 49 | + ->from('filecache') |
|
| 50 | + ->where($query->expr()->eq('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 51 | + return $query->executeQuery()->fetchOne() !== false; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return \Traversable<int, array{id: int, owner: string, fileid: int, target: string}> |
|
| 56 | - */ |
|
| 57 | - public function getAllShares(?string $owner = null, ?string $with = null) { |
|
| 58 | - $query = $this->connection->getQueryBuilder(); |
|
| 59 | - $query->select('id', 'file_source', 'uid_owner', 'file_target') |
|
| 60 | - ->from('share') |
|
| 61 | - ->where($query->expr()->in('item_type', $query->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 54 | + /** |
|
| 55 | + * @return \Traversable<int, array{id: int, owner: string, fileid: int, target: string}> |
|
| 56 | + */ |
|
| 57 | + public function getAllShares(?string $owner = null, ?string $with = null) { |
|
| 58 | + $query = $this->connection->getQueryBuilder(); |
|
| 59 | + $query->select('id', 'file_source', 'uid_owner', 'file_target') |
|
| 60 | + ->from('share') |
|
| 61 | + ->where($query->expr()->in('item_type', $query->createNamedParameter(['file', 'folder'], IQueryBuilder::PARAM_STR_ARRAY))); |
|
| 62 | 62 | |
| 63 | - if ($owner !== null) { |
|
| 64 | - $query->andWhere($query->expr()->eq('uid_owner', $query->createNamedParameter($owner))); |
|
| 65 | - } |
|
| 66 | - if ($with !== null) { |
|
| 67 | - $query->andWhere($query->expr()->eq('share_with', $query->createNamedParameter($with))); |
|
| 68 | - } |
|
| 63 | + if ($owner !== null) { |
|
| 64 | + $query->andWhere($query->expr()->eq('uid_owner', $query->createNamedParameter($owner))); |
|
| 65 | + } |
|
| 66 | + if ($with !== null) { |
|
| 67 | + $query->andWhere($query->expr()->eq('share_with', $query->createNamedParameter($with))); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - $result = $query->executeQuery(); |
|
| 71 | - while ($row = $result->fetch()) { |
|
| 72 | - yield [ |
|
| 73 | - 'id' => (int)$row['id'], |
|
| 74 | - 'owner' => (string)$row['uid_owner'], |
|
| 75 | - 'fileid' => (int)$row['file_source'], |
|
| 76 | - 'target' => (string)$row['file_target'], |
|
| 77 | - ]; |
|
| 78 | - } |
|
| 79 | - } |
|
| 70 | + $result = $query->executeQuery(); |
|
| 71 | + while ($row = $result->fetch()) { |
|
| 72 | + yield [ |
|
| 73 | + 'id' => (int)$row['id'], |
|
| 74 | + 'owner' => (string)$row['uid_owner'], |
|
| 75 | + 'fileid' => (int)$row['file_source'], |
|
| 76 | + 'target' => (string)$row['file_target'], |
|
| 77 | + ]; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - public function findOwner(int $fileId): ?string { |
|
| 82 | - $mounts = $this->userMountCache->getMountsForFileId($fileId); |
|
| 83 | - if (!$mounts) { |
|
| 84 | - return null; |
|
| 85 | - } |
|
| 86 | - foreach ($mounts as $mount) { |
|
| 87 | - $userHomeMountPoint = '/' . $mount->getUser()->getUID() . '/'; |
|
| 88 | - if ($mount->getMountPoint() === $userHomeMountPoint) { |
|
| 89 | - return $mount->getUser()->getUID(); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - return null; |
|
| 93 | - } |
|
| 81 | + public function findOwner(int $fileId): ?string { |
|
| 82 | + $mounts = $this->userMountCache->getMountsForFileId($fileId); |
|
| 83 | + if (!$mounts) { |
|
| 84 | + return null; |
|
| 85 | + } |
|
| 86 | + foreach ($mounts as $mount) { |
|
| 87 | + $userHomeMountPoint = '/' . $mount->getUser()->getUID() . '/'; |
|
| 88 | + if ($mount->getMountPoint() === $userHomeMountPoint) { |
|
| 89 | + return $mount->getUser()->getUID(); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + return null; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - public function updateShareOwner(int $shareId, string $owner): void { |
|
| 96 | - $query = $this->connection->getQueryBuilder(); |
|
| 97 | - $query->update('share') |
|
| 98 | - ->set('uid_owner', $query->createNamedParameter($owner)) |
|
| 99 | - ->where($query->expr()->eq('id', $query->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))); |
|
| 100 | - $query->executeStatement(); |
|
| 101 | - } |
|
| 95 | + public function updateShareOwner(int $shareId, string $owner): void { |
|
| 96 | + $query = $this->connection->getQueryBuilder(); |
|
| 97 | + $query->update('share') |
|
| 98 | + ->set('uid_owner', $query->createNamedParameter($owner)) |
|
| 99 | + ->where($query->expr()->eq('id', $query->createNamedParameter($shareId, IQueryBuilder::PARAM_INT))); |
|
| 100 | + $query->executeStatement(); |
|
| 101 | + } |
|
| 102 | 102 | } |
@@ -17,67 +17,67 @@ |
||
| 17 | 17 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
| 18 | 18 | |
| 19 | 19 | class DeleteOrphanShares extends Base { |
| 20 | - public function __construct( |
|
| 21 | - private OrphanHelper $orphanHelper, |
|
| 22 | - ) { |
|
| 23 | - parent::__construct(); |
|
| 24 | - } |
|
| 20 | + public function __construct( |
|
| 21 | + private OrphanHelper $orphanHelper, |
|
| 22 | + ) { |
|
| 23 | + parent::__construct(); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - protected function configure(): void { |
|
| 27 | - $this |
|
| 28 | - ->setName('sharing:delete-orphan-shares') |
|
| 29 | - ->setDescription('Delete shares where the owner no longer has access to the file') |
|
| 30 | - ->addOption( |
|
| 31 | - 'force', |
|
| 32 | - 'f', |
|
| 33 | - InputOption::VALUE_NONE, |
|
| 34 | - 'delete the shares without asking' |
|
| 35 | - ) |
|
| 36 | - ->addOption('owner', null, InputOption::VALUE_REQUIRED, 'Only check shares owned by a specific user') |
|
| 37 | - ->addOption('with', null, InputOption::VALUE_REQUIRED, 'Only check shares with a specific user'); |
|
| 38 | - } |
|
| 26 | + protected function configure(): void { |
|
| 27 | + $this |
|
| 28 | + ->setName('sharing:delete-orphan-shares') |
|
| 29 | + ->setDescription('Delete shares where the owner no longer has access to the file') |
|
| 30 | + ->addOption( |
|
| 31 | + 'force', |
|
| 32 | + 'f', |
|
| 33 | + InputOption::VALUE_NONE, |
|
| 34 | + 'delete the shares without asking' |
|
| 35 | + ) |
|
| 36 | + ->addOption('owner', null, InputOption::VALUE_REQUIRED, 'Only check shares owned by a specific user') |
|
| 37 | + ->addOption('with', null, InputOption::VALUE_REQUIRED, 'Only check shares with a specific user'); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 41 | - $force = $input->getOption('force'); |
|
| 42 | - $owner = $input->getOption('owner') ?: null; |
|
| 43 | - $with = $input->getOption('with') ?: null; |
|
| 44 | - $shares = $this->orphanHelper->getAllShares($owner, $with); |
|
| 40 | + public function execute(InputInterface $input, OutputInterface $output): int { |
|
| 41 | + $force = $input->getOption('force'); |
|
| 42 | + $owner = $input->getOption('owner') ?: null; |
|
| 43 | + $with = $input->getOption('with') ?: null; |
|
| 44 | + $shares = $this->orphanHelper->getAllShares($owner, $with); |
|
| 45 | 45 | |
| 46 | - $orphans = []; |
|
| 47 | - foreach ($shares as $share) { |
|
| 48 | - if (!$this->orphanHelper->isShareValid($share['owner'], $share['fileid'])) { |
|
| 49 | - $orphans[] = $share['id']; |
|
| 50 | - $exists = $this->orphanHelper->fileExists($share['fileid']); |
|
| 51 | - $output->writeln("<info>{$share['target']}</info> owned by <info>{$share['owner']}</info>"); |
|
| 52 | - if ($exists) { |
|
| 53 | - $output->writeln(" file still exists but the share owner lost access to it, run <info>occ info:file {$share['fileid']}</info> for more information about the file"); |
|
| 54 | - } else { |
|
| 55 | - $output->writeln(' file no longer exists'); |
|
| 56 | - } |
|
| 57 | - } |
|
| 58 | - } |
|
| 46 | + $orphans = []; |
|
| 47 | + foreach ($shares as $share) { |
|
| 48 | + if (!$this->orphanHelper->isShareValid($share['owner'], $share['fileid'])) { |
|
| 49 | + $orphans[] = $share['id']; |
|
| 50 | + $exists = $this->orphanHelper->fileExists($share['fileid']); |
|
| 51 | + $output->writeln("<info>{$share['target']}</info> owned by <info>{$share['owner']}</info>"); |
|
| 52 | + if ($exists) { |
|
| 53 | + $output->writeln(" file still exists but the share owner lost access to it, run <info>occ info:file {$share['fileid']}</info> for more information about the file"); |
|
| 54 | + } else { |
|
| 55 | + $output->writeln(' file no longer exists'); |
|
| 56 | + } |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - $count = count($orphans); |
|
| 60 | + $count = count($orphans); |
|
| 61 | 61 | |
| 62 | - if ($count === 0) { |
|
| 63 | - $output->writeln('No orphan shares detected'); |
|
| 64 | - return 0; |
|
| 65 | - } |
|
| 62 | + if ($count === 0) { |
|
| 63 | + $output->writeln('No orphan shares detected'); |
|
| 64 | + return 0; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - if ($force) { |
|
| 68 | - $doDelete = true; |
|
| 69 | - } else { |
|
| 70 | - $output->writeln(''); |
|
| 71 | - /** @var QuestionHelper $helper */ |
|
| 72 | - $helper = $this->getHelper('question'); |
|
| 73 | - $question = new ConfirmationQuestion("Delete <info>$count</info> orphan shares? [y/N] ", false); |
|
| 74 | - $doDelete = $helper->ask($input, $output, $question); |
|
| 75 | - } |
|
| 67 | + if ($force) { |
|
| 68 | + $doDelete = true; |
|
| 69 | + } else { |
|
| 70 | + $output->writeln(''); |
|
| 71 | + /** @var QuestionHelper $helper */ |
|
| 72 | + $helper = $this->getHelper('question'); |
|
| 73 | + $question = new ConfirmationQuestion("Delete <info>$count</info> orphan shares? [y/N] ", false); |
|
| 74 | + $doDelete = $helper->ask($input, $output, $question); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - if ($doDelete) { |
|
| 78 | - $this->orphanHelper->deleteShares($orphans); |
|
| 79 | - } |
|
| 77 | + if ($doDelete) { |
|
| 78 | + $this->orphanHelper->deleteShares($orphans); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - return 0; |
|
| 82 | - } |
|
| 81 | + return 0; |
|
| 82 | + } |
|
| 83 | 83 | } |