UserAddressBooks.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  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. * @author Anna Larch <anna.larch@gmx.net>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\CardDAV;
  28. use OCA\DAV\AppInfo\PluginManager;
  29. use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
  30. use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
  31. use OCA\Federation\TrustedServers;
  32. use OCP\AppFramework\QueryException;
  33. use OCP\IConfig;
  34. use OCP\IGroupManager;
  35. use OCP\IL10N;
  36. use OCP\IRequest;
  37. use OCP\IUser;
  38. use OCP\IUserSession;
  39. use Psr\Container\ContainerExceptionInterface;
  40. use Psr\Container\NotFoundExceptionInterface;
  41. use Sabre\CardDAV\Backend;
  42. use Sabre\DAV\Exception\MethodNotAllowed;
  43. use Sabre\CardDAV\IAddressBook;
  44. use function array_map;
  45. use Sabre\DAV\MkCol;
  46. class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
  47. /** @var IL10N */
  48. protected $l10n;
  49. /** @var IConfig */
  50. protected $config;
  51. /** @var PluginManager */
  52. private $pluginManager;
  53. private ?IUser $user;
  54. private ?IGroupManager $groupManager;
  55. public function __construct(Backend\BackendInterface $carddavBackend,
  56. string $principalUri,
  57. PluginManager $pluginManager,
  58. ?IUser $user,
  59. ?IGroupManager $groupManager) {
  60. parent::__construct($carddavBackend, $principalUri);
  61. $this->pluginManager = $pluginManager;
  62. $this->user = $user;
  63. $this->groupManager = $groupManager;
  64. }
  65. /**
  66. * Returns a list of address books
  67. *
  68. * @return IAddressBook[]
  69. */
  70. public function getChildren() {
  71. if ($this->l10n === null) {
  72. $this->l10n = \OC::$server->getL10N('dav');
  73. }
  74. if ($this->config === null) {
  75. $this->config = \OC::$server->getConfig();
  76. }
  77. /** @var string|array $principal */
  78. $principal = $this->principalUri;
  79. $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
  80. // add the system address book
  81. $systemAddressBook = null;
  82. if (is_string($principal) && $principal !== 'principals/system/system' && $this->carddavBackend instanceof CardDavBackend) {
  83. $systemAddressBook = $this->carddavBackend->getAddressBooksByUri('principals/system/system', 'system');
  84. if ($systemAddressBook !== null) {
  85. $systemAddressBook['uri'] = SystemAddressbook::URI_SHARED;
  86. }
  87. }
  88. if (!is_null($systemAddressBook)) {
  89. $addressBooks[] = $systemAddressBook;
  90. }
  91. $objects = [];
  92. if (!empty($addressBooks)) {
  93. /** @var IAddressBook[] $objects */
  94. $objects = array_map(function (array $addressBook) {
  95. $trustedServers = null;
  96. $request = null;
  97. try {
  98. $trustedServers = \OC::$server->get(TrustedServers::class);
  99. $request = \OC::$server->get(IRequest::class);
  100. } catch (NotFoundExceptionInterface | ContainerExceptionInterface $e) {
  101. // nothing to do, the request / trusted servers don't exist
  102. }
  103. if ($addressBook['principaluri'] === 'principals/system/system') {
  104. return new SystemAddressbook(
  105. $this->carddavBackend,
  106. $addressBook,
  107. $this->l10n,
  108. $this->config,
  109. \OCP\Server::get(IUserSession::class),
  110. $request,
  111. $trustedServers,
  112. $this->groupManager
  113. );
  114. }
  115. return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
  116. }, $addressBooks);
  117. }
  118. /** @var IAddressBook[][] $objectsFromPlugins */
  119. $objectsFromPlugins = array_map(function (IAddressBookProvider $plugin): array {
  120. return $plugin->fetchAllForAddressBookHome($this->principalUri);
  121. }, $this->pluginManager->getAddressBookPlugins());
  122. return array_merge($objects, ...$objectsFromPlugins);
  123. }
  124. public function createExtendedCollection($name, MkCol $mkCol) {
  125. if (ExternalAddressBook::doesViolateReservedName($name)) {
  126. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  127. }
  128. parent::createExtendedCollection($name, $mkCol);
  129. }
  130. /**
  131. * Returns a list of ACE's for this node.
  132. *
  133. * Each ACE has the following properties:
  134. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  135. * currently the only supported privileges
  136. * * 'principal', a url to the principal who owns the node
  137. * * 'protected' (optional), indicating that this ACE is not allowed to
  138. * be updated.
  139. *
  140. * @return array
  141. */
  142. public function getACL() {
  143. $acl = parent::getACL();
  144. if ($this->principalUri === 'principals/system/system') {
  145. $acl[] = [
  146. 'privilege' => '{DAV:}read',
  147. 'principal' => '{DAV:}authenticated',
  148. 'protected' => true,
  149. ];
  150. }
  151. return $acl;
  152. }
  153. }