RemoveInvalidShares.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2018 ownCloud GmbH
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\Command;
  9. use OCA\DAV\Connector\Sabre\Principal;
  10. use OCP\IDBConnection;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * Class RemoveInvalidShares - removes shared calendars and addressbook which
  16. * have no matching principal. Happened because of a bug in the calendar app.
  17. */
  18. class RemoveInvalidShares extends Command {
  19. public function __construct(
  20. private IDBConnection $connection,
  21. private Principal $principalBackend,
  22. ) {
  23. parent::__construct();
  24. }
  25. protected function configure(): void {
  26. $this
  27. ->setName('dav:remove-invalid-shares')
  28. ->setDescription('Remove invalid dav shares');
  29. }
  30. protected function execute(InputInterface $input, OutputInterface $output): int {
  31. $query = $this->connection->getQueryBuilder();
  32. $result = $query->selectDistinct('principaluri')
  33. ->from('dav_shares')
  34. ->executeQuery();
  35. while ($row = $result->fetch()) {
  36. $principaluri = $row['principaluri'];
  37. $p = $this->principalBackend->getPrincipalByPath($principaluri);
  38. if ($p === null) {
  39. $this->deleteSharesForPrincipal($principaluri);
  40. }
  41. }
  42. $result->closeCursor();
  43. return self::SUCCESS;
  44. }
  45. /**
  46. * @param string $principaluri
  47. */
  48. private function deleteSharesForPrincipal($principaluri): void {
  49. $delete = $this->connection->getQueryBuilder();
  50. $delete->delete('dav_shares')
  51. ->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri)));
  52. $delete->executeStatement();
  53. }
  54. }