RemoveClassifiedEventActivity.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\DAV\Migration;
  23. use OCA\DAV\CalDAV\CalDavBackend;
  24. use OCP\IDBConnection;
  25. use OCP\Migration\IOutput;
  26. use OCP\Migration\IRepairStep;
  27. class RemoveClassifiedEventActivity implements IRepairStep {
  28. /** @var IDBConnection */
  29. private $connection;
  30. public function __construct(IDBConnection $connection) {
  31. $this->connection = $connection;
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function getName() {
  37. return 'Remove activity entries of private events';
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function run(IOutput $output) {
  43. if (!$this->connection->tableExists('activity')) {
  44. return;
  45. }
  46. $deletedEvents = $this->removePrivateEventActivity();
  47. $deletedEvents += $this->removeConfidentialUncensoredEventActivity();
  48. $output->info("Removed $deletedEvents activity entries");
  49. }
  50. protected function removePrivateEventActivity(): int {
  51. $deletedEvents = 0;
  52. $delete = $this->connection->getQueryBuilder();
  53. $delete->delete('activity')
  54. ->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
  55. ->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
  56. ->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
  57. ->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')));
  58. $query = $this->connection->getQueryBuilder();
  59. $query->select('c.principaluri', 'o.calendarid', 'o.uid')
  60. ->from('calendarobjects', 'o')
  61. ->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
  62. ->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_PRIVATE)));
  63. $result = $query->execute();
  64. while ($row = $result->fetch()) {
  65. if ($row['principaluri'] === null) {
  66. continue;
  67. }
  68. $delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
  69. ->setParameter('type', 'calendar')
  70. ->setParameter('calendar_id', $row['calendarid'])
  71. ->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%');
  72. $deletedEvents += $delete->execute();
  73. }
  74. $result->closeCursor();
  75. return $deletedEvents;
  76. }
  77. protected function removeConfidentialUncensoredEventActivity(): int {
  78. $deletedEvents = 0;
  79. $delete = $this->connection->getQueryBuilder();
  80. $delete->delete('activity')
  81. ->where($delete->expr()->neq('affecteduser', $delete->createParameter('owner')))
  82. ->andWhere($delete->expr()->eq('object_type', $delete->createParameter('type')))
  83. ->andWhere($delete->expr()->eq('object_id', $delete->createParameter('calendar_id')))
  84. ->andWhere($delete->expr()->like('subjectparams', $delete->createParameter('event_uid')))
  85. ->andWhere($delete->expr()->notLike('subjectparams', $delete->createParameter('filtered_name')));
  86. $query = $this->connection->getQueryBuilder();
  87. $query->select('c.principaluri', 'o.calendarid', 'o.uid')
  88. ->from('calendarobjects', 'o')
  89. ->leftJoin('o', 'calendars', 'c', $query->expr()->eq('c.id', 'o.calendarid'))
  90. ->where($query->expr()->eq('o.classification', $query->createNamedParameter(CalDavBackend::CLASSIFICATION_CONFIDENTIAL)));
  91. $result = $query->execute();
  92. while ($row = $result->fetch()) {
  93. if ($row['principaluri'] === null) {
  94. continue;
  95. }
  96. $delete->setParameter('owner', $this->getPrincipal($row['principaluri']))
  97. ->setParameter('type', 'calendar')
  98. ->setParameter('calendar_id', $row['calendarid'])
  99. ->setParameter('event_uid', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '"') . '%')
  100. ->setParameter('filtered_name', '%' . $this->connection->escapeLikeParameter('{"id":"' . $row['uid'] . '","name":"Busy"') . '%');
  101. $deletedEvents += $delete->execute();
  102. }
  103. $result->closeCursor();
  104. return $deletedEvents;
  105. }
  106. protected function getPrincipal(string $principalUri): string {
  107. $uri = explode('/', $principalUri);
  108. return array_pop($uri);
  109. }
  110. }