FixShareOwners.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 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\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class FixShareOwners extends Base {
  14. public function __construct(
  15. private readonly OrphanHelper $orphanHelper,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function configure(): void {
  20. $this
  21. ->setName('sharing:fix-share-owners')
  22. ->setDescription('Fix owner of broken shares after transfer ownership on old versions')
  23. ->addOption(
  24. 'dry-run',
  25. null,
  26. InputOption::VALUE_NONE,
  27. 'only show which shares would be updated'
  28. );
  29. }
  30. public function execute(InputInterface $input, OutputInterface $output): int {
  31. $shares = $this->orphanHelper->getAllShares();
  32. $dryRun = $input->getOption('dry-run');
  33. $count = 0;
  34. foreach ($shares as $share) {
  35. if ($this->orphanHelper->isShareValid($share['owner'], $share['fileid']) || !$this->orphanHelper->fileExists($share['fileid'])) {
  36. continue;
  37. }
  38. $owner = $this->orphanHelper->findOwner($share['fileid']);
  39. if ($owner !== null) {
  40. if ($dryRun) {
  41. $output->writeln("Share with id <info>{$share['id']}</info> (target: <info>{$share['target']}</info>) can be updated to owner <info>$owner</info>");
  42. } else {
  43. $this->orphanHelper->updateShareOwner($share['id'], $owner);
  44. $output->writeln("Share with id <info>{$share['id']}</info> (target: <info>{$share['target']}</info>) updated to owner <info>$owner</info>");
  45. }
  46. $count++;
  47. }
  48. }
  49. if ($count === 0) {
  50. $output->writeln('No broken shares detected');
  51. }
  52. return static::SUCCESS;
  53. }
  54. }