RootCollection.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Comments;
  8. use OCP\Comments\CommentsEntityEvent;
  9. use OCP\Comments\ICommentsManager;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\IUserManager;
  12. use OCP\IUserSession;
  13. use Psr\Log\LoggerInterface;
  14. use Sabre\DAV\Exception\Forbidden;
  15. use Sabre\DAV\Exception\NotAuthenticated;
  16. use Sabre\DAV\Exception\NotFound;
  17. use Sabre\DAV\ICollection;
  18. class RootCollection implements ICollection {
  19. /** @var EntityTypeCollection[]|null */
  20. private ?array $entityTypeCollections = null;
  21. protected string $name = 'comments';
  22. public function __construct(
  23. protected ICommentsManager $commentsManager,
  24. protected IUserManager $userManager,
  25. protected IUserSession $userSession,
  26. protected IEventDispatcher $dispatcher,
  27. protected LoggerInterface $logger,
  28. ) {
  29. }
  30. /**
  31. * initializes the collection. At this point of time, we need the logged in
  32. * user. Since it is not the case when the instance is created, we cannot
  33. * have this in the constructor.
  34. *
  35. * @throws NotAuthenticated
  36. */
  37. protected function initCollections() {
  38. if ($this->entityTypeCollections !== null) {
  39. return;
  40. }
  41. $user = $this->userSession->getUser();
  42. if (is_null($user)) {
  43. throw new NotAuthenticated();
  44. }
  45. $event = new CommentsEntityEvent();
  46. $this->dispatcher->dispatchTyped($event);
  47. $this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event);
  48. $this->entityTypeCollections = [];
  49. foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
  50. $this->entityTypeCollections[$entity] = new EntityTypeCollection(
  51. $entity,
  52. $this->commentsManager,
  53. $this->userManager,
  54. $this->userSession,
  55. $this->logger,
  56. $entityExistsFunction
  57. );
  58. }
  59. }
  60. /**
  61. * Creates a new file in the directory
  62. *
  63. * @param string $name Name of the file
  64. * @param resource|string $data Initial payload
  65. * @return null|string
  66. * @throws Forbidden
  67. */
  68. public function createFile($name, $data = null) {
  69. throw new Forbidden('Cannot create comments by id');
  70. }
  71. /**
  72. * Creates a new subdirectory
  73. *
  74. * @param string $name
  75. * @throws Forbidden
  76. */
  77. public function createDirectory($name) {
  78. throw new Forbidden('Permission denied to create collections');
  79. }
  80. /**
  81. * Returns a specific child node, referenced by its name
  82. *
  83. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  84. * exist.
  85. *
  86. * @param string $name
  87. * @return \Sabre\DAV\INode
  88. * @throws NotFound
  89. */
  90. public function getChild($name) {
  91. $this->initCollections();
  92. if (isset($this->entityTypeCollections[$name])) {
  93. return $this->entityTypeCollections[$name];
  94. }
  95. throw new NotFound('Entity type "' . $name . '" not found."');
  96. }
  97. /**
  98. * Returns an array with all the child nodes
  99. *
  100. * @return \Sabre\DAV\INode[]
  101. */
  102. public function getChildren() {
  103. $this->initCollections();
  104. assert(!is_null($this->entityTypeCollections));
  105. return $this->entityTypeCollections;
  106. }
  107. /**
  108. * Checks if a child-node with the specified name exists
  109. *
  110. * @param string $name
  111. * @return bool
  112. */
  113. public function childExists($name) {
  114. $this->initCollections();
  115. assert(!is_null($this->entityTypeCollections));
  116. return isset($this->entityTypeCollections[$name]);
  117. }
  118. /**
  119. * Deleted the current node
  120. *
  121. * @throws Forbidden
  122. */
  123. public function delete() {
  124. throw new Forbidden('Permission denied to delete this collection');
  125. }
  126. /**
  127. * Returns the name of the node.
  128. *
  129. * This is used to generate the url.
  130. *
  131. * @return string
  132. */
  133. public function getName() {
  134. return $this->name;
  135. }
  136. /**
  137. * Renames the node
  138. *
  139. * @param string $name The new name
  140. * @throws Forbidden
  141. */
  142. public function setName($name) {
  143. throw new Forbidden('Permission denied to rename this collection');
  144. }
  145. /**
  146. * Returns the last modification time, as a unix timestamp
  147. *
  148. * @return ?int
  149. */
  150. public function getLastModified() {
  151. return null;
  152. }
  153. }