Calendar.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV;
  25. use OCA\DAV\DAV\Sharing\IShareable;
  26. use OCP\IL10N;
  27. use Sabre\CalDAV\Backend\BackendInterface;
  28. use Sabre\DAV\Exception\Forbidden;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\PropPatch;
  31. class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
  32. public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) {
  33. parent::__construct($caldavBackend, $calendarInfo);
  34. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  35. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
  36. }
  37. }
  38. /**
  39. * Updates the list of shares.
  40. *
  41. * The first array is a list of people that are to be added to the
  42. * resource.
  43. *
  44. * Every element in the add array has the following properties:
  45. * * href - A url. Usually a mailto: address
  46. * * commonName - Usually a first and last name, or false
  47. * * summary - A description of the share, can also be false
  48. * * readOnly - A boolean value
  49. *
  50. * Every element in the remove array is just the address string.
  51. *
  52. * @param array $add
  53. * @param array $remove
  54. * @return void
  55. */
  56. function updateShares(array $add, array $remove) {
  57. /** @var CalDavBackend $calDavBackend */
  58. $calDavBackend = $this->caldavBackend;
  59. $calDavBackend->updateShares($this, $add, $remove);
  60. }
  61. /**
  62. * Returns the list of people whom this resource is shared with.
  63. *
  64. * Every element in this array should have the following properties:
  65. * * href - Often a mailto: address
  66. * * commonName - Optional, for example a first + last name
  67. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  68. * * readOnly - boolean
  69. * * summary - Optional, a description for the share
  70. *
  71. * @return array
  72. */
  73. function getShares() {
  74. /** @var CalDavBackend $calDavBackend */
  75. $calDavBackend = $this->caldavBackend;
  76. return $calDavBackend->getShares($this->getResourceId());
  77. }
  78. /**
  79. * @return int
  80. */
  81. public function getResourceId() {
  82. return $this->calendarInfo['id'];
  83. }
  84. function getACL() {
  85. $acl = [
  86. [
  87. 'privilege' => '{DAV:}read',
  88. 'principal' => $this->getOwner(),
  89. 'protected' => true,
  90. ]];
  91. if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
  92. $acl[] = [
  93. 'privilege' => '{DAV:}write',
  94. 'principal' => $this->getOwner(),
  95. 'protected' => true,
  96. ];
  97. }
  98. if ($this->getOwner() !== parent::getOwner()) {
  99. $acl[] = [
  100. 'privilege' => '{DAV:}read',
  101. 'principal' => parent::getOwner(),
  102. 'protected' => true,
  103. ];
  104. if ($this->canWrite()) {
  105. $acl[] = [
  106. 'privilege' => '{DAV:}write',
  107. 'principal' => parent::getOwner(),
  108. 'protected' => true,
  109. ];
  110. }
  111. }
  112. /** @var CalDavBackend $calDavBackend */
  113. $calDavBackend = $this->caldavBackend;
  114. return $calDavBackend->applyShareAcl($this->getResourceId(), $acl);
  115. }
  116. function getChildACL() {
  117. return $this->getACL();
  118. }
  119. function getOwner() {
  120. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  121. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  122. }
  123. return parent::getOwner();
  124. }
  125. function delete() {
  126. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  127. $principal = 'principal:' . parent::getOwner();
  128. $shares = $this->getShares();
  129. $shares = array_filter($shares, function($share) use ($principal){
  130. return $share['href'] === $principal;
  131. });
  132. if (empty($shares)) {
  133. throw new Forbidden();
  134. }
  135. /** @var CalDavBackend $calDavBackend */
  136. $calDavBackend = $this->caldavBackend;
  137. $calDavBackend->updateShares($this, [], [
  138. 'href' => $principal
  139. ]);
  140. return;
  141. }
  142. parent::delete();
  143. }
  144. function propPatch(PropPatch $propPatch) {
  145. $mutations = $propPatch->getMutations();
  146. // If this is a shared calendar, the user can only change the enabled property, to hide it.
  147. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) && (sizeof($mutations) !== 1 || !isset($mutations['{http://owncloud.org/ns}calendar-enabled']))) {
  148. throw new Forbidden();
  149. }
  150. parent::propPatch($propPatch);
  151. }
  152. function getChild($name) {
  153. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  154. if (!$obj) {
  155. throw new NotFound('Calendar object not found');
  156. }
  157. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  158. throw new NotFound('Calendar object not found');
  159. }
  160. $obj['acl'] = $this->getChildACL();
  161. return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  162. }
  163. function getChildren() {
  164. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
  165. $children = [];
  166. foreach ($objs as $obj) {
  167. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  168. continue;
  169. }
  170. $obj['acl'] = $this->getChildACL();
  171. $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  172. }
  173. return $children;
  174. }
  175. function getMultipleChildren(array $paths) {
  176. $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
  177. $children = [];
  178. foreach ($objs as $obj) {
  179. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  180. continue;
  181. }
  182. $obj['acl'] = $this->getChildACL();
  183. $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  184. }
  185. return $children;
  186. }
  187. function childExists($name) {
  188. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  189. if (!$obj) {
  190. return false;
  191. }
  192. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  193. return false;
  194. }
  195. return true;
  196. }
  197. function calendarQuery(array $filters) {
  198. $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
  199. if ($this->isShared()) {
  200. return array_filter($uris, function ($uri) {
  201. return $this->childExists($uri);
  202. });
  203. }
  204. return $uris;
  205. }
  206. private function canWrite() {
  207. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  208. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  209. }
  210. return true;
  211. }
  212. private function isShared() {
  213. return isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']);
  214. }
  215. }