AddressBook.php 5.0 KB

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