CachedSubscription.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
  9. use Sabre\DAV\Exception\MethodNotAllowed;
  10. use Sabre\DAV\Exception\NotFound;
  11. use Sabre\DAV\INode;
  12. use Sabre\DAV\PropPatch;
  13. /**
  14. * Class CachedSubscription
  15. *
  16. * @package OCA\DAV\CalDAV
  17. * @property CalDavBackend $caldavBackend
  18. */
  19. class CachedSubscription extends \Sabre\CalDAV\Calendar {
  20. /**
  21. * @return string
  22. */
  23. public function getPrincipalURI():string {
  24. return $this->calendarInfo['principaluri'];
  25. }
  26. /**
  27. * @return array
  28. */
  29. public function getACL() {
  30. return [
  31. [
  32. 'privilege' => '{DAV:}read',
  33. 'principal' => $this->getOwner(),
  34. 'protected' => true,
  35. ],
  36. [
  37. 'privilege' => '{DAV:}read',
  38. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  39. 'protected' => true,
  40. ],
  41. [
  42. 'privilege' => '{DAV:}read',
  43. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  44. 'protected' => true,
  45. ],
  46. [
  47. 'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
  48. 'principal' => '{DAV:}authenticated',
  49. 'protected' => true,
  50. ],
  51. [
  52. 'privilege' => '{DAV:}write-properties',
  53. 'principal' => $this->getOwner(),
  54. 'protected' => true,
  55. ]
  56. ];
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getChildACL() {
  62. return [
  63. [
  64. 'privilege' => '{DAV:}read',
  65. 'principal' => $this->getOwner(),
  66. 'protected' => true,
  67. ],
  68. [
  69. 'privilege' => '{DAV:}read',
  70. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  71. 'protected' => true,
  72. ],
  73. [
  74. 'privilege' => '{DAV:}read',
  75. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  76. 'protected' => true,
  77. ],
  78. ];
  79. }
  80. /**
  81. * @return null|string
  82. */
  83. public function getOwner() {
  84. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  85. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  86. }
  87. return parent::getOwner();
  88. }
  89. public function delete() {
  90. $this->caldavBackend->deleteSubscription($this->calendarInfo['id']);
  91. }
  92. /**
  93. * @param PropPatch $propPatch
  94. */
  95. public function propPatch(PropPatch $propPatch) {
  96. $this->caldavBackend->updateSubscription($this->calendarInfo['id'], $propPatch);
  97. }
  98. /**
  99. * @param string $name
  100. * @return CalendarObject|\Sabre\CalDAV\ICalendarObject
  101. * @throws NotFound
  102. */
  103. public function getChild($name) {
  104. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  105. if (!$obj) {
  106. throw new NotFound('Calendar object not found');
  107. }
  108. $obj['acl'] = $this->getChildACL();
  109. return new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj);
  110. }
  111. /**
  112. * @return INode[]
  113. */
  114. public function getChildren(): array {
  115. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id'], CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  116. $children = [];
  117. foreach ($objs as $obj) {
  118. $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj);
  119. }
  120. return $children;
  121. }
  122. /**
  123. * @param array $paths
  124. * @return array
  125. */
  126. public function getMultipleChildren(array $paths):array {
  127. $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  128. $children = [];
  129. foreach ($objs as $obj) {
  130. $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj);
  131. }
  132. return $children;
  133. }
  134. /**
  135. * @param string $name
  136. * @param null|resource|string $data
  137. * @return null|string
  138. * @throws MethodNotAllowed
  139. */
  140. public function createFile($name, $data = null) {
  141. throw new MethodNotAllowed('Creating objects in cached subscription is not allowed');
  142. }
  143. /**
  144. * @param string $name
  145. * @return bool
  146. */
  147. public function childExists($name):bool {
  148. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  149. if (!$obj) {
  150. return false;
  151. }
  152. return true;
  153. }
  154. /**
  155. * @param array $filters
  156. * @return array
  157. */
  158. public function calendarQuery(array $filters):array {
  159. return $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. public function getChanges($syncToken, $syncLevel, $limit = null) {
  165. if (!$syncToken && $limit) {
  166. throw new UnsupportedLimitOnInitialSyncException();
  167. }
  168. return parent::getChanges($syncToken, $syncLevel, $limit);
  169. }
  170. }