@@ -42,168 +42,168 @@ |
||
| 42 | 42 | use OC\Files\Storage\Wrapper\Wrapper; |
| 43 | 43 | |
| 44 | 44 | class FileSystemTags implements ICheck, IFileCheck { |
| 45 | - use TFileCheck; |
|
| 46 | - |
|
| 47 | - /** @var array */ |
|
| 48 | - protected $fileIds; |
|
| 49 | - |
|
| 50 | - /** @var array */ |
|
| 51 | - protected $fileSystemTags; |
|
| 52 | - |
|
| 53 | - /** @var IL10N */ |
|
| 54 | - protected $l; |
|
| 55 | - |
|
| 56 | - /** @var ISystemTagManager */ |
|
| 57 | - protected $systemTagManager; |
|
| 58 | - |
|
| 59 | - /** @var ISystemTagObjectMapper */ |
|
| 60 | - protected $systemTagObjectMapper; |
|
| 61 | - /** @var IUserSession */ |
|
| 62 | - protected $userSession; |
|
| 63 | - /** @var IGroupManager */ |
|
| 64 | - protected $groupManager; |
|
| 65 | - |
|
| 66 | - public function __construct( |
|
| 67 | - IL10N $l, |
|
| 68 | - ISystemTagManager $systemTagManager, |
|
| 69 | - ISystemTagObjectMapper $systemTagObjectMapper, |
|
| 70 | - IUserSession $userSession, |
|
| 71 | - IGroupManager $groupManager |
|
| 72 | - ) { |
|
| 73 | - $this->l = $l; |
|
| 74 | - $this->systemTagManager = $systemTagManager; |
|
| 75 | - $this->systemTagObjectMapper = $systemTagObjectMapper; |
|
| 76 | - $this->userSession = $userSession; |
|
| 77 | - $this->groupManager = $groupManager; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param string $operator |
|
| 82 | - * @param string $value |
|
| 83 | - * @return bool |
|
| 84 | - */ |
|
| 85 | - public function executeCheck($operator, $value) { |
|
| 86 | - $systemTags = $this->getSystemTags(); |
|
| 87 | - return ($operator === 'is') === in_array($value, $systemTags); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $operator |
|
| 92 | - * @param string $value |
|
| 93 | - * @throws \UnexpectedValueException |
|
| 94 | - */ |
|
| 95 | - public function validateCheck($operator, $value) { |
|
| 96 | - if (!in_array($operator, ['is', '!is'])) { |
|
| 97 | - throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - try { |
|
| 101 | - $tags = $this->systemTagManager->getTagsByIds($value); |
|
| 102 | - |
|
| 103 | - $user = $this->userSession->getUser(); |
|
| 104 | - $isAdmin = $user instanceof IUser && $this->groupManager->isAdmin($user->getUID()); |
|
| 105 | - |
|
| 106 | - if (!$isAdmin) { |
|
| 107 | - foreach ($tags as $tag) { |
|
| 108 | - if (!$tag->isUserVisible()) { |
|
| 109 | - throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 4); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - } catch (TagNotFoundException $e) { |
|
| 114 | - throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2); |
|
| 115 | - } catch (\InvalidArgumentException $e) { |
|
| 116 | - throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Get the ids of the assigned system tags |
|
| 122 | - * @return string[] |
|
| 123 | - */ |
|
| 124 | - protected function getSystemTags() { |
|
| 125 | - $cache = $this->storage->getCache(); |
|
| 126 | - $fileIds = $this->getFileIds($cache, $this->path, !$this->storage->instanceOfStorage(IHomeStorage::class) || $this->storage->instanceOfStorage(SharedStorage::class)); |
|
| 127 | - |
|
| 128 | - $systemTags = []; |
|
| 129 | - foreach ($fileIds as $i => $fileId) { |
|
| 130 | - if (isset($this->fileSystemTags[$fileId])) { |
|
| 131 | - $systemTags[] = $this->fileSystemTags[$fileId]; |
|
| 132 | - unset($fileIds[$i]); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - if (!empty($fileIds)) { |
|
| 137 | - $mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files'); |
|
| 138 | - foreach ($mappedSystemTags as $fileId => $fileSystemTags) { |
|
| 139 | - $this->fileSystemTags[$fileId] = $fileSystemTags; |
|
| 140 | - $systemTags[] = $fileSystemTags; |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - $systemTags = call_user_func_array('array_merge', $systemTags); |
|
| 145 | - $systemTags = array_unique($systemTags); |
|
| 146 | - return $systemTags; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Get the file ids of the given path and its parents |
|
| 151 | - * @param ICache $cache |
|
| 152 | - * @param string $path |
|
| 153 | - * @param bool $isExternalStorage |
|
| 154 | - * @return int[] |
|
| 155 | - */ |
|
| 156 | - protected function getFileIds(ICache $cache, $path, $isExternalStorage) { |
|
| 157 | - /** @psalm-suppress InvalidArgument */ |
|
| 158 | - if ($this->storage->instanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class)) { |
|
| 159 | - // Special implementation for groupfolder since all groupfolders share the same storage |
|
| 160 | - // id so add the group folder id in the cache key too. |
|
| 161 | - $groupFolderStorage = $this->storage; |
|
| 162 | - if ($this->storage instanceof Wrapper) { |
|
| 163 | - $groupFolderStorage = $this->storage->getInstanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class); |
|
| 164 | - } |
|
| 165 | - if ($groupFolderStorage === null) { |
|
| 166 | - throw new \LogicException('Should not happen: Storage is instance of GroupFolderStorage but no group folder storage found while unwrapping.'); |
|
| 167 | - } |
|
| 168 | - /** |
|
| 169 | - * @psalm-suppress UndefinedDocblockClass |
|
| 170 | - * @psalm-suppress UndefinedInterfaceMethod |
|
| 171 | - */ |
|
| 172 | - $cacheId = $cache->getNumericStorageId() . '/' . $groupFolderStorage->getFolderId(); |
|
| 173 | - } else { |
|
| 174 | - $cacheId = $cache->getNumericStorageId(); |
|
| 175 | - } |
|
| 176 | - if (isset($this->fileIds[$cacheId][$path])) { |
|
| 177 | - return $this->fileIds[$cacheId][$path]; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $parentIds = []; |
|
| 181 | - if ($path !== $this->dirname($path)) { |
|
| 182 | - $parentIds = $this->getFileIds($cache, $this->dirname($path), $isExternalStorage); |
|
| 183 | - } elseif (!$isExternalStorage) { |
|
| 184 | - return []; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - $fileId = $cache->getId($path); |
|
| 188 | - if ($fileId !== -1) { |
|
| 189 | - $parentIds[] = $fileId; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - $this->fileIds[$cacheId][$path] = $parentIds; |
|
| 193 | - |
|
| 194 | - return $parentIds; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - protected function dirname($path) { |
|
| 198 | - $dir = dirname($path); |
|
| 199 | - return $dir === '.' ? '' : $dir; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - public function supportedEntities(): array { |
|
| 203 | - return [ File::class ]; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - public function isAvailableForScope(int $scope): bool { |
|
| 207 | - return true; |
|
| 208 | - } |
|
| 45 | + use TFileCheck; |
|
| 46 | + |
|
| 47 | + /** @var array */ |
|
| 48 | + protected $fileIds; |
|
| 49 | + |
|
| 50 | + /** @var array */ |
|
| 51 | + protected $fileSystemTags; |
|
| 52 | + |
|
| 53 | + /** @var IL10N */ |
|
| 54 | + protected $l; |
|
| 55 | + |
|
| 56 | + /** @var ISystemTagManager */ |
|
| 57 | + protected $systemTagManager; |
|
| 58 | + |
|
| 59 | + /** @var ISystemTagObjectMapper */ |
|
| 60 | + protected $systemTagObjectMapper; |
|
| 61 | + /** @var IUserSession */ |
|
| 62 | + protected $userSession; |
|
| 63 | + /** @var IGroupManager */ |
|
| 64 | + protected $groupManager; |
|
| 65 | + |
|
| 66 | + public function __construct( |
|
| 67 | + IL10N $l, |
|
| 68 | + ISystemTagManager $systemTagManager, |
|
| 69 | + ISystemTagObjectMapper $systemTagObjectMapper, |
|
| 70 | + IUserSession $userSession, |
|
| 71 | + IGroupManager $groupManager |
|
| 72 | + ) { |
|
| 73 | + $this->l = $l; |
|
| 74 | + $this->systemTagManager = $systemTagManager; |
|
| 75 | + $this->systemTagObjectMapper = $systemTagObjectMapper; |
|
| 76 | + $this->userSession = $userSession; |
|
| 77 | + $this->groupManager = $groupManager; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param string $operator |
|
| 82 | + * @param string $value |
|
| 83 | + * @return bool |
|
| 84 | + */ |
|
| 85 | + public function executeCheck($operator, $value) { |
|
| 86 | + $systemTags = $this->getSystemTags(); |
|
| 87 | + return ($operator === 'is') === in_array($value, $systemTags); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $operator |
|
| 92 | + * @param string $value |
|
| 93 | + * @throws \UnexpectedValueException |
|
| 94 | + */ |
|
| 95 | + public function validateCheck($operator, $value) { |
|
| 96 | + if (!in_array($operator, ['is', '!is'])) { |
|
| 97 | + throw new \UnexpectedValueException($this->l->t('The given operator is invalid'), 1); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + try { |
|
| 101 | + $tags = $this->systemTagManager->getTagsByIds($value); |
|
| 102 | + |
|
| 103 | + $user = $this->userSession->getUser(); |
|
| 104 | + $isAdmin = $user instanceof IUser && $this->groupManager->isAdmin($user->getUID()); |
|
| 105 | + |
|
| 106 | + if (!$isAdmin) { |
|
| 107 | + foreach ($tags as $tag) { |
|
| 108 | + if (!$tag->isUserVisible()) { |
|
| 109 | + throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 4); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + } catch (TagNotFoundException $e) { |
|
| 114 | + throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 2); |
|
| 115 | + } catch (\InvalidArgumentException $e) { |
|
| 116 | + throw new \UnexpectedValueException($this->l->t('The given tag id is invalid'), 3); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Get the ids of the assigned system tags |
|
| 122 | + * @return string[] |
|
| 123 | + */ |
|
| 124 | + protected function getSystemTags() { |
|
| 125 | + $cache = $this->storage->getCache(); |
|
| 126 | + $fileIds = $this->getFileIds($cache, $this->path, !$this->storage->instanceOfStorage(IHomeStorage::class) || $this->storage->instanceOfStorage(SharedStorage::class)); |
|
| 127 | + |
|
| 128 | + $systemTags = []; |
|
| 129 | + foreach ($fileIds as $i => $fileId) { |
|
| 130 | + if (isset($this->fileSystemTags[$fileId])) { |
|
| 131 | + $systemTags[] = $this->fileSystemTags[$fileId]; |
|
| 132 | + unset($fileIds[$i]); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + if (!empty($fileIds)) { |
|
| 137 | + $mappedSystemTags = $this->systemTagObjectMapper->getTagIdsForObjects($fileIds, 'files'); |
|
| 138 | + foreach ($mappedSystemTags as $fileId => $fileSystemTags) { |
|
| 139 | + $this->fileSystemTags[$fileId] = $fileSystemTags; |
|
| 140 | + $systemTags[] = $fileSystemTags; |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + $systemTags = call_user_func_array('array_merge', $systemTags); |
|
| 145 | + $systemTags = array_unique($systemTags); |
|
| 146 | + return $systemTags; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Get the file ids of the given path and its parents |
|
| 151 | + * @param ICache $cache |
|
| 152 | + * @param string $path |
|
| 153 | + * @param bool $isExternalStorage |
|
| 154 | + * @return int[] |
|
| 155 | + */ |
|
| 156 | + protected function getFileIds(ICache $cache, $path, $isExternalStorage) { |
|
| 157 | + /** @psalm-suppress InvalidArgument */ |
|
| 158 | + if ($this->storage->instanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class)) { |
|
| 159 | + // Special implementation for groupfolder since all groupfolders share the same storage |
|
| 160 | + // id so add the group folder id in the cache key too. |
|
| 161 | + $groupFolderStorage = $this->storage; |
|
| 162 | + if ($this->storage instanceof Wrapper) { |
|
| 163 | + $groupFolderStorage = $this->storage->getInstanceOfStorage(\OCA\GroupFolders\Mount\GroupFolderStorage::class); |
|
| 164 | + } |
|
| 165 | + if ($groupFolderStorage === null) { |
|
| 166 | + throw new \LogicException('Should not happen: Storage is instance of GroupFolderStorage but no group folder storage found while unwrapping.'); |
|
| 167 | + } |
|
| 168 | + /** |
|
| 169 | + * @psalm-suppress UndefinedDocblockClass |
|
| 170 | + * @psalm-suppress UndefinedInterfaceMethod |
|
| 171 | + */ |
|
| 172 | + $cacheId = $cache->getNumericStorageId() . '/' . $groupFolderStorage->getFolderId(); |
|
| 173 | + } else { |
|
| 174 | + $cacheId = $cache->getNumericStorageId(); |
|
| 175 | + } |
|
| 176 | + if (isset($this->fileIds[$cacheId][$path])) { |
|
| 177 | + return $this->fileIds[$cacheId][$path]; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $parentIds = []; |
|
| 181 | + if ($path !== $this->dirname($path)) { |
|
| 182 | + $parentIds = $this->getFileIds($cache, $this->dirname($path), $isExternalStorage); |
|
| 183 | + } elseif (!$isExternalStorage) { |
|
| 184 | + return []; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + $fileId = $cache->getId($path); |
|
| 188 | + if ($fileId !== -1) { |
|
| 189 | + $parentIds[] = $fileId; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + $this->fileIds[$cacheId][$path] = $parentIds; |
|
| 193 | + |
|
| 194 | + return $parentIds; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + protected function dirname($path) { |
|
| 198 | + $dir = dirname($path); |
|
| 199 | + return $dir === '.' ? '' : $dir; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + public function supportedEntities(): array { |
|
| 203 | + return [ File::class ]; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + public function isAvailableForScope(int $scope): bool { |
|
| 207 | + return true; |
|
| 208 | + } |
|
| 209 | 209 | } |