1<?php 2/** 3 * @copyright Copyright (c) 2016, ownCloud, Inc. 4 * 5 * @author Christoph Wurst <christoph@winzerhof-wurst.at> 6 * @author Roeland Jago Douma <roeland@famdouma.nl> 7 * @author Thomas Müller <thomas.mueller@tmit.eu> 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 */ 25namespace OCA\DAV\SystemTag; 26 27use OCP\IGroupManager; 28use OCP\IUserSession; 29use OCP\SystemTag\ISystemTag; 30use OCP\SystemTag\ISystemTagManager; 31use OCP\SystemTag\TagNotFoundException; 32use Sabre\DAV\Exception\BadRequest; 33use Sabre\DAV\Exception\Forbidden; 34use Sabre\DAV\Exception\NotFound; 35use Sabre\DAV\ICollection; 36 37class SystemTagsByIdCollection implements ICollection { 38 39 /** 40 * @var ISystemTagManager 41 */ 42 private $tagManager; 43 44 /** 45 * @var IGroupManager 46 */ 47 private $groupManager; 48 49 /** 50 * @var IUserSession 51 */ 52 private $userSession; 53 54 /** 55 * SystemTagsByIdCollection constructor. 56 * 57 * @param ISystemTagManager $tagManager 58 * @param IUserSession $userSession 59 * @param IGroupManager $groupManager 60 */ 61 public function __construct( 62 ISystemTagManager $tagManager, 63 IUserSession $userSession, 64 IGroupManager $groupManager 65 ) { 66 $this->tagManager = $tagManager; 67 $this->userSession = $userSession; 68 $this->groupManager = $groupManager; 69 } 70 71 /** 72 * Returns whether the currently logged in user is an administrator 73 * 74 * @return bool true if the user is an admin 75 */ 76 private function isAdmin() { 77 $user = $this->userSession->getUser(); 78 if ($user !== null) { 79 return $this->groupManager->isAdmin($user->getUID()); 80 } 81 return false; 82 } 83 84 /** 85 * @param string $name 86 * @param resource|string $data Initial payload 87 * @throws Forbidden 88 */ 89 public function createFile($name, $data = null) { 90 throw new Forbidden('Cannot create tags by id'); 91 } 92 93 /** 94 * @param string $name 95 */ 96 public function createDirectory($name) { 97 throw new Forbidden('Permission denied to create collections'); 98 } 99 100 /** 101 * @param string $name 102 */ 103 public function getChild($name) { 104 try { 105 $tag = $this->tagManager->getTagsByIds([$name]); 106 $tag = current($tag); 107 if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { 108 throw new NotFound('Tag with id ' . $name . ' not found'); 109 } 110 return $this->makeNode($tag); 111 } catch (\InvalidArgumentException $e) { 112 throw new BadRequest('Invalid tag id', 0, $e); 113 } catch (TagNotFoundException $e) { 114 throw new NotFound('Tag with id ' . $name . ' not found', 0, $e); 115 } 116 } 117 118 public function getChildren() { 119 $visibilityFilter = true; 120 if ($this->isAdmin()) { 121 $visibilityFilter = null; 122 } 123 124 $tags = $this->tagManager->getAllTags($visibilityFilter); 125 return array_map(function ($tag) { 126 return $this->makeNode($tag); 127 }, $tags); 128 } 129 130 /** 131 * @param string $name 132 */ 133 public function childExists($name) { 134 try { 135 $tag = $this->tagManager->getTagsByIds([$name]); 136 $tag = current($tag); 137 if (!$this->tagManager->canUserSeeTag($tag, $this->userSession->getUser())) { 138 return false; 139 } 140 return true; 141 } catch (\InvalidArgumentException $e) { 142 throw new BadRequest('Invalid tag id', 0, $e); 143 } catch (TagNotFoundException $e) { 144 return false; 145 } 146 } 147 148 public function delete() { 149 throw new Forbidden('Permission denied to delete this collection'); 150 } 151 152 public function getName() { 153 return 'systemtags'; 154 } 155 156 public function setName($name) { 157 throw new Forbidden('Permission denied to rename this collection'); 158 } 159 160 /** 161 * Returns the last modification time, as a unix timestamp 162 * 163 * @return int 164 */ 165 public function getLastModified() { 166 return null; 167 } 168 169 /** 170 * Create a sabre node for the given system tag 171 * 172 * @param ISystemTag $tag 173 * 174 * @return SystemTagNode 175 */ 176 private function makeNode(ISystemTag $tag) { 177 return new SystemTagNode($tag, $this->userSession->getUser(), $this->isAdmin(), $this->tagManager); 178 } 179} 180