SystemTagsObjectMappingCollection.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Vincent Petry <vincent@nextcloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\SystemTag;
  27. use OCP\IUser;
  28. use OCP\SystemTag\ISystemTag;
  29. use OCP\SystemTag\ISystemTagManager;
  30. use OCP\SystemTag\ISystemTagObjectMapper;
  31. use OCP\SystemTag\TagNotFoundException;
  32. use Sabre\DAV\Exception\BadRequest;
  33. use Sabre\DAV\Exception\Forbidden;
  34. use Sabre\DAV\Exception\NotFound;
  35. use Sabre\DAV\Exception\PreconditionFailed;
  36. use Sabre\DAV\ICollection;
  37. /**
  38. * Collection containing tags by object id
  39. */
  40. class SystemTagsObjectMappingCollection implements ICollection {
  41. public function __construct(
  42. private string $objectId,
  43. private string $objectType,
  44. private IUser $user,
  45. private ISystemTagManager $tagManager,
  46. private ISystemTagObjectMapper $tagMapper,
  47. protected \Closure $childWriteAccessFunction,
  48. ) {
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function createFile($name, $data = null) {
  54. $tagId = $name;
  55. try {
  56. $tags = $this->tagManager->getTagsByIds([$tagId]);
  57. $tag = current($tags);
  58. if (!$this->tagManager->canUserSeeTag($tag, $this->user)) {
  59. throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign');
  60. }
  61. if (!$this->tagManager->canUserAssignTag($tag, $this->user)) {
  62. throw new Forbidden('No permission to assign tag ' . $tagId);
  63. }
  64. $writeAccessFunction = $this->childWriteAccessFunction;
  65. if (!$writeAccessFunction($this->objectId)) {
  66. throw new Forbidden('No permission to assign tag to ' . $this->objectId);
  67. }
  68. $this->tagMapper->assignTags($this->objectId, $this->objectType, $tagId);
  69. } catch (TagNotFoundException $e) {
  70. throw new PreconditionFailed('Tag with id ' . $tagId . ' does not exist, cannot assign');
  71. }
  72. }
  73. /**
  74. * @return never
  75. */
  76. public function createDirectory($name) {
  77. throw new Forbidden('Permission denied to create collections');
  78. }
  79. /**
  80. * @return SystemTagMappingNode
  81. */
  82. public function getChild($tagName) {
  83. try {
  84. if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagName, true)) {
  85. $tag = $this->tagManager->getTagsByIds([$tagName]);
  86. $tag = current($tag);
  87. if ($this->tagManager->canUserSeeTag($tag, $this->user)) {
  88. return $this->makeNode($tag);
  89. }
  90. }
  91. throw new NotFound('Tag with id ' . $tagName . ' not present for object ' . $this->objectId);
  92. } catch (\InvalidArgumentException $e) {
  93. throw new BadRequest('Invalid tag id', 0, $e);
  94. } catch (TagNotFoundException $e) {
  95. throw new NotFound('Tag with id ' . $tagName . ' not found', 0, $e);
  96. }
  97. }
  98. /**
  99. * @return SystemTagMappingNode[]
  100. *
  101. * @psalm-return list<SystemTagMappingNode>
  102. */
  103. public function getChildren() {
  104. $tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType));
  105. if (empty($tagIds)) {
  106. return [];
  107. }
  108. $tags = $this->tagManager->getTagsByIds($tagIds);
  109. // filter out non-visible tags
  110. $tags = array_filter($tags, function ($tag) {
  111. return $this->tagManager->canUserSeeTag($tag, $this->user);
  112. });
  113. return array_values(array_map(function ($tag) {
  114. return $this->makeNode($tag);
  115. }, $tags));
  116. }
  117. public function childExists($tagId) {
  118. try {
  119. $result = $this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true);
  120. if ($result) {
  121. $tags = $this->tagManager->getTagsByIds([$tagId]);
  122. $tag = current($tags);
  123. if (!$this->tagManager->canUserSeeTag($tag, $this->user)) {
  124. return false;
  125. }
  126. }
  127. return $result;
  128. } catch (\InvalidArgumentException $e) {
  129. throw new BadRequest('Invalid tag id', 0, $e);
  130. } catch (TagNotFoundException $e) {
  131. return false;
  132. }
  133. }
  134. /**
  135. * @return never
  136. */
  137. public function delete() {
  138. throw new Forbidden('Permission denied to delete this collection');
  139. }
  140. public function getName() {
  141. return $this->objectId;
  142. }
  143. /**
  144. * @return never
  145. */
  146. public function setName($name) {
  147. throw new Forbidden('Permission denied to rename this collection');
  148. }
  149. /**
  150. * Returns the last modification time, as a unix timestamp
  151. *
  152. * @return null
  153. */
  154. public function getLastModified() {
  155. return null;
  156. }
  157. /**
  158. * Create a sabre node for the mapping of the
  159. * given system tag to the collection's object
  160. *
  161. * @param ISystemTag $tag
  162. *
  163. * @return SystemTagMappingNode
  164. */
  165. private function makeNode(ISystemTag $tag) {
  166. return new SystemTagMappingNode(
  167. $tag,
  168. $this->objectId,
  169. $this->objectType,
  170. $this->user,
  171. $this->tagManager,
  172. $this->tagMapper,
  173. $this->childWriteAccessFunction,
  174. );
  175. }
  176. }