RemoveInvalidShares.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /** @var IDBConnection */
  37. private $connection;
  38. /** @var Principal */
  39. private $principalBackend;
  40. public function __construct(IDBConnection $connection,
  41. Principal $principalBackend) {
  42. parent::__construct();
  43. $this->connection = $connection;
  44. $this->principalBackend = $principalBackend;
  45. }
  46. protected function configure() {
  47. $this
  48. ->setName('dav:remove-invalid-shares')
  49. ->setDescription('Remove invalid dav shares');
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output): int {
  52. $query = $this->connection->getQueryBuilder();
  53. $result = $query->selectDistinct('principaluri')
  54. ->from('dav_shares')
  55. ->execute();
  56. while ($row = $result->fetch()) {
  57. $principaluri = $row['principaluri'];
  58. $p = $this->principalBackend->getPrincipalByPath($principaluri);
  59. if ($p === null) {
  60. $this->deleteSharesForPrincipal($principaluri);
  61. }
  62. }
  63. $result->closeCursor();
  64. return 0;
  65. }
  66. /**
  67. * @param string $principaluri
  68. */
  69. private function deleteSharesForPrincipal($principaluri) {
  70. $delete = $this->connection->getQueryBuilder();
  71. $delete->delete('dav_shares')
  72. ->where($delete->expr()->eq('principaluri', $delete->createNamedParameter($principaluri)));
  73. $delete->execute();
  74. }
  75. }