1
0

RemoveClassifiedEventActivity.php 4.8 KB

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