UserAddressBooks.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. *
  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\AppInfo\PluginManager;
  28. use OCA\DAV\CardDAV\Integration\IAddressBookProvider;
  29. use OCA\DAV\CardDAV\Integration\ExternalAddressBook;
  30. use OCP\IConfig;
  31. use OCP\IL10N;
  32. use Sabre\CardDAV\Backend;
  33. use Sabre\DAV\Exception\MethodNotAllowed;
  34. use Sabre\CardDAV\IAddressBook;
  35. use function array_map;
  36. use Sabre\DAV\MkCol;
  37. class UserAddressBooks extends \Sabre\CardDAV\AddressBookHome {
  38. /** @var IL10N */
  39. protected $l10n;
  40. /** @var IConfig */
  41. protected $config;
  42. /** @var PluginManager */
  43. private $pluginManager;
  44. public function __construct(Backend\BackendInterface $carddavBackend,
  45. string $principalUri,
  46. PluginManager $pluginManager) {
  47. parent::__construct($carddavBackend, $principalUri);
  48. $this->pluginManager = $pluginManager;
  49. }
  50. /**
  51. * Returns a list of address books
  52. *
  53. * @return IAddressBook[]
  54. */
  55. public function getChildren() {
  56. if ($this->l10n === null) {
  57. $this->l10n = \OC::$server->getL10N('dav');
  58. }
  59. if ($this->config === null) {
  60. $this->config = \OC::$server->getConfig();
  61. }
  62. $addressBooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
  63. /** @var IAddressBook[] $objects */
  64. $objects = array_map(function (array $addressBook) {
  65. if ($addressBook['principaluri'] === 'principals/system/system') {
  66. return new SystemAddressbook($this->carddavBackend, $addressBook, $this->l10n, $this->config);
  67. }
  68. return new AddressBook($this->carddavBackend, $addressBook, $this->l10n);
  69. }, $addressBooks);
  70. /** @var IAddressBook[][] $objectsFromPlugins */
  71. $objectsFromPlugins = array_map(function (IAddressBookProvider $plugin): array {
  72. return $plugin->fetchAllForAddressBookHome($this->principalUri);
  73. }, $this->pluginManager->getAddressBookPlugins());
  74. return array_merge($objects, ...$objectsFromPlugins);
  75. }
  76. public function createExtendedCollection($name, MkCol $mkCol) {
  77. if (ExternalAddressBook::doesViolateReservedName($name)) {
  78. throw new MethodNotAllowed('The resource you tried to create has a reserved name');
  79. }
  80. parent::createExtendedCollection($name, $mkCol);
  81. }
  82. /**
  83. * Returns a list of ACE's for this node.
  84. *
  85. * Each ACE has the following properties:
  86. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  87. * currently the only supported privileges
  88. * * 'principal', a url to the principal who owns the node
  89. * * 'protected' (optional), indicating that this ACE is not allowed to
  90. * be updated.
  91. *
  92. * @return array
  93. */
  94. public function getACL() {
  95. $acl = parent::getACL();
  96. if ($this->principalUri === 'principals/system/system') {
  97. $acl[] = [
  98. 'privilege' => '{DAV:}read',
  99. 'principal' => '{DAV:}authenticated',
  100. 'protected' => true,
  101. ];
  102. }
  103. return $acl;
  104. }
  105. }