You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemTagMappingNode.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. * @author Vincent Petry <vincent@nextcloud.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\SystemTag;
  25. use OCP\IUser;
  26. use OCP\SystemTag\ISystemTag;
  27. use OCP\SystemTag\ISystemTagManager;
  28. use OCP\SystemTag\ISystemTagObjectMapper;
  29. use OCP\SystemTag\TagNotFoundException;
  30. use Sabre\DAV\Exception\Forbidden;
  31. use Sabre\DAV\Exception\MethodNotAllowed;
  32. use Sabre\DAV\Exception\NotFound;
  33. /**
  34. * Mapping node for system tag to object id
  35. */
  36. class SystemTagMappingNode implements \Sabre\DAV\INode {
  37. /**
  38. * @var ISystemTag
  39. */
  40. protected $tag;
  41. /**
  42. * @var string
  43. */
  44. private $objectId;
  45. /**
  46. * @var string
  47. */
  48. private $objectType;
  49. /**
  50. * User
  51. *
  52. * @var IUser
  53. */
  54. protected $user;
  55. /**
  56. * @var ISystemTagManager
  57. */
  58. protected $tagManager;
  59. /**
  60. * @var ISystemTagObjectMapper
  61. */
  62. private $tagMapper;
  63. /**
  64. * Sets up the node, expects a full path name
  65. *
  66. * @param ISystemTag $tag system tag
  67. * @param string $objectId
  68. * @param string $objectType
  69. * @param IUser $user user
  70. * @param ISystemTagManager $tagManager
  71. * @param ISystemTagObjectMapper $tagMapper
  72. */
  73. public function __construct(
  74. ISystemTag $tag,
  75. $objectId,
  76. $objectType,
  77. IUser $user,
  78. ISystemTagManager $tagManager,
  79. ISystemTagObjectMapper $tagMapper
  80. ) {
  81. $this->tag = $tag;
  82. $this->objectId = $objectId;
  83. $this->objectType = $objectType;
  84. $this->user = $user;
  85. $this->tagManager = $tagManager;
  86. $this->tagMapper = $tagMapper;
  87. }
  88. /**
  89. * Returns the object id of the relationship
  90. *
  91. * @return string object id
  92. */
  93. public function getObjectId() {
  94. return $this->objectId;
  95. }
  96. /**
  97. * Returns the object type of the relationship
  98. *
  99. * @return string object type
  100. */
  101. public function getObjectType() {
  102. return $this->objectType;
  103. }
  104. /**
  105. * Returns the system tag represented by this node
  106. *
  107. * @return ISystemTag system tag
  108. */
  109. public function getSystemTag() {
  110. return $this->tag;
  111. }
  112. /**
  113. * Returns the id of the tag
  114. *
  115. * @return string
  116. */
  117. public function getName() {
  118. return $this->tag->getId();
  119. }
  120. /**
  121. * Renames the node
  122. *
  123. * @param string $name The new name
  124. *
  125. * @throws MethodNotAllowed not allowed to rename node
  126. */
  127. public function setName($name) {
  128. throw new MethodNotAllowed();
  129. }
  130. /**
  131. * Returns null, not supported
  132. *
  133. */
  134. public function getLastModified() {
  135. return null;
  136. }
  137. /**
  138. * Delete tag to object association
  139. */
  140. public function delete() {
  141. try {
  142. if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
  143. throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found');
  144. }
  145. if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) {
  146. throw new Forbidden('No permission to unassign tag ' . $this->tag->getId());
  147. }
  148. $this->tagMapper->unassignTags($this->objectId, $this->objectType, $this->tag->getId());
  149. } catch (TagNotFoundException $e) {
  150. // can happen if concurrent deletion occurred
  151. throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
  152. }
  153. }
  154. }