AddressBook.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 OCP\IL10N;
  25. use Sabre\CardDAV\Backend\BackendInterface;
  26. use Sabre\CardDAV\Card;
  27. use Sabre\DAV\Exception\Forbidden;
  28. use Sabre\DAV\Exception\NotFound;
  29. use Sabre\DAV\PropPatch;
  30. class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable {
  31. /**
  32. * AddressBook constructor.
  33. *
  34. * @param BackendInterface $carddavBackend
  35. * @param array $addressBookInfo
  36. * @param IL10N $l10n
  37. */
  38. public function __construct(BackendInterface $carddavBackend, array $addressBookInfo, IL10N $l10n) {
  39. parent::__construct($carddavBackend, $addressBookInfo);
  40. if ($this->getName() === CardDavBackend::PERSONAL_ADDRESSBOOK_URI &&
  41. $this->addressBookInfo['{DAV:}displayname'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME) {
  42. $this->addressBookInfo['{DAV:}displayname'] = $l10n->t('Contacts');
  43. }
  44. }
  45. /**
  46. * Updates the list of shares.
  47. *
  48. * The first array is a list of people that are to be added to the
  49. * addressbook.
  50. *
  51. * Every element in the add array has the following properties:
  52. * * href - A url. Usually a mailto: address
  53. * * commonName - Usually a first and last name, or false
  54. * * summary - A description of the share, can also be false
  55. * * readOnly - A boolean value
  56. *
  57. * Every element in the remove array is just the address string.
  58. *
  59. * @param array $add
  60. * @param array $remove
  61. * @return void
  62. */
  63. function updateShares(array $add, array $remove) {
  64. /** @var CardDavBackend $carddavBackend */
  65. $carddavBackend = $this->carddavBackend;
  66. $carddavBackend->updateShares($this, $add, $remove);
  67. }
  68. /**
  69. * Returns the list of people whom this addressbook is shared with.
  70. *
  71. * Every element in this array should have the following properties:
  72. * * href - Often a mailto: address
  73. * * commonName - Optional, for example a first + last name
  74. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  75. * * readOnly - boolean
  76. * * summary - Optional, a description for the share
  77. *
  78. * @return array
  79. */
  80. function getShares() {
  81. /** @var CardDavBackend $carddavBackend */
  82. $carddavBackend = $this->carddavBackend;
  83. return $carddavBackend->getShares($this->getResourceId());
  84. }
  85. function getACL() {
  86. $acl = [
  87. [
  88. 'privilege' => '{DAV:}read',
  89. 'principal' => $this->getOwner(),
  90. 'protected' => true,
  91. ]];
  92. $acl[] = [
  93. 'privilege' => '{DAV:}write',
  94. 'principal' => $this->getOwner(),
  95. 'protected' => true,
  96. ];
  97. if ($this->getOwner() !== parent::getOwner()) {
  98. $acl[] = [
  99. 'privilege' => '{DAV:}read',
  100. 'principal' => parent::getOwner(),
  101. 'protected' => true,
  102. ];
  103. if ($this->canWrite()) {
  104. $acl[] = [
  105. 'privilege' => '{DAV:}write',
  106. 'principal' => parent::getOwner(),
  107. 'protected' => true,
  108. ];
  109. }
  110. }
  111. if ($this->getOwner() === 'principals/system/system') {
  112. $acl[] = [
  113. 'privilege' => '{DAV:}read',
  114. 'principal' => '{DAV:}authenticated',
  115. 'protected' => true,
  116. ];
  117. }
  118. /** @var CardDavBackend $carddavBackend */
  119. $carddavBackend = $this->carddavBackend;
  120. return $carddavBackend->applyShareAcl($this->getResourceId(), $acl);
  121. }
  122. function getChildACL() {
  123. return $this->getACL();
  124. }
  125. function getChild($name) {
  126. $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name);
  127. if (!$obj) {
  128. throw new NotFound('Card not found');
  129. }
  130. $obj['acl'] = $this->getChildACL();
  131. return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  132. }
  133. /**
  134. * @return int
  135. */
  136. public function getResourceId() {
  137. return $this->addressBookInfo['id'];
  138. }
  139. function getOwner() {
  140. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  141. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
  142. }
  143. return parent::getOwner();
  144. }
  145. function delete() {
  146. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  147. $principal = 'principal:' . parent::getOwner();
  148. $shares = $this->getShares();
  149. $shares = array_filter($shares, function($share) use ($principal){
  150. return $share['href'] === $principal;
  151. });
  152. if (empty($shares)) {
  153. throw new Forbidden();
  154. }
  155. /** @var CardDavBackend $cardDavBackend */
  156. $cardDavBackend = $this->carddavBackend;
  157. $cardDavBackend->updateShares($this, [], [
  158. 'href' => $principal
  159. ]);
  160. return;
  161. }
  162. parent::delete();
  163. }
  164. function propPatch(PropPatch $propPatch) {
  165. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  166. throw new Forbidden();
  167. }
  168. parent::propPatch($propPatch);
  169. }
  170. public function getContactsGroups() {
  171. /** @var CardDavBackend $cardDavBackend */
  172. $cardDavBackend = $this->carddavBackend;
  173. return $cardDavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES');
  174. }
  175. private function canWrite() {
  176. if (isset($this->addressBookInfo['{http://owncloud.org/ns}read-only'])) {
  177. return !$this->addressBookInfo['{http://owncloud.org/ns}read-only'];
  178. }
  179. return true;
  180. }
  181. }