DeleteOrphanShares.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files_Sharing\Command;
  23. use OC\Core\Command\Base;
  24. use OCA\Files_Sharing\OrphanHelper;
  25. use Symfony\Component\Console\Helper\QuestionHelper;
  26. use Symfony\Component\Console\Input\InputInterface;
  27. use Symfony\Component\Console\Input\InputOption;
  28. use Symfony\Component\Console\Output\OutputInterface;
  29. use Symfony\Component\Console\Question\ConfirmationQuestion;
  30. class DeleteOrphanShares extends Base {
  31. private OrphanHelper $orphanHelper;
  32. public function __construct(OrphanHelper $orphanHelper) {
  33. parent::__construct();
  34. $this->orphanHelper = $orphanHelper;
  35. }
  36. protected function configure(): void {
  37. $this
  38. ->setName('sharing:delete-orphan-shares')
  39. ->setDescription('Delete shares where the owner no longer has access to the file')
  40. ->addOption(
  41. 'force',
  42. 'f',
  43. InputOption::VALUE_NONE,
  44. 'delete the shares without asking'
  45. );
  46. }
  47. public function execute(InputInterface $input, OutputInterface $output): int {
  48. $force = $input->getOption('force');
  49. $shares = $this->orphanHelper->getAllShares();
  50. $orphans = [];
  51. foreach ($shares as $share) {
  52. if (!$this->orphanHelper->isShareValid($share['owner'], $share['fileid'])) {
  53. $orphans[] = $share['id'];
  54. $exists = $this->orphanHelper->fileExists($share['fileid']);
  55. $output->writeln("<info>{$share['target']}</info> owned by <info>{$share['owner']}</info>");
  56. if ($exists) {
  57. $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");
  58. } else {
  59. $output->writeln(" file no longer exists");
  60. }
  61. }
  62. }
  63. $count = count($orphans);
  64. if ($count === 0) {
  65. $output->writeln("No orphan shares detected");
  66. return 0;
  67. }
  68. if ($force) {
  69. $doDelete = true;
  70. } else {
  71. $output->writeln("");
  72. /** @var QuestionHelper $helper */
  73. $helper = $this->getHelper('question');
  74. $question = new ConfirmationQuestion("Delete <info>$count</info> orphan shares? [y/N] ", false);
  75. $doDelete = $helper->ask($input, $output, $question);
  76. }
  77. if ($doDelete) {
  78. $this->orphanHelper->deleteShares($orphans);
  79. }
  80. return 0;
  81. }
  82. }