RootCollection.php 4.3 KB

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