RootCollection.php 5.0 KB

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