1
0

DeletedCalendarObjectsCollection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\Trashbin;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use Sabre\CalDAV\ICalendarObjectContainer;
  10. use Sabre\DAV\Exception\BadRequest;
  11. use Sabre\DAV\Exception\Forbidden;
  12. use Sabre\DAV\Exception\NotFound;
  13. use Sabre\DAV\Exception\NotImplemented;
  14. use Sabre\DAVACL\ACLTrait;
  15. use Sabre\DAVACL\IACL;
  16. use function array_map;
  17. use function implode;
  18. use function preg_match;
  19. class DeletedCalendarObjectsCollection implements ICalendarObjectContainer, IACL {
  20. use ACLTrait;
  21. public const NAME = 'objects';
  22. public function __construct(
  23. protected CalDavBackend $caldavBackend,
  24. /** @var mixed[] */
  25. private array $principalInfo,
  26. ) {
  27. }
  28. /**
  29. * @see \OCA\DAV\CalDAV\Trashbin\DeletedCalendarObjectsCollection::calendarQuery
  30. */
  31. public function getChildren() {
  32. throw new NotImplemented();
  33. }
  34. public function getChild($name) {
  35. if (!preg_match("/(\d+)\\.ics/", $name, $matches)) {
  36. throw new NotFound();
  37. }
  38. $data = $this->caldavBackend->getCalendarObjectById(
  39. $this->principalInfo['uri'],
  40. (int)$matches[1],
  41. );
  42. // If the object hasn't been deleted yet then we don't want to find it here
  43. if ($data === null) {
  44. throw new NotFound();
  45. }
  46. if (!isset($data['deleted_at'])) {
  47. throw new BadRequest('The calendar object you\'re trying to restore is not marked as deleted');
  48. }
  49. return new DeletedCalendarObject(
  50. $this->getRelativeObjectPath($data),
  51. $data,
  52. $this->principalInfo['uri'],
  53. $this->caldavBackend
  54. );
  55. }
  56. public function createFile($name, $data = null) {
  57. throw new Forbidden();
  58. }
  59. public function createDirectory($name) {
  60. throw new Forbidden();
  61. }
  62. public function childExists($name) {
  63. try {
  64. $this->getChild($name);
  65. } catch (NotFound $e) {
  66. return false;
  67. }
  68. return true;
  69. }
  70. public function delete() {
  71. throw new Forbidden();
  72. }
  73. public function getName(): string {
  74. return self::NAME;
  75. }
  76. public function setName($name) {
  77. throw new Forbidden();
  78. }
  79. public function getLastModified(): int {
  80. return 0;
  81. }
  82. public function calendarQuery(array $filters) {
  83. return array_map(function (array $calendarObjectInfo) {
  84. return $this->getRelativeObjectPath($calendarObjectInfo);
  85. }, $this->caldavBackend->getDeletedCalendarObjectsByPrincipal($this->principalInfo['uri']));
  86. }
  87. private function getRelativeObjectPath(array $calendarInfo): string {
  88. return implode(
  89. '.',
  90. [$calendarInfo['id'], 'ics'],
  91. );
  92. }
  93. public function getOwner() {
  94. return $this->principalInfo['uri'];
  95. }
  96. public function getACL(): array {
  97. return [
  98. [
  99. 'privilege' => '{DAV:}read',
  100. 'principal' => $this->getOwner(),
  101. 'protected' => true,
  102. ],
  103. [
  104. 'privilege' => '{DAV:}unbind',
  105. 'principal' => '{DAV:}owner',
  106. 'protected' => true,
  107. ]
  108. ];
  109. }
  110. }