RootCollection.php 5.2 KB

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