UserAddressBooks.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\CardDAV;
  9. use OCA\DAV\AppInfo\PluginManager;
  10. use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
  11. use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
  12. use OCA\Federation\TrustedServers;
  13. use OCP\AppFramework\QueryException;
  14. use OCP\IConfig;
  15. use OCP\IGroupManager;
  16. use OCP\IL10N;
  17. use OCP\IRequest;
  18. use OCP\IUser;
  19. use OCP\IUserSession;
  20. use Psr\Container\ContainerExceptionInterface;
  21. use Psr\Container\NotFoundExceptionInterface;
  22. use Sabre\CardDAV\Backend;
  23. use Sabre\CardDAV\IAddressBook;
  24. use Sabre\DAV\Exception\MethodNotAllowed;
  25. use Sabre\DAV\MkCol;
  26. use function array_map;
  27. class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
  28. /** @var IL10N */
  29. protected $l10n;
  30. /** @var IConfig */
  31. protected $config;
  32. /** @var PluginManager */
  33. private $pluginManager;
  34. private ?IUser $user;
  35. private ?IGroupManager $groupManager;
  36. public function __construct(Backend\BackendInterface $carddavBackend,
  37. string $principalUri,
  38. PluginManager $pluginManager,
  39. ?IUser $user,
  40. ?IGroupManager $groupManager) {
  41. parent::__construct($carddavBackend, $principalUri);
  42. $this->pluginManager = $pluginManager;
  43. $this->user = $user;
  44. $this->groupManager = $groupManager;
  45. }
  46. /**
  47. * Returns a list of address books
  48. *
  49. * @return IAddressBook[]
  50. */
  51. public function getChildren() {
  52. if ($this->l10n === null) {
  53. $this->l10n = \OC::$server->getL10N('dav');
  54. }
  55. if ($this->config === null) {
  56. $this->config = \OC::$server->getConfig();
  57. }
  58. /** @var string|array $principal */
  59. $principal = $this->principalUri;
  60. $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
  61. // add the system address book
  62. $systemAddressBook = null;
  63. $systemAddressBookExposed = $this->config->getAppValue('dav', 'system_addressbook_exposed', 'yes') === 'yes';
  64. if ($systemAddressBookExposed && is_string($principal) && $principal !== 'principals/system/system' && $this->carddavBackend instanceof CardDavBackend) {
  65. $systemAddressBook = $this->carddavBackend->getAddressBooksByUri('principals/system/system', 'system');
  66. if ($systemAddressBook !== null) {
  67. $systemAddressBook['uri'] = SystemAddressbook::URI_SHARED;
  68. }
  69. }
  70. if (!is_null($systemAddressBook)) {
  71. $addressBooks[] = $systemAddressBook;
  72. }
  73. $objects = [];
  74. if (!empty($addressBooks)) {
  75. /** @var IAddressBook[] $objects */
  76. $objects = array_map(function (array $addressBook) {
  77. $trustedServers = null;
  78. $request = null;
  79. try {
  80. $trustedServers = \OC::$server->get(TrustedServers::class);
  81. $request = \OC::$server->get(IRequest::class);
  82. } catch (QueryException | NotFoundExceptionInterface | ContainerExceptionInterface $e) {
  83. // nothing to do, the request / trusted servers don't exist
  84. }
  85. if ($addressBook['principaluri'] === 'principals/system/system') {
  86. return new SystemAddressbook(
  87. $this->carddavBackend,
  88. $addressBook,
  89. $this->l10n,
  90. $this->config,
  91. \OCP\Server::get(IUserSession::class),
  92. $request,
  93. $trustedServers,
  94. $this->groupManager
  95. );
  96. }
  97. return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
  98. }, $addressBooks);
  99. }
  100. /** @var IAddressBook[][] $objectsFromPlugins */
  101. $objectsFromPlugins = array_map(function (IAddressBookProvider $plugin): array {
  102. return $plugin->fetchAllForAddressBookHome($this->principalUri);
  103. }, $this->pluginManager->getAddressBookPlugins());
  104. return array_merge($objects, ...$objectsFromPlugins);
  105. }
  106. public function createExtendedCollection($name, MkCol $mkCol) {
  107. if (ExternalAddressBook::doesViolateReservedName($name)) {
  108. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  109. }
  110. parent::createExtendedCollection($name, $mkCol);
  111. }
  112. /**
  113. * Returns a list of ACE's for this node.
  114. *
  115. * Each ACE has the following properties:
  116. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  117. * currently the only supported privileges
  118. * * 'principal', a url to the principal who owns the node
  119. * * 'protected' (optional), indicating that this ACE is not allowed to
  120. * be updated.
  121. *
  122. * @return array
  123. */
  124. public function getACL() {
  125. $acl = parent::getACL();
  126. if ($this->principalUri === 'principals/system/system') {
  127. $acl[] = [
  128. 'privilege' => '{DAV:}read',
  129. 'principal' => '{DAV:}authenticated',
  130. 'protected' => true,
  131. ];
  132. }
  133. return $acl;
  134. }
  135. }