Calendar.php 5.0 KB

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