RemoveInvalidShares.php 2.5 KB

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