UserAddressBooks.php 4.3 KB

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