DeletedCalendarObjectsCollection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /** @var CalDavBackend */
  23. protected $caldavBackend;
  24. /** @var mixed[] */
  25. private $principalInfo;
  26. public function __construct(CalDavBackend $caldavBackend,
  27. array $principalInfo) {
  28. $this->caldavBackend = $caldavBackend;
  29. $this->principalInfo = $principalInfo;
  30. }
  31. /**
  32. * @see \OCA\DAV\CalDAV\Trashbin\DeletedCalendarObjectsCollection::calendarQuery
  33. */
  34. public function getChildren() {
  35. throw new NotImplemented();
  36. }
  37. public function getChild($name) {
  38. if (!preg_match("/(\d+)\\.ics/", $name, $matches)) {
  39. throw new NotFound();
  40. }
  41. $data = $this->caldavBackend->getCalendarObjectById(
  42. $this->principalInfo['uri'],
  43. (int) $matches[1],
  44. );
  45. // If the object hasn't been deleted yet then we don't want to find it here
  46. if ($data === null) {
  47. throw new NotFound();
  48. }
  49. if (!isset($data['deleted_at'])) {
  50. throw new BadRequest('The calendar object you\'re trying to restore is not marked as deleted');
  51. }
  52. return new DeletedCalendarObject(
  53. $this->getRelativeObjectPath($data),
  54. $data,
  55. $this->principalInfo['uri'],
  56. $this->caldavBackend
  57. );
  58. }
  59. public function createFile($name, $data = null) {
  60. throw new Forbidden();
  61. }
  62. public function createDirectory($name) {
  63. throw new Forbidden();
  64. }
  65. public function childExists($name) {
  66. try {
  67. $this->getChild($name);
  68. } catch (NotFound $e) {
  69. return false;
  70. }
  71. return true;
  72. }
  73. public function delete() {
  74. throw new Forbidden();
  75. }
  76. public function getName(): string {
  77. return self::NAME;
  78. }
  79. public function setName($name) {
  80. throw new Forbidden();
  81. }
  82. public function getLastModified(): int {
  83. return 0;
  84. }
  85. public function calendarQuery(array $filters) {
  86. return array_map(function (array $calendarObjectInfo) {
  87. return $this->getRelativeObjectPath($calendarObjectInfo);
  88. }, $this->caldavBackend->getDeletedCalendarObjectsByPrincipal($this->principalInfo['uri']));
  89. }
  90. private function getRelativeObjectPath(array $calendarInfo): string {
  91. return implode(
  92. '.',
  93. [$calendarInfo['id'], 'ics'],
  94. );
  95. }
  96. public function getOwner() {
  97. return $this->principalInfo['uri'];
  98. }
  99. public function getACL(): array {
  100. return [
  101. [
  102. 'privilege' => '{DAV:}read',
  103. 'principal' => $this->getOwner(),
  104. 'protected' => true,
  105. ],
  106. [
  107. 'privilege' => '{DAV:}unbind',
  108. 'principal' => '{DAV:}owner',
  109. 'protected' => true,
  110. ]
  111. ];
  112. }
  113. }