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.

UserAddressBooks.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\ExternalAddressBook;
  30. use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
  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\CardDAV\IAddressBook;
  43. use Sabre\DAV\Exception\MethodNotAllowed;
  44. use Sabre\DAV\MkCol;
  45. use function array_map;
  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. $systemAddressBookExposed = $this->config->getAppValue('dav', 'system_addressbook_exposed', 'yes') === 'yes';
  83. if ($systemAddressBookExposed && is_string($principal) && $principal !== 'principals/system/system' && $this->carddavBackend instanceof CardDavBackend) {
  84. $systemAddressBook = $this->carddavBackend->getAddressBooksByUri('principals/system/system', 'system');
  85. if ($systemAddressBook !== null) {
  86. $systemAddressBook['uri'] = SystemAddressbook::URI_SHARED;
  87. }
  88. }
  89. if (!is_null($systemAddressBook)) {
  90. $addressBooks[] = $systemAddressBook;
  91. }
  92. $objects = [];
  93. if (!empty($addressBooks)) {
  94. /** @var IAddressBook[] $objects */
  95. $objects = array_map(function (array $addressBook) {
  96. $trustedServers = null;
  97. $request = null;
  98. try {
  99. $trustedServers = \OC::$server->get(TrustedServers::class);
  100. $request = \OC::$server->get(IRequest::class);
  101. } catch (QueryException | NotFoundExceptionInterface | ContainerExceptionInterface $e) {
  102. // nothing to do, the request / trusted servers don't exist
  103. }
  104. if ($addressBook['principaluri'] === 'principals/system/system') {
  105. return new SystemAddressbook(
  106. $this->carddavBackend,
  107. $addressBook,
  108. $this->l10n,
  109. $this->config,
  110. \OCP\Server::get(IUserSession::class),
  111. $request,
  112. $trustedServers,
  113. $this->groupManager
  114. );
  115. }
  116. return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
  117. }, $addressBooks);
  118. }
  119. /** @var IAddressBook[][] $objectsFromPlugins */
  120. $objectsFromPlugins = array_map(function (IAddressBookProvider $plugin): array {
  121. return $plugin->fetchAllForAddressBookHome($this->principalUri);
  122. }, $this->pluginManager->getAddressBookPlugins());
  123. return array_merge($objects, ...$objectsFromPlugins);
  124. }
  125. public function createExtendedCollection($name, MkCol $mkCol) {
  126. if (ExternalAddressBook::doesViolateReservedName($name)) {
  127. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  128. }
  129. parent::createExtendedCollection($name, $mkCol);
  130. }
  131. /**
  132. * Returns a list of ACE's for this node.
  133. *
  134. * Each ACE has the following properties:
  135. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  136. * currently the only supported privileges
  137. * * 'principal', a url to the principal who owns the node
  138. * * 'protected' (optional), indicating that this ACE is not allowed to
  139. * be updated.
  140. *
  141. * @return array
  142. */
  143. public function getACL() {
  144. $acl = parent::getACL();
  145. if ($this->principalUri === 'principals/system/system') {
  146. $acl[] = [
  147. 'privilege' => '{DAV:}read',
  148. 'principal' => '{DAV:}authenticated',
  149. 'protected' => true,
  150. ];
  151. }
  152. return $acl;
  153. }
  154. }