1
0

RootCollection.php 5.0 KB

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