DeleteOrphanShares.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_Sharing\OrphanHelper;
  10. use Symfony\Component\Console\Helper\QuestionHelper;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Question\ConfirmationQuestion;
  15. class DeleteOrphanShares extends Base {
  16. public function __construct(
  17. private OrphanHelper $orphanHelper,
  18. ) {
  19. parent::__construct();
  20. }
  21. protected function configure(): void {
  22. $this
  23. ->setName('sharing:delete-orphan-shares')
  24. ->setDescription('Delete shares where the owner no longer has access to the file')
  25. ->addOption(
  26. 'force',
  27. 'f',
  28. InputOption::VALUE_NONE,
  29. 'delete the shares without asking'
  30. );
  31. }
  32. public function execute(InputInterface $input, OutputInterface $output): int {
  33. $force = $input->getOption('force');
  34. $shares = $this->orphanHelper->getAllShares();
  35. $orphans = [];
  36. foreach ($shares as $share) {
  37. if (!$this->orphanHelper->isShareValid($share['owner'], $share['fileid'])) {
  38. $orphans[] = $share['id'];
  39. $exists = $this->orphanHelper->fileExists($share['fileid']);
  40. $output->writeln("<info>{$share['target']}</info> owned by <info>{$share['owner']}</info>");
  41. if ($exists) {
  42. $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");
  43. } else {
  44. $output->writeln(' file no longer exists');
  45. }
  46. }
  47. }
  48. $count = count($orphans);
  49. if ($count === 0) {
  50. $output->writeln('No orphan shares detected');
  51. return 0;
  52. }
  53. if ($force) {
  54. $doDelete = true;
  55. } else {
  56. $output->writeln('');
  57. /** @var QuestionHelper $helper */
  58. $helper = $this->getHelper('question');
  59. $question = new ConfirmationQuestion("Delete <info>$count</info> orphan shares? [y/N] ", false);
  60. $doDelete = $helper->ask($input, $output, $question);
  61. }
  62. if ($doDelete) {
  63. $this->orphanHelper->deleteShares($orphans);
  64. }
  65. return 0;
  66. }
  67. }