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.

SystemTagNode.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Vincent Petry <vincent@nextcloud.com>
  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\SystemTag;
  26. use OCP\IUser;
  27. use OCP\SystemTag\ISystemTag;
  28. use OCP\SystemTag\ISystemTagManager;
  29. use OCP\SystemTag\TagAlreadyExistsException;
  30. use OCP\SystemTag\TagNotFoundException;
  31. use Sabre\DAV\Exception\Conflict;
  32. use Sabre\DAV\Exception\Forbidden;
  33. use Sabre\DAV\Exception\MethodNotAllowed;
  34. use Sabre\DAV\Exception\NotFound;
  35. /**
  36. * DAV node representing a system tag, with the name being the tag id.
  37. */
  38. class SystemTagNode implements \Sabre\DAV\INode {
  39. /**
  40. * @var ISystemTag
  41. */
  42. protected $tag;
  43. /**
  44. * @var ISystemTagManager
  45. */
  46. protected $tagManager;
  47. /**
  48. * User
  49. *
  50. * @var IUser
  51. */
  52. protected $user;
  53. /**
  54. * Whether to allow permissions for admins
  55. *
  56. * @var bool
  57. */
  58. protected $isAdmin;
  59. /**
  60. * Sets up the node, expects a full path name
  61. *
  62. * @param ISystemTag $tag system tag
  63. * @param IUser $user user
  64. * @param bool $isAdmin whether to allow operations for admins
  65. * @param ISystemTagManager $tagManager tag manager
  66. */
  67. public function __construct(ISystemTag $tag, IUser $user, $isAdmin, ISystemTagManager $tagManager) {
  68. $this->tag = $tag;
  69. $this->user = $user;
  70. $this->isAdmin = $isAdmin;
  71. $this->tagManager = $tagManager;
  72. }
  73. /**
  74. * Returns the id of the tag
  75. *
  76. * @return string
  77. */
  78. public function getName() {
  79. return $this->tag->getId();
  80. }
  81. /**
  82. * Returns the system tag represented by this node
  83. *
  84. * @return ISystemTag system tag
  85. */
  86. public function getSystemTag() {
  87. return $this->tag;
  88. }
  89. /**
  90. * Renames the node
  91. *
  92. * @param string $name The new name
  93. *
  94. * @throws MethodNotAllowed not allowed to rename node
  95. */
  96. public function setName($name) {
  97. throw new MethodNotAllowed();
  98. }
  99. /**
  100. * Update tag
  101. *
  102. * @param string $name new tag name
  103. * @param bool $userVisible user visible
  104. * @param bool $userAssignable user assignable
  105. * @throws NotFound whenever the given tag id does not exist
  106. * @throws Forbidden whenever there is no permission to update said tag
  107. * @throws Conflict whenever a tag already exists with the given attributes
  108. */
  109. public function update($name, $userVisible, $userAssignable) {
  110. try {
  111. if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
  112. throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
  113. }
  114. if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) {
  115. throw new Forbidden('No permission to update tag ' . $this->tag->getId());
  116. }
  117. // only admin is able to change permissions, regular users can only rename
  118. if (!$this->isAdmin) {
  119. // only renaming is allowed for regular users
  120. if ($userVisible !== $this->tag->isUserVisible()
  121. || $userAssignable !== $this->tag->isUserAssignable()
  122. ) {
  123. throw new Forbidden('No permission to update permissions for tag ' . $this->tag->getId());
  124. }
  125. }
  126. $this->tagManager->updateTag($this->tag->getId(), $name, $userVisible, $userAssignable);
  127. } catch (TagNotFoundException $e) {
  128. throw new NotFound('Tag with id ' . $this->tag->getId() . ' does not exist');
  129. } catch (TagAlreadyExistsException $e) {
  130. throw new Conflict(
  131. 'Tag with the properties "' . $name . '", ' .
  132. $userVisible . ', ' . $userAssignable . ' already exists'
  133. );
  134. }
  135. }
  136. /**
  137. * Returns null, not supported
  138. *
  139. */
  140. public function getLastModified() {
  141. return null;
  142. }
  143. public function delete() {
  144. try {
  145. if (!$this->isAdmin) {
  146. throw new Forbidden('No permission to delete tag ' . $this->tag->getId());
  147. }
  148. if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
  149. throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found');
  150. }
  151. $this->tagManager->deleteTags($this->tag->getId());
  152. } catch (TagNotFoundException $e) {
  153. // can happen if concurrent deletion occurred
  154. throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found', 0, $e);
  155. }
  156. }
  157. }