RemoveInvalidShares.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, ownCloud GmbH
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Command;
  26. use OCA\DAV\Connector\Sabre\Principal;
  27. use OCP\IDBConnection;
  28. use Symfony\Component\Console\Command\Command;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. /**
  32. * Class RemoveInvalidShares - removes shared calendars and addressbook which
  33. * have no matching principal. Happened because of a bug in the calendar app.
  34. */
  35. class RemoveInvalidShares extends Command {
  36. public function __construct(
  37. private IDBConnection $connection,
  38. private Principal $principalBackend,
  39. ) {
  40. parent::__construct();
  41. }
  42. protected function configure(): void {
  43. $this
  44. ->setName('dav:remove-invalid-shares')
  45. ->setDescription('Remove invalid dav shares');
  46. }
  47. protected function execute(InputInterface $input, OutputInterface $output): int {
  48. $query = $this->connection->getQueryBuilder();
  49. $result = $query->selectDistinct('principaluri')
  50. ->from('dav_shares')
  51. ->execute();
  52. while ($row = $result->fetch()) {
  53. $principaluri = $row['principaluri'];
  54. $p = $this->principalBackend->getPrincipalByPath($principaluri);
  55. if ($p === null) {
  56. $this->deleteSharesForPrincipal($principaluri);
  57. }
  58. }
  59. $result->closeCursor();
  60. return self::SUCCESS;
  61. }
  62. /**
  63. * @param string $principaluri
  64. */
  65. private function deleteSharesForPrincipal($principaluri): void {
  66. $delete = $this->connection->getQueryBuilder();
  67. $delete->delete('dav_shares')
  68. ->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri)));
  69. $delete->execute();
  70. }
  71. }