1
0

RootCollection.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Comments;
  26. use OCP\Comments\CommentsEntityEvent;
  27. use OCP\Comments\ICommentsManager;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\IUserManager;
  30. use OCP\IUserSession;
  31. use Psr\Log\LoggerInterface;
  32. use Sabre\DAV\Exception\Forbidden;
  33. use Sabre\DAV\Exception\NotAuthenticated;
  34. use Sabre\DAV\Exception\NotFound;
  35. use Sabre\DAV\ICollection;
  36. class RootCollection implements ICollection {
  37. /** @var EntityTypeCollection[]|null */
  38. private ?array $entityTypeCollections = null;
  39. protected ICommentsManager $commentsManager;
  40. protected string $name = 'comments';
  41. protected LoggerInterface $logger;
  42. protected IUserManager $userManager;
  43. protected IUserSession $userSession;
  44. protected IEventDispatcher $dispatcher;
  45. public function __construct(
  46. ICommentsManager $commentsManager,
  47. IUserManager $userManager,
  48. IUserSession $userSession,
  49. IEventDispatcher $dispatcher,
  50. LoggerInterface $logger) {
  51. $this->commentsManager = $commentsManager;
  52. $this->logger = $logger;
  53. $this->userManager = $userManager;
  54. $this->userSession = $userSession;
  55. $this->dispatcher = $dispatcher;
  56. }
  57. /**
  58. * initializes the collection. At this point of time, we need the logged in
  59. * user. Since it is not the case when the instance is created, we cannot
  60. * have this in the constructor.
  61. *
  62. * @throws NotAuthenticated
  63. */
  64. protected function initCollections() {
  65. if ($this->entityTypeCollections !== null) {
  66. return;
  67. }
  68. $user = $this->userSession->getUser();
  69. if (is_null($user)) {
  70. throw new NotAuthenticated();
  71. }
  72. $event = new CommentsEntityEvent();
  73. $this->dispatcher->dispatchTyped($event);
  74. $this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event);
  75. $this->entityTypeCollections = [];
  76. foreach ($event->getEntityCollections() as $entity => $entityExistsFunction) {
  77. $this->entityTypeCollections[$entity] = new EntityTypeCollection(
  78. $entity,
  79. $this->commentsManager,
  80. $this->userManager,
  81. $this->userSession,
  82. $this->logger,
  83. $entityExistsFunction
  84. );
  85. }
  86. }
  87. /**
  88. * Creates a new file in the directory
  89. *
  90. * @param string $name Name of the file
  91. * @param resource|string $data Initial payload
  92. * @return null|string
  93. * @throws Forbidden
  94. */
  95. public function createFile($name, $data = null) {
  96. throw new Forbidden('Cannot create comments by id');
  97. }
  98. /**
  99. * Creates a new subdirectory
  100. *
  101. * @param string $name
  102. * @throws Forbidden
  103. */
  104. public function createDirectory($name) {
  105. throw new Forbidden('Permission denied to create collections');
  106. }
  107. /**
  108. * Returns a specific child node, referenced by its name
  109. *
  110. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  111. * exist.
  112. *
  113. * @param string $name
  114. * @return \Sabre\DAV\INode
  115. * @throws NotFound
  116. */
  117. public function getChild($name) {
  118. $this->initCollections();
  119. if (isset($this->entityTypeCollections[$name])) {
  120. return $this->entityTypeCollections[$name];
  121. }
  122. throw new NotFound('Entity type "' . $name . '" not found."');
  123. }
  124. /**
  125. * Returns an array with all the child nodes
  126. *
  127. * @return \Sabre\DAV\INode[]
  128. */
  129. public function getChildren() {
  130. $this->initCollections();
  131. assert(!is_null($this->entityTypeCollections));
  132. return $this->entityTypeCollections;
  133. }
  134. /**
  135. * Checks if a child-node with the specified name exists
  136. *
  137. * @param string $name
  138. * @return bool
  139. */
  140. public function childExists($name) {
  141. $this->initCollections();
  142. assert(!is_null($this->entityTypeCollections));
  143. return isset($this->entityTypeCollections[$name]);
  144. }
  145. /**
  146. * Deleted the current node
  147. *
  148. * @throws Forbidden
  149. */
  150. public function delete() {
  151. throw new Forbidden('Permission denied to delete this collection');
  152. }
  153. /**
  154. * Returns the name of the node.
  155. *
  156. * This is used to generate the url.
  157. *
  158. * @return string
  159. */
  160. public function getName() {
  161. return $this->name;
  162. }
  163. /**
  164. * Renames the node
  165. *
  166. * @param string $name The new name
  167. * @throws Forbidden
  168. */
  169. public function setName($name) {
  170. throw new Forbidden('Permission denied to rename this collection');
  171. }
  172. /**
  173. * Returns the last modification time, as a unix timestamp
  174. *
  175. * @return ?int
  176. */
  177. public function getLastModified() {
  178. return null;
  179. }
  180. }