|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright (c) 2023 Arthur Schiwon <blizzz@arthur-schiwon.de> |
| 7 | + * |
| 8 | + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 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 |
| 23 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | + |
| 27 | +namespace OCA\DAV\SystemTag; |
| 28 | + |
| 29 | +use OC\SystemTag\SystemTag; |
| 30 | +use OC\SystemTag\SystemTagsInFilesDetector; |
| 31 | +use OC\User\NoUserException; |
| 32 | +use OCP\Files\IRootFolder; |
| 33 | +use OCP\Files\NotPermittedException; |
| 34 | +use OCP\IUserSession; |
| 35 | +use OCP\SystemTag\ISystemTagManager; |
| 36 | +use Sabre\DAV\Exception\Forbidden; |
| 37 | +use Sabre\DAV\Exception\NotFound; |
| 38 | +use Sabre\DAV\SimpleCollection; |
| 39 | + |
| 40 | +class SystemTagsInUseCollection extends SimpleCollection { |
| 41 | + protected IUserSession $userSession; |
| 42 | + protected IRootFolder $rootFolder; |
| 43 | + protected string $mediaType; |
| 44 | + protected ISystemTagManager $systemTagManager; |
| 45 | + protected SystemTagsInFilesDetector $systemTagsInFilesDetector; |
| 46 | + |
| 47 | + /** @noinspection PhpMissingParentConstructorInspection */ |
| 48 | + public function __construct( |
| 49 | + IUserSession $userSession, |
| 50 | + IRootFolder $rootFolder, |
| 51 | + ISystemTagManager $systemTagManager, |
| 52 | + SystemTagsInFilesDetector $systemTagsInFilesDetector, |
| 53 | + string $mediaType = '' |
| 54 | + ) { |
| 55 | + $this->userSession = $userSession; |
| 56 | + $this->rootFolder = $rootFolder; |
| 57 | + $this->systemTagManager = $systemTagManager; |
| 58 | + $this->mediaType = $mediaType; |
| 59 | + $this->systemTagsInFilesDetector = $systemTagsInFilesDetector; |
| 60 | + $this->name = 'systemtags-assigned'; |
| 61 | + if ($this->mediaType != '') { |
| 62 | + $this->name .= '/' . $this->mediaType; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public function setName($name): void { |
| 67 | + throw new Forbidden('Permission denied to rename this collection'); |
| 68 | + } |
| 69 | + |
| 70 | + public function getChild($name): self { |
| 71 | + if ($this->mediaType !== '') { |
| 72 | + throw new NotFound('Invalid media type'); |
| 73 | + } |
| 74 | + return new self($this->userSession, $this->rootFolder, $this->systemTagManager, $this->systemTagsInFilesDetector, $name); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @return SystemTagNode[] |
| 79 | + * @throws NotPermittedException |
| 80 | + * @throws Forbidden |
| 81 | + */ |
| 82 | + public function getChildren(): array { |
| 83 | + $user = $this->userSession->getUser(); |
| 84 | + $userFolder = null; |
| 85 | + try { |
| 86 | + if ($user) { |
| 87 | + $userFolder = $this->rootFolder->getUserFolder($user->getUID()); |
| 88 | + } |
| 89 | + } catch (NoUserException) { |
| 90 | + // will throw a Sabre exception in the next step. |
| 91 | + } |
| 92 | + if ($user === null || $userFolder === null) { |
| 93 | + throw new Forbidden('Permission denied to read this collection'); |
| 94 | + } |
| 95 | + |
| 96 | + $result = $this->systemTagsInFilesDetector->detectAssignedSystemTagsIn($userFolder, $this->mediaType); |
| 97 | + $children = []; |
| 98 | + foreach ($result as $tagData) { |
| 99 | + $tag = new SystemTag((string)$tagData['id'], $tagData['name'], (bool)$tagData['visibility'], (bool)$tagData['editable']); |
| 100 | + // read only, so we can submit the isAdmin parameter as false generally |
| 101 | + $node = new SystemTagNode($tag, $user, false, $this->systemTagManager); |
| 102 | + $node->setNumberOfFiles($tagData['number_files']); |
| 103 | + $node->setReferenceFileId($tagData['ref_file_id']); |
| 104 | + $children[] = $node; |
| 105 | + } |
| 106 | + return $children; |
| 107 | + } |
| 108 | +} |
0 commit comments