AddressBook.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. * * readOnly - A boolean value
  66. *
  67. * Every element in the remove array is just the address string.
  68. *
  69. * @param list<array{href: string, commonName: string, readOnly: bool}> $add
  70. * @param list<string> $remove
  71. * @throws Forbidden
  72. */
  73. public function updateShares(array $add, array $remove): void {
  74. if ($this->isShared()) {
  75. throw new Forbidden();
  76. }
  77. $this->carddavBackend->updateShares($this, $add, $remove);
  78. }
  79. /**
  80. * Returns the list of people whom this addressbook is shared with.
  81. *
  82. * Every element in this array should have the following properties:
  83. * * href - Often a mailto: address
  84. * * commonName - Optional, for example a first + last name
  85. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  86. * * readOnly - boolean
  87. *
  88. * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}>
  89. */
  90. public function getShares(): array {
  91. if ($this->isShared()) {
  92. return [];
  93. }
  94. return $this->carddavBackend->getShares($this->getResourceId());
  95. }
  96. public function getACL() {
  97. $acl = [
  98. [
  99. 'privilege' => '{DAV:}read',
  100. 'principal' => $this->getOwner(),
  101. 'protected' => true,
  102. ],[
  103. 'privilege' => '{DAV:}write',
  104. 'principal' => $this->getOwner(),
  105. 'protected' => true,
  106. ]
  107. ];
  108. if ($this->getOwner() === 'principals/system/system') {
  109. $acl[] = [
  110. 'privilege' => '{DAV:}read',
  111. 'principal' => '{DAV:}authenticated',
  112. 'protected' => true,
  113. ];
  114. }
  115. if (!$this->isShared()) {
  116. return $acl;
  117. }
  118. if ($this->getOwner() !== parent::getOwner()) {
  119. $acl[] = [
  120. 'privilege' => '{DAV:}read',
  121. 'principal' => parent::getOwner(),
  122. 'protected' => true,
  123. ];
  124. if ($this->canWrite()) {
  125. $acl[] = [
  126. 'privilege' => '{DAV:}write',
  127. 'principal' => parent::getOwner(),
  128. 'protected' => true,
  129. ];
  130. }
  131. }
  132. $acl = $this->carddavBackend->applyShareAcl($this->getResourceId(), $acl);
  133. $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system'];
  134. return array_filter($acl, function ($rule) use ($allowedPrincipals) {
  135. return \in_array($rule['principal'], $allowedPrincipals, true);
  136. });
  137. }
  138. public function getChildACL() {
  139. return $this->getACL();
  140. }
  141. public function getChild($name) {
  142. $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name);
  143. if (!$obj) {
  144. throw new NotFound('Card not found');
  145. }
  146. $obj['acl'] = $this->getChildACL();
  147. return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  148. }
  149. public function getResourceId(): int {
  150. return $this->addressBookInfo['id'];
  151. }
  152. public function getOwner(): ?string {
  153. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  154. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
  155. }
  156. return parent::getOwner();
  157. }
  158. public function delete() {
  159. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  160. $principal = 'principal:' . parent::getOwner();
  161. $shares = $this->carddavBackend->getShares($this->getResourceId());
  162. $shares = array_filter($shares, function ($share) use ($principal) {
  163. return $share['href'] === $principal;
  164. });
  165. if (empty($shares)) {
  166. throw new Forbidden();
  167. }
  168. $this->carddavBackend->updateShares($this, [], [
  169. $principal
  170. ]);
  171. return;
  172. }
  173. parent::delete();
  174. }
  175. public function propPatch(PropPatch $propPatch) {
  176. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  177. throw new Forbidden();
  178. }
  179. parent::propPatch($propPatch);
  180. }
  181. public function getContactsGroups() {
  182. return $this->carddavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES');
  183. }
  184. private function isShared(): bool {
  185. if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  186. return false;
  187. }
  188. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'] !== $this->addressBookInfo['principaluri'];
  189. }
  190. private function canWrite(): bool {
  191. if (isset($this->addressBookInfo['{http://owncloud.org/ns}read-only'])) {
  192. return !$this->addressBookInfo['{http://owncloud.org/ns}read-only'];
  193. }
  194. return true;
  195. }
  196. public function getChanges($syncToken, $syncLevel, $limit = null) {
  197. if (!$syncToken && $limit) {
  198. throw new UnsupportedLimitOnInitialSyncException();
  199. }
  200. return parent::getChanges($syncToken, $syncLevel, $limit);
  201. }
  202. }