You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AddressBook.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\CardDAV;
  27. use OCA\DAV\DAV\Sharing\IShareable;
  28. use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
  29. use OCP\IL10N;
  30. use Sabre\CardDAV\Backend\BackendInterface;
  31. use Sabre\CardDAV\Card;
  32. use Sabre\DAV\Exception\Forbidden;
  33. use Sabre\DAV\Exception\NotFound;
  34. use Sabre\DAV\PropPatch;
  35. /**
  36. * Class AddressBook
  37. *
  38. * @package OCA\DAV\CardDAV
  39. * @property BackendInterface|CardDavBackend $carddavBackend
  40. */
  41. class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable {
  42. /**
  43. * AddressBook constructor.
  44. *
  45. * @param BackendInterface $carddavBackend
  46. * @param array $addressBookInfo
  47. * @param IL10N $l10n
  48. */
  49. public function __construct(BackendInterface $carddavBackend, array $addressBookInfo, IL10N $l10n) {
  50. parent::__construct($carddavBackend, $addressBookInfo);
  51. if ($this->addressBookInfo['{DAV:}displayname'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME &&
  52. $this->getName() === CardDavBackend::PERSONAL_ADDRESSBOOK_URI) {
  53. $this->addressBookInfo['{DAV:}displayname'] = $l10n->t('Contacts');
  54. }
  55. }
  56. /**
  57. * Updates the list of shares.
  58. *
  59. * The first array is a list of people that are to be added to the
  60. * addressbook.
  61. *
  62. * Every element in the add array has the following properties:
  63. * * href - A url. Usually a mailto: address
  64. * * commonName - Usually a first and last name, or false
  65. * * summary - A description of the share, can also be false
  66. * * readOnly - A boolean value
  67. *
  68. * Every element in the remove array is just the address string.
  69. *
  70. * @param array $add
  71. * @param array $remove
  72. * @return void
  73. * @throws Forbidden
  74. */
  75. public function updateShares(array $add, array $remove) {
  76. if ($this->isShared()) {
  77. throw new Forbidden();
  78. }
  79. $this->carddavBackend->updateShares($this, $add, $remove);
  80. }
  81. /**
  82. * Returns the list of people whom this addressbook is shared with.
  83. *
  84. * Every element in this array should have the following properties:
  85. * * href - Often a mailto: address
  86. * * commonName - Optional, for example a first + last name
  87. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  88. * * readOnly - boolean
  89. * * summary - Optional, a description for the share
  90. *
  91. * @return array
  92. */
  93. public function getShares() {
  94. if ($this->isShared()) {
  95. return [];
  96. }
  97. return $this->carddavBackend->getShares($this->getResourceId());
  98. }
  99. public function getACL() {
  100. $acl = [
  101. [
  102. 'privilege' => '{DAV:}read',
  103. 'principal' => $this->getOwner(),
  104. 'protected' => true,
  105. ],[
  106. 'privilege' => '{DAV:}write',
  107. 'principal' => $this->getOwner(),
  108. 'protected' => true,
  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. if (!$this->isShared()) {
  119. return $acl;
  120. }
  121. if ($this->getOwner() !== parent::getOwner()) {
  122. $acl[] = [
  123. 'privilege' => '{DAV:}read',
  124. 'principal' => parent::getOwner(),
  125. 'protected' => true,
  126. ];
  127. if ($this->canWrite()) {
  128. $acl[] = [
  129. 'privilege' => '{DAV:}write',
  130. 'principal' => parent::getOwner(),
  131. 'protected' => true,
  132. ];
  133. }
  134. }
  135. $acl = $this->carddavBackend->applyShareAcl($this->getResourceId(), $acl);
  136. $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system'];
  137. return array_filter($acl, function ($rule) use ($allowedPrincipals) {
  138. return \in_array($rule['principal'], $allowedPrincipals, true);
  139. });
  140. }
  141. public function getChildACL() {
  142. return $this->getACL();
  143. }
  144. public function getChild($name) {
  145. $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name);
  146. if (!$obj) {
  147. throw new NotFound('Card not found');
  148. }
  149. $obj['acl'] = $this->getChildACL();
  150. return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  151. }
  152. /**
  153. * @return int
  154. */
  155. public function getResourceId() {
  156. return $this->addressBookInfo['id'];
  157. }
  158. public function getOwner() {
  159. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  160. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
  161. }
  162. return parent::getOwner();
  163. }
  164. public function delete() {
  165. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  166. $principal = 'principal:' . parent::getOwner();
  167. $shares = $this->carddavBackend->getShares($this->getResourceId());
  168. $shares = array_filter($shares, function ($share) use ($principal) {
  169. return $share['href'] === $principal;
  170. });
  171. if (empty($shares)) {
  172. throw new Forbidden();
  173. }
  174. $this->carddavBackend->updateShares($this, [], [
  175. $principal
  176. ]);
  177. return;
  178. }
  179. parent::delete();
  180. }
  181. public function propPatch(PropPatch $propPatch) {
  182. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  183. throw new Forbidden();
  184. }
  185. parent::propPatch($propPatch);
  186. }
  187. public function getContactsGroups() {
  188. return $this->carddavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES');
  189. }
  190. private function isShared() {
  191. if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  192. return false;
  193. }
  194. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'] !== $this->addressBookInfo['principaluri'];
  195. }
  196. private function canWrite() {
  197. if (isset($this->addressBookInfo['{http://owncloud.org/ns}read-only'])) {
  198. return !$this->addressBookInfo['{http://owncloud.org/ns}read-only'];
  199. }
  200. return true;
  201. }
  202. public function getChanges($syncToken, $syncLevel, $limit = null) {
  203. if (!$syncToken && $limit) {
  204. throw new UnsupportedLimitOnInitialSyncException();
  205. }
  206. return parent::getChanges($syncToken, $syncLevel, $limit);
  207. }
  208. }