@@ -39,370 +39,370 @@ |
||
| 39 | 39 | use OCP\IUser; |
| 40 | 40 | |
| 41 | 41 | class Util { |
| 42 | - public const HEADER_START = 'HBEGIN'; |
|
| 43 | - public const HEADER_END = 'HEND'; |
|
| 44 | - public const HEADER_PADDING_CHAR = '-'; |
|
| 45 | - |
|
| 46 | - public const HEADER_ENCRYPTION_MODULE_KEY = 'oc_encryption_module'; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * block size will always be 8192 for a PHP stream |
|
| 50 | - * @see https://bugs.php.net/bug.php?id=21641 |
|
| 51 | - * @var integer |
|
| 52 | - */ |
|
| 53 | - protected $headerSize = 8192; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * block size will always be 8192 for a PHP stream |
|
| 57 | - * @see https://bugs.php.net/bug.php?id=21641 |
|
| 58 | - * @var integer |
|
| 59 | - */ |
|
| 60 | - protected $blockSize = 8192; |
|
| 61 | - |
|
| 62 | - /** @var View */ |
|
| 63 | - protected $rootView; |
|
| 64 | - |
|
| 65 | - /** @var array */ |
|
| 66 | - protected $ocHeaderKeys; |
|
| 67 | - |
|
| 68 | - /** @var \OC\User\Manager */ |
|
| 69 | - protected $userManager; |
|
| 70 | - |
|
| 71 | - /** @var IConfig */ |
|
| 72 | - protected $config; |
|
| 73 | - |
|
| 74 | - /** @var array paths excluded from encryption */ |
|
| 75 | - protected $excludedPaths; |
|
| 76 | - |
|
| 77 | - /** @var \OC\Group\Manager $manager */ |
|
| 78 | - protected $groupManager; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * |
|
| 82 | - * @param View $rootView |
|
| 83 | - * @param \OC\User\Manager $userManager |
|
| 84 | - * @param \OC\Group\Manager $groupManager |
|
| 85 | - * @param IConfig $config |
|
| 86 | - */ |
|
| 87 | - public function __construct( |
|
| 88 | - View $rootView, |
|
| 89 | - \OC\User\Manager $userManager, |
|
| 90 | - \OC\Group\Manager $groupManager, |
|
| 91 | - IConfig $config) { |
|
| 92 | - $this->ocHeaderKeys = [ |
|
| 93 | - self::HEADER_ENCRYPTION_MODULE_KEY |
|
| 94 | - ]; |
|
| 95 | - |
|
| 96 | - $this->rootView = $rootView; |
|
| 97 | - $this->userManager = $userManager; |
|
| 98 | - $this->groupManager = $groupManager; |
|
| 99 | - $this->config = $config; |
|
| 100 | - |
|
| 101 | - $this->excludedPaths[] = 'files_encryption'; |
|
| 102 | - $this->excludedPaths[] = 'appdata_' . $config->getSystemValue('instanceid', null); |
|
| 103 | - $this->excludedPaths[] = 'files_external'; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * read encryption module ID from header |
|
| 108 | - * |
|
| 109 | - * @param array $header |
|
| 110 | - * @return string |
|
| 111 | - * @throws ModuleDoesNotExistsException |
|
| 112 | - */ |
|
| 113 | - public function getEncryptionModuleId(array $header = null) { |
|
| 114 | - $id = ''; |
|
| 115 | - $encryptionModuleKey = self::HEADER_ENCRYPTION_MODULE_KEY; |
|
| 116 | - |
|
| 117 | - if (isset($header[$encryptionModuleKey])) { |
|
| 118 | - $id = $header[$encryptionModuleKey]; |
|
| 119 | - } elseif (isset($header['cipher'])) { |
|
| 120 | - if (class_exists('\OCA\Encryption\Crypto\Encryption')) { |
|
| 121 | - // fall back to default encryption if the user migrated from |
|
| 122 | - // ownCloud <= 8.0 with the old encryption |
|
| 123 | - $id = \OCA\Encryption\Crypto\Encryption::ID; |
|
| 124 | - } else { |
|
| 125 | - throw new ModuleDoesNotExistsException('Default encryption module missing'); |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - return $id; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * create header for encrypted file |
|
| 134 | - * |
|
| 135 | - * @param array $headerData |
|
| 136 | - * @param IEncryptionModule $encryptionModule |
|
| 137 | - * @return string |
|
| 138 | - * @throws EncryptionHeaderToLargeException if header has to many arguments |
|
| 139 | - * @throws EncryptionHeaderKeyExistsException if header key is already in use |
|
| 140 | - */ |
|
| 141 | - public function createHeader(array $headerData, IEncryptionModule $encryptionModule) { |
|
| 142 | - $header = self::HEADER_START . ':' . self::HEADER_ENCRYPTION_MODULE_KEY . ':' . $encryptionModule->getId() . ':'; |
|
| 143 | - foreach ($headerData as $key => $value) { |
|
| 144 | - if (in_array($key, $this->ocHeaderKeys)) { |
|
| 145 | - throw new EncryptionHeaderKeyExistsException($key); |
|
| 146 | - } |
|
| 147 | - $header .= $key . ':' . $value . ':'; |
|
| 148 | - } |
|
| 149 | - $header .= self::HEADER_END; |
|
| 150 | - |
|
| 151 | - if (strlen($header) > $this->getHeaderSize()) { |
|
| 152 | - throw new EncryptionHeaderToLargeException(); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $paddedHeader = str_pad($header, $this->headerSize, self::HEADER_PADDING_CHAR, STR_PAD_RIGHT); |
|
| 156 | - |
|
| 157 | - return $paddedHeader; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * go recursively through a dir and collect all files and sub files. |
|
| 162 | - * |
|
| 163 | - * @param string $dir relative to the users files folder |
|
| 164 | - * @return array with list of files relative to the users files folder |
|
| 165 | - */ |
|
| 166 | - public function getAllFiles($dir) { |
|
| 167 | - $result = []; |
|
| 168 | - $dirList = [$dir]; |
|
| 169 | - |
|
| 170 | - while ($dirList) { |
|
| 171 | - $dir = array_pop($dirList); |
|
| 172 | - $content = $this->rootView->getDirectoryContent($dir); |
|
| 173 | - |
|
| 174 | - foreach ($content as $c) { |
|
| 175 | - if ($c->getType() === 'dir') { |
|
| 176 | - $dirList[] = $c->getPath(); |
|
| 177 | - } else { |
|
| 178 | - $result[] = $c->getPath(); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - return $result; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * check if it is a file uploaded by the user stored in data/user/files |
|
| 188 | - * or a metadata file |
|
| 189 | - * |
|
| 190 | - * @param string $path relative to the data/ folder |
|
| 191 | - * @return boolean |
|
| 192 | - */ |
|
| 193 | - public function isFile($path) { |
|
| 194 | - $parts = explode('/', Filesystem::normalizePath($path), 4); |
|
| 195 | - if (isset($parts[2]) && $parts[2] === 'files') { |
|
| 196 | - return true; |
|
| 197 | - } |
|
| 198 | - return false; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * return size of encryption header |
|
| 203 | - * |
|
| 204 | - * @return integer |
|
| 205 | - */ |
|
| 206 | - public function getHeaderSize() { |
|
| 207 | - return $this->headerSize; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * return size of block read by a PHP stream |
|
| 212 | - * |
|
| 213 | - * @return integer |
|
| 214 | - */ |
|
| 215 | - public function getBlockSize() { |
|
| 216 | - return $this->blockSize; |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * get the owner and the path for the file relative to the owners files folder |
|
| 221 | - * |
|
| 222 | - * @param string $path |
|
| 223 | - * @return array |
|
| 224 | - * @throws \BadMethodCallException |
|
| 225 | - */ |
|
| 226 | - public function getUidAndFilename($path) { |
|
| 227 | - $parts = explode('/', $path); |
|
| 228 | - $uid = ''; |
|
| 229 | - if (count($parts) > 2) { |
|
| 230 | - $uid = $parts[1]; |
|
| 231 | - } |
|
| 232 | - if (!$this->userManager->userExists($uid)) { |
|
| 233 | - throw new \BadMethodCallException( |
|
| 234 | - 'path needs to be relative to the system wide data folder and point to a user specific file' |
|
| 235 | - ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - $ownerPath = implode('/', array_slice($parts, 2)); |
|
| 239 | - |
|
| 240 | - return [$uid, Filesystem::normalizePath($ownerPath)]; |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Remove .path extension from a file path |
|
| 245 | - * @param string $path Path that may identify a .part file |
|
| 246 | - * @return string File path without .part extension |
|
| 247 | - * @note this is needed for reusing keys |
|
| 248 | - */ |
|
| 249 | - public function stripPartialFileExtension($path) { |
|
| 250 | - $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 251 | - |
|
| 252 | - if ($extension === 'part') { |
|
| 253 | - $newLength = strlen($path) - 5; // 5 = strlen(".part") |
|
| 254 | - $fPath = substr($path, 0, $newLength); |
|
| 255 | - |
|
| 256 | - // if path also contains a transaction id, we remove it too |
|
| 257 | - $extension = pathinfo($fPath, PATHINFO_EXTENSION); |
|
| 258 | - if (substr($extension, 0, 12) === 'ocTransferId') { // 12 = strlen("ocTransferId") |
|
| 259 | - $newLength = strlen($fPath) - strlen($extension) - 1; |
|
| 260 | - $fPath = substr($fPath, 0, $newLength); |
|
| 261 | - } |
|
| 262 | - return $fPath; |
|
| 263 | - } else { |
|
| 264 | - return $path; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - public function getUserWithAccessToMountPoint($users, $groups) { |
|
| 269 | - $result = []; |
|
| 270 | - if ($users === [] && $groups === []) { |
|
| 271 | - $users = $this->userManager->search('', null, null); |
|
| 272 | - $result = array_map(function (IUser $user) { |
|
| 273 | - return $user->getUID(); |
|
| 274 | - }, $users); |
|
| 275 | - } else { |
|
| 276 | - $result = array_merge($result, $users); |
|
| 277 | - |
|
| 278 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 279 | - foreach ($groups as $group) { |
|
| 280 | - $groupObject = $groupManager->get($group); |
|
| 281 | - if ($groupObject) { |
|
| 282 | - $foundUsers = $groupObject->searchUsers('', -1, 0); |
|
| 283 | - $userIds = []; |
|
| 284 | - foreach ($foundUsers as $user) { |
|
| 285 | - $userIds[] = $user->getUID(); |
|
| 286 | - } |
|
| 287 | - $result = array_merge($result, $userIds); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - return $result; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * check if the file is stored on a system wide mount point |
|
| 297 | - * @param string $path relative to /data/user with leading '/' |
|
| 298 | - * @param string $uid |
|
| 299 | - * @return boolean |
|
| 300 | - */ |
|
| 301 | - public function isSystemWideMountPoint($path, $uid) { |
|
| 302 | - if (\OCP\App::isEnabled("files_external")) { |
|
| 303 | - /** @var GlobalStoragesService $storageService */ |
|
| 304 | - $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
| 305 | - $storages = $storageService->getAllStorages(); |
|
| 306 | - foreach ($storages as $storage) { |
|
| 307 | - if (strpos($path, '/files/' . $storage->getMountPoint()) === 0) { |
|
| 308 | - if ($this->isMountPointApplicableToUser($storage, $uid)) { |
|
| 309 | - return true; |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * check if mount point is applicable to user |
|
| 319 | - * |
|
| 320 | - * @param StorageConfig $mount |
|
| 321 | - * @param string $uid |
|
| 322 | - * @return boolean |
|
| 323 | - */ |
|
| 324 | - private function isMountPointApplicableToUser(StorageConfig $mount, string $uid) { |
|
| 325 | - if ($mount->getApplicableUsers() === [] && $mount->getApplicableGroups() === []) { |
|
| 326 | - // applicable for everyone |
|
| 327 | - return true; |
|
| 328 | - } |
|
| 329 | - // check if mount point is applicable for the user |
|
| 330 | - if (array_search($uid, $mount->getApplicableUsers()) !== false) { |
|
| 331 | - return true; |
|
| 332 | - } |
|
| 333 | - // check if mount point is applicable for group where the user is a member |
|
| 334 | - foreach ($mount->getApplicableGroups() as $gid) { |
|
| 335 | - if ($this->groupManager->isInGroup($uid, $gid)) { |
|
| 336 | - return true; |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - return false; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * check if it is a path which is excluded by ownCloud from encryption |
|
| 344 | - * |
|
| 345 | - * @param string $path |
|
| 346 | - * @return boolean |
|
| 347 | - */ |
|
| 348 | - public function isExcluded($path) { |
|
| 349 | - $normalizedPath = Filesystem::normalizePath($path); |
|
| 350 | - $root = explode('/', $normalizedPath, 4); |
|
| 351 | - if (count($root) > 1) { |
|
| 352 | - |
|
| 353 | - // detect alternative key storage root |
|
| 354 | - $rootDir = $this->getKeyStorageRoot(); |
|
| 355 | - if ($rootDir !== '' && |
|
| 356 | - 0 === strpos( |
|
| 357 | - Filesystem::normalizePath($path), |
|
| 358 | - Filesystem::normalizePath($rootDir) |
|
| 359 | - ) |
|
| 360 | - ) { |
|
| 361 | - return true; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - |
|
| 365 | - //detect system wide folders |
|
| 366 | - if (in_array($root[1], $this->excludedPaths)) { |
|
| 367 | - return true; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - // detect user specific folders |
|
| 371 | - if ($this->userManager->userExists($root[1]) |
|
| 372 | - && in_array($root[2], $this->excludedPaths)) { |
|
| 373 | - return true; |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - return false; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * check if recovery key is enabled for user |
|
| 381 | - * |
|
| 382 | - * @param string $uid |
|
| 383 | - * @return boolean |
|
| 384 | - */ |
|
| 385 | - public function recoveryEnabled($uid) { |
|
| 386 | - $enabled = $this->config->getUserValue($uid, 'encryption', 'recovery_enabled', '0'); |
|
| 387 | - |
|
| 388 | - return $enabled === '1'; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * set new key storage root |
|
| 393 | - * |
|
| 394 | - * @param string $root new key store root relative to the data folder |
|
| 395 | - */ |
|
| 396 | - public function setKeyStorageRoot($root) { |
|
| 397 | - $this->config->setAppValue('core', 'encryption_key_storage_root', $root); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * get key storage root |
|
| 402 | - * |
|
| 403 | - * @return string key storage root |
|
| 404 | - */ |
|
| 405 | - public function getKeyStorageRoot() { |
|
| 406 | - return $this->config->getAppValue('core', 'encryption_key_storage_root', ''); |
|
| 407 | - } |
|
| 42 | + public const HEADER_START = 'HBEGIN'; |
|
| 43 | + public const HEADER_END = 'HEND'; |
|
| 44 | + public const HEADER_PADDING_CHAR = '-'; |
|
| 45 | + |
|
| 46 | + public const HEADER_ENCRYPTION_MODULE_KEY = 'oc_encryption_module'; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * block size will always be 8192 for a PHP stream |
|
| 50 | + * @see https://bugs.php.net/bug.php?id=21641 |
|
| 51 | + * @var integer |
|
| 52 | + */ |
|
| 53 | + protected $headerSize = 8192; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * block size will always be 8192 for a PHP stream |
|
| 57 | + * @see https://bugs.php.net/bug.php?id=21641 |
|
| 58 | + * @var integer |
|
| 59 | + */ |
|
| 60 | + protected $blockSize = 8192; |
|
| 61 | + |
|
| 62 | + /** @var View */ |
|
| 63 | + protected $rootView; |
|
| 64 | + |
|
| 65 | + /** @var array */ |
|
| 66 | + protected $ocHeaderKeys; |
|
| 67 | + |
|
| 68 | + /** @var \OC\User\Manager */ |
|
| 69 | + protected $userManager; |
|
| 70 | + |
|
| 71 | + /** @var IConfig */ |
|
| 72 | + protected $config; |
|
| 73 | + |
|
| 74 | + /** @var array paths excluded from encryption */ |
|
| 75 | + protected $excludedPaths; |
|
| 76 | + |
|
| 77 | + /** @var \OC\Group\Manager $manager */ |
|
| 78 | + protected $groupManager; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * |
|
| 82 | + * @param View $rootView |
|
| 83 | + * @param \OC\User\Manager $userManager |
|
| 84 | + * @param \OC\Group\Manager $groupManager |
|
| 85 | + * @param IConfig $config |
|
| 86 | + */ |
|
| 87 | + public function __construct( |
|
| 88 | + View $rootView, |
|
| 89 | + \OC\User\Manager $userManager, |
|
| 90 | + \OC\Group\Manager $groupManager, |
|
| 91 | + IConfig $config) { |
|
| 92 | + $this->ocHeaderKeys = [ |
|
| 93 | + self::HEADER_ENCRYPTION_MODULE_KEY |
|
| 94 | + ]; |
|
| 95 | + |
|
| 96 | + $this->rootView = $rootView; |
|
| 97 | + $this->userManager = $userManager; |
|
| 98 | + $this->groupManager = $groupManager; |
|
| 99 | + $this->config = $config; |
|
| 100 | + |
|
| 101 | + $this->excludedPaths[] = 'files_encryption'; |
|
| 102 | + $this->excludedPaths[] = 'appdata_' . $config->getSystemValue('instanceid', null); |
|
| 103 | + $this->excludedPaths[] = 'files_external'; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * read encryption module ID from header |
|
| 108 | + * |
|
| 109 | + * @param array $header |
|
| 110 | + * @return string |
|
| 111 | + * @throws ModuleDoesNotExistsException |
|
| 112 | + */ |
|
| 113 | + public function getEncryptionModuleId(array $header = null) { |
|
| 114 | + $id = ''; |
|
| 115 | + $encryptionModuleKey = self::HEADER_ENCRYPTION_MODULE_KEY; |
|
| 116 | + |
|
| 117 | + if (isset($header[$encryptionModuleKey])) { |
|
| 118 | + $id = $header[$encryptionModuleKey]; |
|
| 119 | + } elseif (isset($header['cipher'])) { |
|
| 120 | + if (class_exists('\OCA\Encryption\Crypto\Encryption')) { |
|
| 121 | + // fall back to default encryption if the user migrated from |
|
| 122 | + // ownCloud <= 8.0 with the old encryption |
|
| 123 | + $id = \OCA\Encryption\Crypto\Encryption::ID; |
|
| 124 | + } else { |
|
| 125 | + throw new ModuleDoesNotExistsException('Default encryption module missing'); |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + return $id; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * create header for encrypted file |
|
| 134 | + * |
|
| 135 | + * @param array $headerData |
|
| 136 | + * @param IEncryptionModule $encryptionModule |
|
| 137 | + * @return string |
|
| 138 | + * @throws EncryptionHeaderToLargeException if header has to many arguments |
|
| 139 | + * @throws EncryptionHeaderKeyExistsException if header key is already in use |
|
| 140 | + */ |
|
| 141 | + public function createHeader(array $headerData, IEncryptionModule $encryptionModule) { |
|
| 142 | + $header = self::HEADER_START . ':' . self::HEADER_ENCRYPTION_MODULE_KEY . ':' . $encryptionModule->getId() . ':'; |
|
| 143 | + foreach ($headerData as $key => $value) { |
|
| 144 | + if (in_array($key, $this->ocHeaderKeys)) { |
|
| 145 | + throw new EncryptionHeaderKeyExistsException($key); |
|
| 146 | + } |
|
| 147 | + $header .= $key . ':' . $value . ':'; |
|
| 148 | + } |
|
| 149 | + $header .= self::HEADER_END; |
|
| 150 | + |
|
| 151 | + if (strlen($header) > $this->getHeaderSize()) { |
|
| 152 | + throw new EncryptionHeaderToLargeException(); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $paddedHeader = str_pad($header, $this->headerSize, self::HEADER_PADDING_CHAR, STR_PAD_RIGHT); |
|
| 156 | + |
|
| 157 | + return $paddedHeader; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * go recursively through a dir and collect all files and sub files. |
|
| 162 | + * |
|
| 163 | + * @param string $dir relative to the users files folder |
|
| 164 | + * @return array with list of files relative to the users files folder |
|
| 165 | + */ |
|
| 166 | + public function getAllFiles($dir) { |
|
| 167 | + $result = []; |
|
| 168 | + $dirList = [$dir]; |
|
| 169 | + |
|
| 170 | + while ($dirList) { |
|
| 171 | + $dir = array_pop($dirList); |
|
| 172 | + $content = $this->rootView->getDirectoryContent($dir); |
|
| 173 | + |
|
| 174 | + foreach ($content as $c) { |
|
| 175 | + if ($c->getType() === 'dir') { |
|
| 176 | + $dirList[] = $c->getPath(); |
|
| 177 | + } else { |
|
| 178 | + $result[] = $c->getPath(); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + return $result; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * check if it is a file uploaded by the user stored in data/user/files |
|
| 188 | + * or a metadata file |
|
| 189 | + * |
|
| 190 | + * @param string $path relative to the data/ folder |
|
| 191 | + * @return boolean |
|
| 192 | + */ |
|
| 193 | + public function isFile($path) { |
|
| 194 | + $parts = explode('/', Filesystem::normalizePath($path), 4); |
|
| 195 | + if (isset($parts[2]) && $parts[2] === 'files') { |
|
| 196 | + return true; |
|
| 197 | + } |
|
| 198 | + return false; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * return size of encryption header |
|
| 203 | + * |
|
| 204 | + * @return integer |
|
| 205 | + */ |
|
| 206 | + public function getHeaderSize() { |
|
| 207 | + return $this->headerSize; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * return size of block read by a PHP stream |
|
| 212 | + * |
|
| 213 | + * @return integer |
|
| 214 | + */ |
|
| 215 | + public function getBlockSize() { |
|
| 216 | + return $this->blockSize; |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * get the owner and the path for the file relative to the owners files folder |
|
| 221 | + * |
|
| 222 | + * @param string $path |
|
| 223 | + * @return array |
|
| 224 | + * @throws \BadMethodCallException |
|
| 225 | + */ |
|
| 226 | + public function getUidAndFilename($path) { |
|
| 227 | + $parts = explode('/', $path); |
|
| 228 | + $uid = ''; |
|
| 229 | + if (count($parts) > 2) { |
|
| 230 | + $uid = $parts[1]; |
|
| 231 | + } |
|
| 232 | + if (!$this->userManager->userExists($uid)) { |
|
| 233 | + throw new \BadMethodCallException( |
|
| 234 | + 'path needs to be relative to the system wide data folder and point to a user specific file' |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + $ownerPath = implode('/', array_slice($parts, 2)); |
|
| 239 | + |
|
| 240 | + return [$uid, Filesystem::normalizePath($ownerPath)]; |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Remove .path extension from a file path |
|
| 245 | + * @param string $path Path that may identify a .part file |
|
| 246 | + * @return string File path without .part extension |
|
| 247 | + * @note this is needed for reusing keys |
|
| 248 | + */ |
|
| 249 | + public function stripPartialFileExtension($path) { |
|
| 250 | + $extension = pathinfo($path, PATHINFO_EXTENSION); |
|
| 251 | + |
|
| 252 | + if ($extension === 'part') { |
|
| 253 | + $newLength = strlen($path) - 5; // 5 = strlen(".part") |
|
| 254 | + $fPath = substr($path, 0, $newLength); |
|
| 255 | + |
|
| 256 | + // if path also contains a transaction id, we remove it too |
|
| 257 | + $extension = pathinfo($fPath, PATHINFO_EXTENSION); |
|
| 258 | + if (substr($extension, 0, 12) === 'ocTransferId') { // 12 = strlen("ocTransferId") |
|
| 259 | + $newLength = strlen($fPath) - strlen($extension) - 1; |
|
| 260 | + $fPath = substr($fPath, 0, $newLength); |
|
| 261 | + } |
|
| 262 | + return $fPath; |
|
| 263 | + } else { |
|
| 264 | + return $path; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + public function getUserWithAccessToMountPoint($users, $groups) { |
|
| 269 | + $result = []; |
|
| 270 | + if ($users === [] && $groups === []) { |
|
| 271 | + $users = $this->userManager->search('', null, null); |
|
| 272 | + $result = array_map(function (IUser $user) { |
|
| 273 | + return $user->getUID(); |
|
| 274 | + }, $users); |
|
| 275 | + } else { |
|
| 276 | + $result = array_merge($result, $users); |
|
| 277 | + |
|
| 278 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 279 | + foreach ($groups as $group) { |
|
| 280 | + $groupObject = $groupManager->get($group); |
|
| 281 | + if ($groupObject) { |
|
| 282 | + $foundUsers = $groupObject->searchUsers('', -1, 0); |
|
| 283 | + $userIds = []; |
|
| 284 | + foreach ($foundUsers as $user) { |
|
| 285 | + $userIds[] = $user->getUID(); |
|
| 286 | + } |
|
| 287 | + $result = array_merge($result, $userIds); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + return $result; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * check if the file is stored on a system wide mount point |
|
| 297 | + * @param string $path relative to /data/user with leading '/' |
|
| 298 | + * @param string $uid |
|
| 299 | + * @return boolean |
|
| 300 | + */ |
|
| 301 | + public function isSystemWideMountPoint($path, $uid) { |
|
| 302 | + if (\OCP\App::isEnabled("files_external")) { |
|
| 303 | + /** @var GlobalStoragesService $storageService */ |
|
| 304 | + $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
| 305 | + $storages = $storageService->getAllStorages(); |
|
| 306 | + foreach ($storages as $storage) { |
|
| 307 | + if (strpos($path, '/files/' . $storage->getMountPoint()) === 0) { |
|
| 308 | + if ($this->isMountPointApplicableToUser($storage, $uid)) { |
|
| 309 | + return true; |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * check if mount point is applicable to user |
|
| 319 | + * |
|
| 320 | + * @param StorageConfig $mount |
|
| 321 | + * @param string $uid |
|
| 322 | + * @return boolean |
|
| 323 | + */ |
|
| 324 | + private function isMountPointApplicableToUser(StorageConfig $mount, string $uid) { |
|
| 325 | + if ($mount->getApplicableUsers() === [] && $mount->getApplicableGroups() === []) { |
|
| 326 | + // applicable for everyone |
|
| 327 | + return true; |
|
| 328 | + } |
|
| 329 | + // check if mount point is applicable for the user |
|
| 330 | + if (array_search($uid, $mount->getApplicableUsers()) !== false) { |
|
| 331 | + return true; |
|
| 332 | + } |
|
| 333 | + // check if mount point is applicable for group where the user is a member |
|
| 334 | + foreach ($mount->getApplicableGroups() as $gid) { |
|
| 335 | + if ($this->groupManager->isInGroup($uid, $gid)) { |
|
| 336 | + return true; |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + return false; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * check if it is a path which is excluded by ownCloud from encryption |
|
| 344 | + * |
|
| 345 | + * @param string $path |
|
| 346 | + * @return boolean |
|
| 347 | + */ |
|
| 348 | + public function isExcluded($path) { |
|
| 349 | + $normalizedPath = Filesystem::normalizePath($path); |
|
| 350 | + $root = explode('/', $normalizedPath, 4); |
|
| 351 | + if (count($root) > 1) { |
|
| 352 | + |
|
| 353 | + // detect alternative key storage root |
|
| 354 | + $rootDir = $this->getKeyStorageRoot(); |
|
| 355 | + if ($rootDir !== '' && |
|
| 356 | + 0 === strpos( |
|
| 357 | + Filesystem::normalizePath($path), |
|
| 358 | + Filesystem::normalizePath($rootDir) |
|
| 359 | + ) |
|
| 360 | + ) { |
|
| 361 | + return true; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + |
|
| 365 | + //detect system wide folders |
|
| 366 | + if (in_array($root[1], $this->excludedPaths)) { |
|
| 367 | + return true; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + // detect user specific folders |
|
| 371 | + if ($this->userManager->userExists($root[1]) |
|
| 372 | + && in_array($root[2], $this->excludedPaths)) { |
|
| 373 | + return true; |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + return false; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * check if recovery key is enabled for user |
|
| 381 | + * |
|
| 382 | + * @param string $uid |
|
| 383 | + * @return boolean |
|
| 384 | + */ |
|
| 385 | + public function recoveryEnabled($uid) { |
|
| 386 | + $enabled = $this->config->getUserValue($uid, 'encryption', 'recovery_enabled', '0'); |
|
| 387 | + |
|
| 388 | + return $enabled === '1'; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * set new key storage root |
|
| 393 | + * |
|
| 394 | + * @param string $root new key store root relative to the data folder |
|
| 395 | + */ |
|
| 396 | + public function setKeyStorageRoot($root) { |
|
| 397 | + $this->config->setAppValue('core', 'encryption_key_storage_root', $root); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * get key storage root |
|
| 402 | + * |
|
| 403 | + * @return string key storage root |
|
| 404 | + */ |
|
| 405 | + public function getKeyStorageRoot() { |
|
| 406 | + return $this->config->getAppValue('core', 'encryption_key_storage_root', ''); |
|
| 407 | + } |
|
| 408 | 408 | } |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | $this->config = $config; |
| 100 | 100 | |
| 101 | 101 | $this->excludedPaths[] = 'files_encryption'; |
| 102 | - $this->excludedPaths[] = 'appdata_' . $config->getSystemValue('instanceid', null); |
|
| 102 | + $this->excludedPaths[] = 'appdata_'.$config->getSystemValue('instanceid', null); |
|
| 103 | 103 | $this->excludedPaths[] = 'files_external'; |
| 104 | 104 | } |
| 105 | 105 | |
@@ -139,12 +139,12 @@ discard block |
||
| 139 | 139 | * @throws EncryptionHeaderKeyExistsException if header key is already in use |
| 140 | 140 | */ |
| 141 | 141 | public function createHeader(array $headerData, IEncryptionModule $encryptionModule) { |
| 142 | - $header = self::HEADER_START . ':' . self::HEADER_ENCRYPTION_MODULE_KEY . ':' . $encryptionModule->getId() . ':'; |
|
| 142 | + $header = self::HEADER_START.':'.self::HEADER_ENCRYPTION_MODULE_KEY.':'.$encryptionModule->getId().':'; |
|
| 143 | 143 | foreach ($headerData as $key => $value) { |
| 144 | 144 | if (in_array($key, $this->ocHeaderKeys)) { |
| 145 | 145 | throw new EncryptionHeaderKeyExistsException($key); |
| 146 | 146 | } |
| 147 | - $header .= $key . ':' . $value . ':'; |
|
| 147 | + $header .= $key.':'.$value.':'; |
|
| 148 | 148 | } |
| 149 | 149 | $header .= self::HEADER_END; |
| 150 | 150 | |
@@ -269,7 +269,7 @@ discard block |
||
| 269 | 269 | $result = []; |
| 270 | 270 | if ($users === [] && $groups === []) { |
| 271 | 271 | $users = $this->userManager->search('', null, null); |
| 272 | - $result = array_map(function (IUser $user) { |
|
| 272 | + $result = array_map(function(IUser $user) { |
|
| 273 | 273 | return $user->getUID(); |
| 274 | 274 | }, $users); |
| 275 | 275 | } else { |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | $storageService = \OC::$server->get(GlobalStoragesService::class); |
| 305 | 305 | $storages = $storageService->getAllStorages(); |
| 306 | 306 | foreach ($storages as $storage) { |
| 307 | - if (strpos($path, '/files/' . $storage->getMountPoint()) === 0) { |
|
| 307 | + if (strpos($path, '/files/'.$storage->getMountPoint()) === 0) { |
|
| 308 | 308 | if ($this->isMountPointApplicableToUser($storage, $uid)) { |
| 309 | 309 | return true; |
| 310 | 310 | } |
@@ -35,96 +35,96 @@ |
||
| 35 | 35 | |
| 36 | 36 | class File implements \OCP\Encryption\IFile { |
| 37 | 37 | |
| 38 | - /** @var Util */ |
|
| 39 | - protected $util; |
|
| 40 | - |
|
| 41 | - /** @var IRootFolder */ |
|
| 42 | - private $rootFolder; |
|
| 43 | - |
|
| 44 | - /** @var IManager */ |
|
| 45 | - private $shareManager; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * cache results of already checked folders |
|
| 49 | - * |
|
| 50 | - * @var array |
|
| 51 | - */ |
|
| 52 | - protected $cache; |
|
| 53 | - |
|
| 54 | - public function __construct(Util $util, |
|
| 55 | - IRootFolder $rootFolder, |
|
| 56 | - IManager $shareManager) { |
|
| 57 | - $this->util = $util; |
|
| 58 | - $this->cache = new CappedMemoryCache(); |
|
| 59 | - $this->rootFolder = $rootFolder; |
|
| 60 | - $this->shareManager = $shareManager; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * get list of users with access to the file |
|
| 66 | - * |
|
| 67 | - * @param string $path to the file |
|
| 68 | - * @return array ['users' => $uniqueUserIds, 'public' => $public] |
|
| 69 | - */ |
|
| 70 | - public function getAccessList($path) { |
|
| 71 | - |
|
| 72 | - // Make sure that a share key is generated for the owner too |
|
| 73 | - [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
| 74 | - |
|
| 75 | - // always add owner to the list of users with access to the file |
|
| 76 | - $userIds = [$owner]; |
|
| 77 | - |
|
| 78 | - if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
| 79 | - return ['users' => $userIds, 'public' => false]; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - $ownerPath = substr($ownerPath, strlen('/files')); |
|
| 83 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 84 | - try { |
|
| 85 | - $file = $userFolder->get($ownerPath); |
|
| 86 | - } catch (NotFoundException $e) { |
|
| 87 | - $file = null; |
|
| 88 | - } |
|
| 89 | - $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
| 90 | - |
|
| 91 | - // first get the shares for the parent and cache the result so that we don't |
|
| 92 | - // need to check all parents for every file |
|
| 93 | - $parent = dirname($ownerPath); |
|
| 94 | - $parentNode = $userFolder->get($parent); |
|
| 95 | - if (isset($this->cache[$parent])) { |
|
| 96 | - $resultForParents = $this->cache[$parent]; |
|
| 97 | - } else { |
|
| 98 | - $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
| 99 | - $this->cache[$parent] = $resultForParents; |
|
| 100 | - } |
|
| 101 | - $userIds = array_merge($userIds, $resultForParents['users']); |
|
| 102 | - $public = $resultForParents['public'] || $resultForParents['remote']; |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - // Find out who, if anyone, is sharing the file |
|
| 106 | - if ($file !== null) { |
|
| 107 | - $resultForFile = $this->shareManager->getAccessList($file, false); |
|
| 108 | - $userIds = array_merge($userIds, $resultForFile['users']); |
|
| 109 | - $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - // check if it is a group mount |
|
| 113 | - if (\OCP\App::isEnabled("files_external")) { |
|
| 114 | - /** @var GlobalStoragesService $storageService */ |
|
| 115 | - $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
| 116 | - $storages = $storageService->getAllStorages(); |
|
| 117 | - foreach ($storages as $storage) { |
|
| 118 | - if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) { |
|
| 119 | - $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups()); |
|
| 120 | - $userIds = array_merge($userIds, $mountedFor); |
|
| 121 | - } |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Remove duplicate UIDs |
|
| 126 | - $uniqueUserIds = array_unique($userIds); |
|
| 127 | - |
|
| 128 | - return ['users' => $uniqueUserIds, 'public' => $public]; |
|
| 129 | - } |
|
| 38 | + /** @var Util */ |
|
| 39 | + protected $util; |
|
| 40 | + |
|
| 41 | + /** @var IRootFolder */ |
|
| 42 | + private $rootFolder; |
|
| 43 | + |
|
| 44 | + /** @var IManager */ |
|
| 45 | + private $shareManager; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * cache results of already checked folders |
|
| 49 | + * |
|
| 50 | + * @var array |
|
| 51 | + */ |
|
| 52 | + protected $cache; |
|
| 53 | + |
|
| 54 | + public function __construct(Util $util, |
|
| 55 | + IRootFolder $rootFolder, |
|
| 56 | + IManager $shareManager) { |
|
| 57 | + $this->util = $util; |
|
| 58 | + $this->cache = new CappedMemoryCache(); |
|
| 59 | + $this->rootFolder = $rootFolder; |
|
| 60 | + $this->shareManager = $shareManager; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * get list of users with access to the file |
|
| 66 | + * |
|
| 67 | + * @param string $path to the file |
|
| 68 | + * @return array ['users' => $uniqueUserIds, 'public' => $public] |
|
| 69 | + */ |
|
| 70 | + public function getAccessList($path) { |
|
| 71 | + |
|
| 72 | + // Make sure that a share key is generated for the owner too |
|
| 73 | + [$owner, $ownerPath] = $this->util->getUidAndFilename($path); |
|
| 74 | + |
|
| 75 | + // always add owner to the list of users with access to the file |
|
| 76 | + $userIds = [$owner]; |
|
| 77 | + |
|
| 78 | + if (!$this->util->isFile($owner . '/' . $ownerPath)) { |
|
| 79 | + return ['users' => $userIds, 'public' => false]; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + $ownerPath = substr($ownerPath, strlen('/files')); |
|
| 83 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 84 | + try { |
|
| 85 | + $file = $userFolder->get($ownerPath); |
|
| 86 | + } catch (NotFoundException $e) { |
|
| 87 | + $file = null; |
|
| 88 | + } |
|
| 89 | + $ownerPath = $this->util->stripPartialFileExtension($ownerPath); |
|
| 90 | + |
|
| 91 | + // first get the shares for the parent and cache the result so that we don't |
|
| 92 | + // need to check all parents for every file |
|
| 93 | + $parent = dirname($ownerPath); |
|
| 94 | + $parentNode = $userFolder->get($parent); |
|
| 95 | + if (isset($this->cache[$parent])) { |
|
| 96 | + $resultForParents = $this->cache[$parent]; |
|
| 97 | + } else { |
|
| 98 | + $resultForParents = $this->shareManager->getAccessList($parentNode); |
|
| 99 | + $this->cache[$parent] = $resultForParents; |
|
| 100 | + } |
|
| 101 | + $userIds = array_merge($userIds, $resultForParents['users']); |
|
| 102 | + $public = $resultForParents['public'] || $resultForParents['remote']; |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + // Find out who, if anyone, is sharing the file |
|
| 106 | + if ($file !== null) { |
|
| 107 | + $resultForFile = $this->shareManager->getAccessList($file, false); |
|
| 108 | + $userIds = array_merge($userIds, $resultForFile['users']); |
|
| 109 | + $public = $resultForFile['public'] || $resultForFile['remote'] || $public; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + // check if it is a group mount |
|
| 113 | + if (\OCP\App::isEnabled("files_external")) { |
|
| 114 | + /** @var GlobalStoragesService $storageService */ |
|
| 115 | + $storageService = \OC::$server->get(GlobalStoragesService::class); |
|
| 116 | + $storages = $storageService->getAllStorages(); |
|
| 117 | + foreach ($storages as $storage) { |
|
| 118 | + if ($storage->getMountPoint() == substr($ownerPath, 0, strlen($storage->getMountPoint()))) { |
|
| 119 | + $mountedFor = $this->util->getUserWithAccessToMountPoint($storage->getApplicableUsers(), $storage->getApplicableGroups()); |
|
| 120 | + $userIds = array_merge($userIds, $mountedFor); |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Remove duplicate UIDs |
|
| 126 | + $uniqueUserIds = array_unique($userIds); |
|
| 127 | + |
|
| 128 | + return ['users' => $uniqueUserIds, 'public' => $public]; |
|
| 129 | + } |
|
| 130 | 130 | } |
@@ -39,87 +39,87 @@ |
||
| 39 | 39 | |
| 40 | 40 | class ApiController extends OCSController { |
| 41 | 41 | |
| 42 | - /** @var IUserSession */ |
|
| 43 | - private $userSession; |
|
| 44 | - /** @var UserGlobalStoragesService */ |
|
| 45 | - private $userGlobalStoragesService; |
|
| 46 | - /** @var UserStoragesService */ |
|
| 47 | - private $userStoragesService; |
|
| 42 | + /** @var IUserSession */ |
|
| 43 | + private $userSession; |
|
| 44 | + /** @var UserGlobalStoragesService */ |
|
| 45 | + private $userGlobalStoragesService; |
|
| 46 | + /** @var UserStoragesService */ |
|
| 47 | + private $userStoragesService; |
|
| 48 | 48 | |
| 49 | - public function __construct( |
|
| 50 | - string $appName, |
|
| 51 | - IRequest $request, |
|
| 52 | - IUserSession $userSession, |
|
| 53 | - UserGlobalStoragesService $userGlobalStorageService, |
|
| 54 | - UserStoragesService $userStorageService |
|
| 55 | - ) { |
|
| 56 | - parent::__construct($appName, $request); |
|
| 49 | + public function __construct( |
|
| 50 | + string $appName, |
|
| 51 | + IRequest $request, |
|
| 52 | + IUserSession $userSession, |
|
| 53 | + UserGlobalStoragesService $userGlobalStorageService, |
|
| 54 | + UserStoragesService $userStorageService |
|
| 55 | + ) { |
|
| 56 | + parent::__construct($appName, $request); |
|
| 57 | 57 | |
| 58 | - $this->userSession = $userSession; |
|
| 59 | - $this->userGlobalStoragesService = $userGlobalStorageService; |
|
| 60 | - $this->userStoragesService = $userStorageService; |
|
| 61 | - } |
|
| 58 | + $this->userSession = $userSession; |
|
| 59 | + $this->userGlobalStoragesService = $userGlobalStorageService; |
|
| 60 | + $this->userStoragesService = $userStorageService; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * Formats the given mount config to a mount entry. |
|
| 65 | - * |
|
| 66 | - * @param string $mountPoint mount point name, relative to the data dir |
|
| 67 | - * @param StorageConfig $mountConfig mount config to format |
|
| 68 | - * |
|
| 69 | - * @return array entry |
|
| 70 | - */ |
|
| 71 | - private function formatMount(string $mountPoint, StorageConfig $mountConfig): array { |
|
| 72 | - // split path from mount point |
|
| 73 | - $path = \dirname($mountPoint); |
|
| 74 | - if ($path === '.' || $path === '/') { |
|
| 75 | - $path = ''; |
|
| 76 | - } |
|
| 63 | + /** |
|
| 64 | + * Formats the given mount config to a mount entry. |
|
| 65 | + * |
|
| 66 | + * @param string $mountPoint mount point name, relative to the data dir |
|
| 67 | + * @param StorageConfig $mountConfig mount config to format |
|
| 68 | + * |
|
| 69 | + * @return array entry |
|
| 70 | + */ |
|
| 71 | + private function formatMount(string $mountPoint, StorageConfig $mountConfig): array { |
|
| 72 | + // split path from mount point |
|
| 73 | + $path = \dirname($mountPoint); |
|
| 74 | + if ($path === '.' || $path === '/') { |
|
| 75 | + $path = ''; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN; |
|
| 78 | + $isSystemMount = $mountConfig->getType() === StorageConfig::MOUNT_TYPE_ADMIN; |
|
| 79 | 79 | |
| 80 | - $permissions = \OCP\Constants::PERMISSION_READ; |
|
| 81 | - // personal mounts can be deleted |
|
| 82 | - if (!$isSystemMount) { |
|
| 83 | - $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
| 84 | - } |
|
| 80 | + $permissions = \OCP\Constants::PERMISSION_READ; |
|
| 81 | + // personal mounts can be deleted |
|
| 82 | + if (!$isSystemMount) { |
|
| 83 | + $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - $entry = [ |
|
| 87 | - 'name' => basename($mountPoint), |
|
| 88 | - 'path' => $path, |
|
| 89 | - 'type' => 'dir', |
|
| 90 | - 'backend' => $mountConfig->getBackend()->getText(), |
|
| 91 | - 'scope' => $isSystemMount ? 'system' : 'personal', |
|
| 92 | - 'permissions' => $permissions, |
|
| 93 | - 'id' => $mountConfig->getId(), |
|
| 94 | - 'class' => $mountConfig->getBackend()->getIdentifier(), |
|
| 95 | - ]; |
|
| 96 | - return $entry; |
|
| 97 | - } |
|
| 86 | + $entry = [ |
|
| 87 | + 'name' => basename($mountPoint), |
|
| 88 | + 'path' => $path, |
|
| 89 | + 'type' => 'dir', |
|
| 90 | + 'backend' => $mountConfig->getBackend()->getText(), |
|
| 91 | + 'scope' => $isSystemMount ? 'system' : 'personal', |
|
| 92 | + 'permissions' => $permissions, |
|
| 93 | + 'id' => $mountConfig->getId(), |
|
| 94 | + 'class' => $mountConfig->getBackend()->getIdentifier(), |
|
| 95 | + ]; |
|
| 96 | + return $entry; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * @NoAdminRequired |
|
| 101 | - * |
|
| 102 | - * Returns the mount points visible for this user. |
|
| 103 | - * |
|
| 104 | - * @return DataResponse share information |
|
| 105 | - */ |
|
| 106 | - public function getUserMounts(): DataResponse { |
|
| 107 | - $entries = []; |
|
| 108 | - $mountPoints = []; |
|
| 99 | + /** |
|
| 100 | + * @NoAdminRequired |
|
| 101 | + * |
|
| 102 | + * Returns the mount points visible for this user. |
|
| 103 | + * |
|
| 104 | + * @return DataResponse share information |
|
| 105 | + */ |
|
| 106 | + public function getUserMounts(): DataResponse { |
|
| 107 | + $entries = []; |
|
| 108 | + $mountPoints = []; |
|
| 109 | 109 | |
| 110 | - foreach ($this->userGlobalStoragesService->getStorages() as $storage) { |
|
| 111 | - $mountPoint = $storage->getMountPoint(); |
|
| 112 | - $mountPoints[$mountPoint] = $storage; |
|
| 113 | - } |
|
| 110 | + foreach ($this->userGlobalStoragesService->getStorages() as $storage) { |
|
| 111 | + $mountPoint = $storage->getMountPoint(); |
|
| 112 | + $mountPoints[$mountPoint] = $storage; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - foreach ($this->userStoragesService->getStorages() as $storage) { |
|
| 116 | - $mountPoint = $storage->getMountPoint(); |
|
| 117 | - $mountPoints[$mountPoint] = $storage; |
|
| 118 | - } |
|
| 119 | - foreach ($mountPoints as $mountPoint => $mount) { |
|
| 120 | - $entries[] = $this->formatMount($mountPoint, $mount); |
|
| 121 | - } |
|
| 115 | + foreach ($this->userStoragesService->getStorages() as $storage) { |
|
| 116 | + $mountPoint = $storage->getMountPoint(); |
|
| 117 | + $mountPoints[$mountPoint] = $storage; |
|
| 118 | + } |
|
| 119 | + foreach ($mountPoints as $mountPoint => $mount) { |
|
| 120 | + $entries[] = $this->formatMount($mountPoint, $mount); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - return new DataResponse($entries); |
|
| 124 | - } |
|
| 123 | + return new DataResponse($entries); |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -40,187 +40,187 @@ |
||
| 40 | 40 | use Symfony\Component\Console\Output\OutputInterface; |
| 41 | 41 | |
| 42 | 42 | class Import extends Base { |
| 43 | - /** |
|
| 44 | - * @var GlobalStoragesService |
|
| 45 | - */ |
|
| 46 | - private $globalService; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var UserStoragesService |
|
| 50 | - */ |
|
| 51 | - private $userService; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var IUserSession |
|
| 55 | - */ |
|
| 56 | - private $userSession; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var IUserManager |
|
| 60 | - */ |
|
| 61 | - private $userManager; |
|
| 62 | - |
|
| 63 | - /** @var ImportLegacyStoragesService */ |
|
| 64 | - private $importLegacyStorageService; |
|
| 65 | - |
|
| 66 | - /** @var BackendService */ |
|
| 67 | - private $backendService; |
|
| 68 | - |
|
| 69 | - public function __construct(GlobalStoragesService $globalService, |
|
| 70 | - UserStoragesService $userService, |
|
| 71 | - IUserSession $userSession, |
|
| 72 | - IUserManager $userManager, |
|
| 73 | - ImportLegacyStoragesService $importLegacyStorageService, |
|
| 74 | - BackendService $backendService |
|
| 75 | - ) { |
|
| 76 | - parent::__construct(); |
|
| 77 | - $this->globalService = $globalService; |
|
| 78 | - $this->userService = $userService; |
|
| 79 | - $this->userSession = $userSession; |
|
| 80 | - $this->userManager = $userManager; |
|
| 81 | - $this->importLegacyStorageService = $importLegacyStorageService; |
|
| 82 | - $this->backendService = $backendService; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - protected function configure() { |
|
| 86 | - $this |
|
| 87 | - ->setName('files_external:import') |
|
| 88 | - ->setDescription('Import mount configurations') |
|
| 89 | - ->addOption( |
|
| 90 | - 'user', |
|
| 91 | - '', |
|
| 92 | - InputOption::VALUE_OPTIONAL, |
|
| 93 | - 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
| 94 | - ) |
|
| 95 | - ->addArgument( |
|
| 96 | - 'path', |
|
| 97 | - InputArgument::REQUIRED, |
|
| 98 | - 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
| 99 | - ) |
|
| 100 | - ->addOption( |
|
| 101 | - 'dry', |
|
| 102 | - '', |
|
| 103 | - InputOption::VALUE_NONE, |
|
| 104 | - 'Don\'t save the imported mounts, only list the new mounts' |
|
| 105 | - ); |
|
| 106 | - parent::configure(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 110 | - $user = (string) $input->getOption('user'); |
|
| 111 | - $path = $input->getArgument('path'); |
|
| 112 | - if ($path === '-') { |
|
| 113 | - $json = file_get_contents('php://stdin'); |
|
| 114 | - } else { |
|
| 115 | - if (!file_exists($path)) { |
|
| 116 | - $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
| 117 | - return 1; |
|
| 118 | - } |
|
| 119 | - $json = file_get_contents($path); |
|
| 120 | - } |
|
| 121 | - if (!is_string($json) || strlen($json) < 2) { |
|
| 122 | - $output->writeln('<error>Error while reading json</error>'); |
|
| 123 | - return 1; |
|
| 124 | - } |
|
| 125 | - $data = json_decode($json, true); |
|
| 126 | - if (!is_array($data)) { |
|
| 127 | - $output->writeln('<error>Error while parsing json</error>'); |
|
| 128 | - return 1; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - $isLegacy = isset($data['user']) || isset($data['group']); |
|
| 132 | - if ($isLegacy) { |
|
| 133 | - $this->importLegacyStorageService->setData($data); |
|
| 134 | - $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
| 135 | - foreach ($mounts as $mount) { |
|
| 136 | - if ($mount->getBackendOption('password') === false) { |
|
| 137 | - $output->writeln('<error>Failed to decrypt password</error>'); |
|
| 138 | - return 1; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - } else { |
|
| 142 | - if (!isset($data[0])) { //normalize to an array of mounts |
|
| 143 | - $data = [$data]; |
|
| 144 | - } |
|
| 145 | - $mounts = array_map([$this, 'parseData'], $data); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - if ($user) { |
|
| 149 | - // ensure applicables are correct for personal mounts |
|
| 150 | - foreach ($mounts as $mount) { |
|
| 151 | - $mount->setApplicableGroups([]); |
|
| 152 | - $mount->setApplicableUsers([$user]); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $storageService = $this->getStorageService($user); |
|
| 157 | - |
|
| 158 | - $existingMounts = $storageService->getAllStorages(); |
|
| 159 | - |
|
| 160 | - foreach ($mounts as $mount) { |
|
| 161 | - foreach ($existingMounts as $existingMount) { |
|
| 162 | - if ( |
|
| 163 | - $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
| 164 | - $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
| 165 | - $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
|
| 166 | - $existingMount->getBackendOptions() === $mount->getBackendOptions() |
|
| 167 | - ) { |
|
| 168 | - $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
| 169 | - return 1; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - if ($input->getOption('dry')) { |
|
| 175 | - if (count($mounts) === 0) { |
|
| 176 | - $output->writeln('<error>No mounts to be imported</error>'); |
|
| 177 | - return 1; |
|
| 178 | - } |
|
| 179 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 180 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 181 | - $listInput->setOption('output', $input->getOption('output')); |
|
| 182 | - $listInput->setOption('show-password', true); |
|
| 183 | - $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
| 184 | - } else { |
|
| 185 | - foreach ($mounts as $mount) { |
|
| 186 | - $storageService->addStorage($mount); |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - return 0; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - private function parseData(array $data) { |
|
| 193 | - $mount = new StorageConfig($data['mount_id']); |
|
| 194 | - $mount->setMountPoint($data['mount_point']); |
|
| 195 | - $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
| 196 | - $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
| 197 | - $mount->setAuthMechanism($authBackend); |
|
| 198 | - $mount->setBackendOptions($data['configuration']); |
|
| 199 | - $mount->setMountOptions($data['options']); |
|
| 200 | - $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
| 201 | - $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
| 202 | - return $mount; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - private function getBackendByClass($className) { |
|
| 206 | - $backends = $this->backendService->getBackends(); |
|
| 207 | - foreach ($backends as $backend) { |
|
| 208 | - if ($backend->getStorageClass() === $className) { |
|
| 209 | - return $backend; |
|
| 210 | - } |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - protected function getStorageService($userId) { |
|
| 215 | - if (!empty($userId)) { |
|
| 216 | - $user = $this->userManager->get($userId); |
|
| 217 | - if (is_null($user)) { |
|
| 218 | - throw new NoUserException("user $userId not found"); |
|
| 219 | - } |
|
| 220 | - $this->userSession->setUser($user); |
|
| 221 | - return $this->userService; |
|
| 222 | - } else { |
|
| 223 | - return $this->globalService; |
|
| 224 | - } |
|
| 225 | - } |
|
| 43 | + /** |
|
| 44 | + * @var GlobalStoragesService |
|
| 45 | + */ |
|
| 46 | + private $globalService; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var UserStoragesService |
|
| 50 | + */ |
|
| 51 | + private $userService; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var IUserSession |
|
| 55 | + */ |
|
| 56 | + private $userSession; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var IUserManager |
|
| 60 | + */ |
|
| 61 | + private $userManager; |
|
| 62 | + |
|
| 63 | + /** @var ImportLegacyStoragesService */ |
|
| 64 | + private $importLegacyStorageService; |
|
| 65 | + |
|
| 66 | + /** @var BackendService */ |
|
| 67 | + private $backendService; |
|
| 68 | + |
|
| 69 | + public function __construct(GlobalStoragesService $globalService, |
|
| 70 | + UserStoragesService $userService, |
|
| 71 | + IUserSession $userSession, |
|
| 72 | + IUserManager $userManager, |
|
| 73 | + ImportLegacyStoragesService $importLegacyStorageService, |
|
| 74 | + BackendService $backendService |
|
| 75 | + ) { |
|
| 76 | + parent::__construct(); |
|
| 77 | + $this->globalService = $globalService; |
|
| 78 | + $this->userService = $userService; |
|
| 79 | + $this->userSession = $userSession; |
|
| 80 | + $this->userManager = $userManager; |
|
| 81 | + $this->importLegacyStorageService = $importLegacyStorageService; |
|
| 82 | + $this->backendService = $backendService; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + protected function configure() { |
|
| 86 | + $this |
|
| 87 | + ->setName('files_external:import') |
|
| 88 | + ->setDescription('Import mount configurations') |
|
| 89 | + ->addOption( |
|
| 90 | + 'user', |
|
| 91 | + '', |
|
| 92 | + InputOption::VALUE_OPTIONAL, |
|
| 93 | + 'user to add the mount configurations for, if not set the mount will be added as system mount' |
|
| 94 | + ) |
|
| 95 | + ->addArgument( |
|
| 96 | + 'path', |
|
| 97 | + InputArgument::REQUIRED, |
|
| 98 | + 'path to a json file containing the mounts to import, use "-" to read from stdin' |
|
| 99 | + ) |
|
| 100 | + ->addOption( |
|
| 101 | + 'dry', |
|
| 102 | + '', |
|
| 103 | + InputOption::VALUE_NONE, |
|
| 104 | + 'Don\'t save the imported mounts, only list the new mounts' |
|
| 105 | + ); |
|
| 106 | + parent::configure(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 110 | + $user = (string) $input->getOption('user'); |
|
| 111 | + $path = $input->getArgument('path'); |
|
| 112 | + if ($path === '-') { |
|
| 113 | + $json = file_get_contents('php://stdin'); |
|
| 114 | + } else { |
|
| 115 | + if (!file_exists($path)) { |
|
| 116 | + $output->writeln('<error>File not found: ' . $path . '</error>'); |
|
| 117 | + return 1; |
|
| 118 | + } |
|
| 119 | + $json = file_get_contents($path); |
|
| 120 | + } |
|
| 121 | + if (!is_string($json) || strlen($json) < 2) { |
|
| 122 | + $output->writeln('<error>Error while reading json</error>'); |
|
| 123 | + return 1; |
|
| 124 | + } |
|
| 125 | + $data = json_decode($json, true); |
|
| 126 | + if (!is_array($data)) { |
|
| 127 | + $output->writeln('<error>Error while parsing json</error>'); |
|
| 128 | + return 1; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + $isLegacy = isset($data['user']) || isset($data['group']); |
|
| 132 | + if ($isLegacy) { |
|
| 133 | + $this->importLegacyStorageService->setData($data); |
|
| 134 | + $mounts = $this->importLegacyStorageService->getAllStorages(); |
|
| 135 | + foreach ($mounts as $mount) { |
|
| 136 | + if ($mount->getBackendOption('password') === false) { |
|
| 137 | + $output->writeln('<error>Failed to decrypt password</error>'); |
|
| 138 | + return 1; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + } else { |
|
| 142 | + if (!isset($data[0])) { //normalize to an array of mounts |
|
| 143 | + $data = [$data]; |
|
| 144 | + } |
|
| 145 | + $mounts = array_map([$this, 'parseData'], $data); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + if ($user) { |
|
| 149 | + // ensure applicables are correct for personal mounts |
|
| 150 | + foreach ($mounts as $mount) { |
|
| 151 | + $mount->setApplicableGroups([]); |
|
| 152 | + $mount->setApplicableUsers([$user]); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $storageService = $this->getStorageService($user); |
|
| 157 | + |
|
| 158 | + $existingMounts = $storageService->getAllStorages(); |
|
| 159 | + |
|
| 160 | + foreach ($mounts as $mount) { |
|
| 161 | + foreach ($existingMounts as $existingMount) { |
|
| 162 | + if ( |
|
| 163 | + $existingMount->getMountPoint() === $mount->getMountPoint() && |
|
| 164 | + $existingMount->getApplicableGroups() === $mount->getApplicableGroups() && |
|
| 165 | + $existingMount->getApplicableUsers() === $mount->getApplicableUsers() && |
|
| 166 | + $existingMount->getBackendOptions() === $mount->getBackendOptions() |
|
| 167 | + ) { |
|
| 168 | + $output->writeln("<error>Duplicate mount (" . $mount->getMountPoint() . ")</error>"); |
|
| 169 | + return 1; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + if ($input->getOption('dry')) { |
|
| 175 | + if (count($mounts) === 0) { |
|
| 176 | + $output->writeln('<error>No mounts to be imported</error>'); |
|
| 177 | + return 1; |
|
| 178 | + } |
|
| 179 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 180 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 181 | + $listInput->setOption('output', $input->getOption('output')); |
|
| 182 | + $listInput->setOption('show-password', true); |
|
| 183 | + $listCommand->listMounts($user, $mounts, $listInput, $output); |
|
| 184 | + } else { |
|
| 185 | + foreach ($mounts as $mount) { |
|
| 186 | + $storageService->addStorage($mount); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + return 0; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + private function parseData(array $data) { |
|
| 193 | + $mount = new StorageConfig($data['mount_id']); |
|
| 194 | + $mount->setMountPoint($data['mount_point']); |
|
| 195 | + $mount->setBackend($this->getBackendByClass($data['storage'])); |
|
| 196 | + $authBackend = $this->backendService->getAuthMechanism($data['authentication_type']); |
|
| 197 | + $mount->setAuthMechanism($authBackend); |
|
| 198 | + $mount->setBackendOptions($data['configuration']); |
|
| 199 | + $mount->setMountOptions($data['options']); |
|
| 200 | + $mount->setApplicableUsers(isset($data['applicable_users']) ? $data['applicable_users'] : []); |
|
| 201 | + $mount->setApplicableGroups(isset($data['applicable_groups']) ? $data['applicable_groups'] : []); |
|
| 202 | + return $mount; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + private function getBackendByClass($className) { |
|
| 206 | + $backends = $this->backendService->getBackends(); |
|
| 207 | + foreach ($backends as $backend) { |
|
| 208 | + if ($backend->getStorageClass() === $className) { |
|
| 209 | + return $backend; |
|
| 210 | + } |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + protected function getStorageService($userId) { |
|
| 215 | + if (!empty($userId)) { |
|
| 216 | + $user = $this->userManager->get($userId); |
|
| 217 | + if (is_null($user)) { |
|
| 218 | + throw new NoUserException("user $userId not found"); |
|
| 219 | + } |
|
| 220 | + $this->userSession->setUser($user); |
|
| 221 | + return $this->userService; |
|
| 222 | + } else { |
|
| 223 | + return $this->globalService; |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | 226 | } |
@@ -43,182 +43,182 @@ |
||
| 43 | 43 | use Symfony\Component\Console\Output\OutputInterface; |
| 44 | 44 | |
| 45 | 45 | class Create extends Base { |
| 46 | - /** |
|
| 47 | - * @var GlobalStoragesService |
|
| 48 | - */ |
|
| 49 | - private $globalService; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var UserStoragesService |
|
| 53 | - */ |
|
| 54 | - private $userService; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var IUserManager |
|
| 58 | - */ |
|
| 59 | - private $userManager; |
|
| 60 | - |
|
| 61 | - /** @var BackendService */ |
|
| 62 | - private $backendService; |
|
| 63 | - |
|
| 64 | - /** @var IUserSession */ |
|
| 65 | - private $userSession; |
|
| 66 | - |
|
| 67 | - public function __construct(GlobalStoragesService $globalService, |
|
| 68 | - UserStoragesService $userService, |
|
| 69 | - IUserManager $userManager, |
|
| 70 | - IUserSession $userSession, |
|
| 71 | - BackendService $backendService |
|
| 72 | - ) { |
|
| 73 | - parent::__construct(); |
|
| 74 | - $this->globalService = $globalService; |
|
| 75 | - $this->userService = $userService; |
|
| 76 | - $this->userManager = $userManager; |
|
| 77 | - $this->userSession = $userSession; |
|
| 78 | - $this->backendService = $backendService; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - protected function configure() { |
|
| 82 | - $this |
|
| 83 | - ->setName('files_external:create') |
|
| 84 | - ->setDescription('Create a new mount configuration') |
|
| 85 | - ->addOption( |
|
| 86 | - 'user', |
|
| 87 | - '', |
|
| 88 | - InputOption::VALUE_OPTIONAL, |
|
| 89 | - 'user to add the mount configuration for, if not set the mount will be added as system mount' |
|
| 90 | - ) |
|
| 91 | - ->addArgument( |
|
| 92 | - 'mount_point', |
|
| 93 | - InputArgument::REQUIRED, |
|
| 94 | - 'mount point for the new mount' |
|
| 95 | - ) |
|
| 96 | - ->addArgument( |
|
| 97 | - 'storage_backend', |
|
| 98 | - InputArgument::REQUIRED, |
|
| 99 | - 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
| 100 | - ) |
|
| 101 | - ->addArgument( |
|
| 102 | - 'authentication_backend', |
|
| 103 | - InputArgument::REQUIRED, |
|
| 104 | - 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
| 105 | - ) |
|
| 106 | - ->addOption( |
|
| 107 | - 'config', |
|
| 108 | - 'c', |
|
| 109 | - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
| 110 | - 'Mount configuration option in key=value format' |
|
| 111 | - ) |
|
| 112 | - ->addOption( |
|
| 113 | - 'dry', |
|
| 114 | - '', |
|
| 115 | - InputOption::VALUE_NONE, |
|
| 116 | - 'Don\'t save the created mount, only list the new mount' |
|
| 117 | - ); |
|
| 118 | - parent::configure(); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 122 | - $user = (string) $input->getOption('user'); |
|
| 123 | - $mountPoint = $input->getArgument('mount_point'); |
|
| 124 | - $storageIdentifier = $input->getArgument('storage_backend'); |
|
| 125 | - $authIdentifier = $input->getArgument('authentication_backend'); |
|
| 126 | - $configInput = $input->getOption('config'); |
|
| 127 | - |
|
| 128 | - $storageBackend = $this->backendService->getBackend($storageIdentifier); |
|
| 129 | - $authBackend = $this->backendService->getAuthMechanism($authIdentifier); |
|
| 130 | - |
|
| 131 | - if (!Filesystem::isValidPath($mountPoint)) { |
|
| 132 | - $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>'); |
|
| 133 | - return 1; |
|
| 134 | - } |
|
| 135 | - if (is_null($storageBackend)) { |
|
| 136 | - $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
| 137 | - return 404; |
|
| 138 | - } |
|
| 139 | - if (is_null($authBackend)) { |
|
| 140 | - $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
| 141 | - return 404; |
|
| 142 | - } |
|
| 143 | - $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); |
|
| 144 | - if (!in_array($authBackend->getScheme(), $supportedSchemes)) { |
|
| 145 | - $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>'); |
|
| 146 | - return 1; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - $config = []; |
|
| 150 | - foreach ($configInput as $configOption) { |
|
| 151 | - if (!strpos($configOption, '=')) { |
|
| 152 | - $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
| 153 | - return 1; |
|
| 154 | - } |
|
| 155 | - [$key, $value] = explode('=', $configOption, 2); |
|
| 156 | - if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { |
|
| 157 | - $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>'); |
|
| 158 | - return 1; |
|
| 159 | - } |
|
| 160 | - $config[$key] = $value; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $mount = new StorageConfig(); |
|
| 164 | - $mount->setMountPoint($mountPoint); |
|
| 165 | - $mount->setBackend($storageBackend); |
|
| 166 | - $mount->setAuthMechanism($authBackend); |
|
| 167 | - $mount->setBackendOptions($config); |
|
| 168 | - |
|
| 169 | - if ($user) { |
|
| 170 | - if (!$this->userManager->userExists($user)) { |
|
| 171 | - $output->writeln('<error>User "' . $user . '" not found</error>'); |
|
| 172 | - return 1; |
|
| 173 | - } |
|
| 174 | - $mount->setApplicableUsers([$user]); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if ($input->getOption('dry')) { |
|
| 178 | - $this->showMount($user, $mount, $input, $output); |
|
| 179 | - } else { |
|
| 180 | - $this->getStorageService($user)->addStorage($mount); |
|
| 181 | - if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { |
|
| 182 | - $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>'); |
|
| 183 | - } else { |
|
| 184 | - $output->writeln((string)$mount->getId()); |
|
| 185 | - } |
|
| 186 | - } |
|
| 187 | - return 0; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) { |
|
| 191 | - $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters()); |
|
| 192 | - foreach ($params as $param) { |
|
| 193 | - /** @var DefinitionParameter $param */ |
|
| 194 | - if ($param->getName() === $key) { |
|
| 195 | - if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) { |
|
| 196 | - $value = ($value === 'true'); |
|
| 197 | - } |
|
| 198 | - return true; |
|
| 199 | - } |
|
| 200 | - } |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) { |
|
| 205 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 206 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 207 | - $listInput->setOption('output', $input->getOption('output')); |
|
| 208 | - $listInput->setOption('show-password', true); |
|
| 209 | - $listCommand->listMounts($user, [$mount], $listInput, $output); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - protected function getStorageService($userId) { |
|
| 213 | - if (!empty($userId)) { |
|
| 214 | - $user = $this->userManager->get($userId); |
|
| 215 | - if (is_null($user)) { |
|
| 216 | - throw new NoUserException("user $userId not found"); |
|
| 217 | - } |
|
| 218 | - $this->userSession->setUser($user); |
|
| 219 | - return $this->userService; |
|
| 220 | - } else { |
|
| 221 | - return $this->globalService; |
|
| 222 | - } |
|
| 223 | - } |
|
| 46 | + /** |
|
| 47 | + * @var GlobalStoragesService |
|
| 48 | + */ |
|
| 49 | + private $globalService; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var UserStoragesService |
|
| 53 | + */ |
|
| 54 | + private $userService; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var IUserManager |
|
| 58 | + */ |
|
| 59 | + private $userManager; |
|
| 60 | + |
|
| 61 | + /** @var BackendService */ |
|
| 62 | + private $backendService; |
|
| 63 | + |
|
| 64 | + /** @var IUserSession */ |
|
| 65 | + private $userSession; |
|
| 66 | + |
|
| 67 | + public function __construct(GlobalStoragesService $globalService, |
|
| 68 | + UserStoragesService $userService, |
|
| 69 | + IUserManager $userManager, |
|
| 70 | + IUserSession $userSession, |
|
| 71 | + BackendService $backendService |
|
| 72 | + ) { |
|
| 73 | + parent::__construct(); |
|
| 74 | + $this->globalService = $globalService; |
|
| 75 | + $this->userService = $userService; |
|
| 76 | + $this->userManager = $userManager; |
|
| 77 | + $this->userSession = $userSession; |
|
| 78 | + $this->backendService = $backendService; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + protected function configure() { |
|
| 82 | + $this |
|
| 83 | + ->setName('files_external:create') |
|
| 84 | + ->setDescription('Create a new mount configuration') |
|
| 85 | + ->addOption( |
|
| 86 | + 'user', |
|
| 87 | + '', |
|
| 88 | + InputOption::VALUE_OPTIONAL, |
|
| 89 | + 'user to add the mount configuration for, if not set the mount will be added as system mount' |
|
| 90 | + ) |
|
| 91 | + ->addArgument( |
|
| 92 | + 'mount_point', |
|
| 93 | + InputArgument::REQUIRED, |
|
| 94 | + 'mount point for the new mount' |
|
| 95 | + ) |
|
| 96 | + ->addArgument( |
|
| 97 | + 'storage_backend', |
|
| 98 | + InputArgument::REQUIRED, |
|
| 99 | + 'storage backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
| 100 | + ) |
|
| 101 | + ->addArgument( |
|
| 102 | + 'authentication_backend', |
|
| 103 | + InputArgument::REQUIRED, |
|
| 104 | + 'authentication backend identifier for the new mount, see `occ files_external:backends` for possible values' |
|
| 105 | + ) |
|
| 106 | + ->addOption( |
|
| 107 | + 'config', |
|
| 108 | + 'c', |
|
| 109 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
| 110 | + 'Mount configuration option in key=value format' |
|
| 111 | + ) |
|
| 112 | + ->addOption( |
|
| 113 | + 'dry', |
|
| 114 | + '', |
|
| 115 | + InputOption::VALUE_NONE, |
|
| 116 | + 'Don\'t save the created mount, only list the new mount' |
|
| 117 | + ); |
|
| 118 | + parent::configure(); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
|
| 122 | + $user = (string) $input->getOption('user'); |
|
| 123 | + $mountPoint = $input->getArgument('mount_point'); |
|
| 124 | + $storageIdentifier = $input->getArgument('storage_backend'); |
|
| 125 | + $authIdentifier = $input->getArgument('authentication_backend'); |
|
| 126 | + $configInput = $input->getOption('config'); |
|
| 127 | + |
|
| 128 | + $storageBackend = $this->backendService->getBackend($storageIdentifier); |
|
| 129 | + $authBackend = $this->backendService->getAuthMechanism($authIdentifier); |
|
| 130 | + |
|
| 131 | + if (!Filesystem::isValidPath($mountPoint)) { |
|
| 132 | + $output->writeln('<error>Invalid mountpoint "' . $mountPoint . '"</error>'); |
|
| 133 | + return 1; |
|
| 134 | + } |
|
| 135 | + if (is_null($storageBackend)) { |
|
| 136 | + $output->writeln('<error>Storage backend with identifier "' . $storageIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
| 137 | + return 404; |
|
| 138 | + } |
|
| 139 | + if (is_null($authBackend)) { |
|
| 140 | + $output->writeln('<error>Authentication backend with identifier "' . $authIdentifier . '" not found (see `occ files_external:backends` for possible values)</error>'); |
|
| 141 | + return 404; |
|
| 142 | + } |
|
| 143 | + $supportedSchemes = array_keys($storageBackend->getAuthSchemes()); |
|
| 144 | + if (!in_array($authBackend->getScheme(), $supportedSchemes)) { |
|
| 145 | + $output->writeln('<error>Authentication backend "' . $authIdentifier . '" not valid for storage backend "' . $storageIdentifier . '" (see `occ files_external:backends storage ' . $storageIdentifier . '` for possible values)</error>'); |
|
| 146 | + return 1; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + $config = []; |
|
| 150 | + foreach ($configInput as $configOption) { |
|
| 151 | + if (!strpos($configOption, '=')) { |
|
| 152 | + $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
| 153 | + return 1; |
|
| 154 | + } |
|
| 155 | + [$key, $value] = explode('=', $configOption, 2); |
|
| 156 | + if (!$this->validateParam($key, $value, $storageBackend, $authBackend)) { |
|
| 157 | + $output->writeln('<error>Unknown configuration for backends "' . $key . '"</error>'); |
|
| 158 | + return 1; |
|
| 159 | + } |
|
| 160 | + $config[$key] = $value; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $mount = new StorageConfig(); |
|
| 164 | + $mount->setMountPoint($mountPoint); |
|
| 165 | + $mount->setBackend($storageBackend); |
|
| 166 | + $mount->setAuthMechanism($authBackend); |
|
| 167 | + $mount->setBackendOptions($config); |
|
| 168 | + |
|
| 169 | + if ($user) { |
|
| 170 | + if (!$this->userManager->userExists($user)) { |
|
| 171 | + $output->writeln('<error>User "' . $user . '" not found</error>'); |
|
| 172 | + return 1; |
|
| 173 | + } |
|
| 174 | + $mount->setApplicableUsers([$user]); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if ($input->getOption('dry')) { |
|
| 178 | + $this->showMount($user, $mount, $input, $output); |
|
| 179 | + } else { |
|
| 180 | + $this->getStorageService($user)->addStorage($mount); |
|
| 181 | + if ($input->getOption('output') === self::OUTPUT_FORMAT_PLAIN) { |
|
| 182 | + $output->writeln('<info>Storage created with id ' . $mount->getId() . '</info>'); |
|
| 183 | + } else { |
|
| 184 | + $output->writeln((string)$mount->getId()); |
|
| 185 | + } |
|
| 186 | + } |
|
| 187 | + return 0; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + private function validateParam($key, &$value, Backend $storageBackend, AuthMechanism $authBackend) { |
|
| 191 | + $params = array_merge($storageBackend->getParameters(), $authBackend->getParameters()); |
|
| 192 | + foreach ($params as $param) { |
|
| 193 | + /** @var DefinitionParameter $param */ |
|
| 194 | + if ($param->getName() === $key) { |
|
| 195 | + if ($param->getType() === DefinitionParameter::VALUE_BOOLEAN) { |
|
| 196 | + $value = ($value === 'true'); |
|
| 197 | + } |
|
| 198 | + return true; |
|
| 199 | + } |
|
| 200 | + } |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + private function showMount($user, StorageConfig $mount, InputInterface $input, OutputInterface $output) { |
|
| 205 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 206 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 207 | + $listInput->setOption('output', $input->getOption('output')); |
|
| 208 | + $listInput->setOption('show-password', true); |
|
| 209 | + $listCommand->listMounts($user, [$mount], $listInput, $output); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + protected function getStorageService($userId) { |
|
| 213 | + if (!empty($userId)) { |
|
| 214 | + $user = $this->userManager->get($userId); |
|
| 215 | + if (is_null($user)) { |
|
| 216 | + throw new NoUserException("user $userId not found"); |
|
| 217 | + } |
|
| 218 | + $this->userSession->setUser($user); |
|
| 219 | + return $this->userService; |
|
| 220 | + } else { |
|
| 221 | + return $this->globalService; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | 224 | } |
@@ -36,398 +36,398 @@ |
||
| 36 | 36 | * External storage configuration |
| 37 | 37 | */ |
| 38 | 38 | class StorageConfig implements \JsonSerializable { |
| 39 | - public const MOUNT_TYPE_ADMIN = 1; |
|
| 40 | - public const MOUNT_TYPE_PERSONAl = 2; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Storage config id |
|
| 44 | - * |
|
| 45 | - * @var int |
|
| 46 | - */ |
|
| 47 | - private $id; |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Backend |
|
| 51 | - * |
|
| 52 | - * @var Backend |
|
| 53 | - */ |
|
| 54 | - private $backend; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Authentication mechanism |
|
| 58 | - * |
|
| 59 | - * @var AuthMechanism |
|
| 60 | - */ |
|
| 61 | - private $authMechanism; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Backend options |
|
| 65 | - * |
|
| 66 | - * @var array |
|
| 67 | - */ |
|
| 68 | - private $backendOptions = []; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Mount point path, relative to the user's "files" folder |
|
| 72 | - * |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - private $mountPoint; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Storage status |
|
| 79 | - * |
|
| 80 | - * @var int |
|
| 81 | - */ |
|
| 82 | - private $status; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Status message |
|
| 86 | - * |
|
| 87 | - * @var string |
|
| 88 | - */ |
|
| 89 | - private $statusMessage; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Priority |
|
| 93 | - * |
|
| 94 | - * @var int |
|
| 95 | - */ |
|
| 96 | - private $priority; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * List of users who have access to this storage |
|
| 100 | - * |
|
| 101 | - * @var string[] |
|
| 102 | - */ |
|
| 103 | - private $applicableUsers = []; |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * List of groups that have access to this storage |
|
| 107 | - * |
|
| 108 | - * @var string[] |
|
| 109 | - */ |
|
| 110 | - private $applicableGroups = []; |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Mount-specific options |
|
| 114 | - * |
|
| 115 | - * @var array |
|
| 116 | - */ |
|
| 117 | - private $mountOptions = []; |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Whether it's a personal or admin mount |
|
| 121 | - * |
|
| 122 | - * @var int |
|
| 123 | - */ |
|
| 124 | - private $type; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Creates a storage config |
|
| 128 | - * |
|
| 129 | - * @param int|null $id config id or null for a new config |
|
| 130 | - */ |
|
| 131 | - public function __construct($id = null) { |
|
| 132 | - $this->id = $id; |
|
| 133 | - $this->mountOptions['enable_sharing'] = false; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Returns the configuration id |
|
| 138 | - * |
|
| 139 | - * @return int |
|
| 140 | - */ |
|
| 141 | - public function getId() { |
|
| 142 | - return $this->id; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Sets the configuration id |
|
| 147 | - * |
|
| 148 | - * @param int $id configuration id |
|
| 149 | - */ |
|
| 150 | - public function setId($id) { |
|
| 151 | - $this->id = $id; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Returns mount point path relative to the user's |
|
| 156 | - * "files" folder. |
|
| 157 | - * |
|
| 158 | - * @return string path |
|
| 159 | - */ |
|
| 160 | - public function getMountPoint() { |
|
| 161 | - return $this->mountPoint; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Sets mount point path relative to the user's |
|
| 166 | - * "files" folder. |
|
| 167 | - * The path will be normalized. |
|
| 168 | - * |
|
| 169 | - * @param string $mountPoint path |
|
| 170 | - */ |
|
| 171 | - public function setMountPoint($mountPoint) { |
|
| 172 | - $this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @return Backend |
|
| 177 | - */ |
|
| 178 | - public function getBackend() { |
|
| 179 | - return $this->backend; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param Backend $backend |
|
| 184 | - */ |
|
| 185 | - public function setBackend(Backend $backend) { |
|
| 186 | - $this->backend = $backend; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @return AuthMechanism |
|
| 191 | - */ |
|
| 192 | - public function getAuthMechanism() { |
|
| 193 | - return $this->authMechanism; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param AuthMechanism $authMechanism |
|
| 198 | - */ |
|
| 199 | - public function setAuthMechanism(AuthMechanism $authMechanism) { |
|
| 200 | - $this->authMechanism = $authMechanism; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Returns the external storage backend-specific options |
|
| 205 | - * |
|
| 206 | - * @return array backend options |
|
| 207 | - */ |
|
| 208 | - public function getBackendOptions() { |
|
| 209 | - return $this->backendOptions; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Sets the external storage backend-specific options |
|
| 214 | - * |
|
| 215 | - * @param array $backendOptions backend options |
|
| 216 | - */ |
|
| 217 | - public function setBackendOptions($backendOptions) { |
|
| 218 | - if ($this->getBackend() instanceof Backend) { |
|
| 219 | - $parameters = $this->getBackend()->getParameters(); |
|
| 220 | - foreach ($backendOptions as $key => $value) { |
|
| 221 | - if (isset($parameters[$key])) { |
|
| 222 | - switch ($parameters[$key]->getType()) { |
|
| 223 | - case \OCA\Files_External\Lib\DefinitionParameter::VALUE_BOOLEAN: |
|
| 224 | - $value = (bool)$value; |
|
| 225 | - break; |
|
| 226 | - } |
|
| 227 | - $backendOptions[$key] = $value; |
|
| 228 | - } |
|
| 229 | - } |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - $this->backendOptions = $backendOptions; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * @param string $key |
|
| 237 | - * @return mixed |
|
| 238 | - */ |
|
| 239 | - public function getBackendOption($key) { |
|
| 240 | - if (isset($this->backendOptions[$key])) { |
|
| 241 | - return $this->backendOptions[$key]; |
|
| 242 | - } |
|
| 243 | - return null; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * @param string $key |
|
| 248 | - * @param mixed $value |
|
| 249 | - */ |
|
| 250 | - public function setBackendOption($key, $value) { |
|
| 251 | - $this->backendOptions[$key] = $value; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Returns the mount priority |
|
| 256 | - * |
|
| 257 | - * @return int priority |
|
| 258 | - */ |
|
| 259 | - public function getPriority() { |
|
| 260 | - return $this->priority; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Sets the mount priotity |
|
| 265 | - * |
|
| 266 | - * @param int $priority priority |
|
| 267 | - */ |
|
| 268 | - public function setPriority($priority) { |
|
| 269 | - $this->priority = $priority; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * Returns the users for which to mount this storage |
|
| 274 | - * |
|
| 275 | - * @return string[] applicable users |
|
| 276 | - */ |
|
| 277 | - public function getApplicableUsers() { |
|
| 278 | - return $this->applicableUsers; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * Sets the users for which to mount this storage |
|
| 283 | - * |
|
| 284 | - * @param string[]|null $applicableUsers applicable users |
|
| 285 | - */ |
|
| 286 | - public function setApplicableUsers($applicableUsers) { |
|
| 287 | - if (is_null($applicableUsers)) { |
|
| 288 | - $applicableUsers = []; |
|
| 289 | - } |
|
| 290 | - $this->applicableUsers = $applicableUsers; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Returns the groups for which to mount this storage |
|
| 295 | - * |
|
| 296 | - * @return string[] applicable groups |
|
| 297 | - */ |
|
| 298 | - public function getApplicableGroups() { |
|
| 299 | - return $this->applicableGroups; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Sets the groups for which to mount this storage |
|
| 304 | - * |
|
| 305 | - * @param string[]|null $applicableGroups applicable groups |
|
| 306 | - */ |
|
| 307 | - public function setApplicableGroups($applicableGroups) { |
|
| 308 | - if (is_null($applicableGroups)) { |
|
| 309 | - $applicableGroups = []; |
|
| 310 | - } |
|
| 311 | - $this->applicableGroups = $applicableGroups; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Returns the mount-specific options |
|
| 316 | - * |
|
| 317 | - * @return array mount specific options |
|
| 318 | - */ |
|
| 319 | - public function getMountOptions() { |
|
| 320 | - return $this->mountOptions; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Sets the mount-specific options |
|
| 325 | - * |
|
| 326 | - * @param array $mountOptions applicable groups |
|
| 327 | - */ |
|
| 328 | - public function setMountOptions($mountOptions) { |
|
| 329 | - if (is_null($mountOptions)) { |
|
| 330 | - $mountOptions = []; |
|
| 331 | - } |
|
| 332 | - $this->mountOptions = $mountOptions; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @param string $key |
|
| 337 | - * @return mixed |
|
| 338 | - */ |
|
| 339 | - public function getMountOption($key) { |
|
| 340 | - if (isset($this->mountOptions[$key])) { |
|
| 341 | - return $this->mountOptions[$key]; |
|
| 342 | - } |
|
| 343 | - return null; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * @param string $key |
|
| 348 | - * @param mixed $value |
|
| 349 | - */ |
|
| 350 | - public function setMountOption($key, $value) { |
|
| 351 | - $this->mountOptions[$key] = $value; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Gets the storage status, whether the config worked last time |
|
| 356 | - * |
|
| 357 | - * @return int $status status |
|
| 358 | - */ |
|
| 359 | - public function getStatus() { |
|
| 360 | - return $this->status; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * Gets the message describing the storage status |
|
| 365 | - * |
|
| 366 | - * @return string|null |
|
| 367 | - */ |
|
| 368 | - public function getStatusMessage() { |
|
| 369 | - return $this->statusMessage; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Sets the storage status, whether the config worked last time |
|
| 374 | - * |
|
| 375 | - * @param int $status status |
|
| 376 | - * @param string|null $message optional message |
|
| 377 | - */ |
|
| 378 | - public function setStatus($status, $message = null) { |
|
| 379 | - $this->status = $status; |
|
| 380 | - $this->statusMessage = $message; |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl |
|
| 385 | - */ |
|
| 386 | - public function getType() { |
|
| 387 | - return $this->type; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl |
|
| 392 | - */ |
|
| 393 | - public function setType($type) { |
|
| 394 | - $this->type = $type; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - /** |
|
| 398 | - * Serialize config to JSON |
|
| 399 | - * |
|
| 400 | - * @return array |
|
| 401 | - */ |
|
| 402 | - public function jsonSerialize() { |
|
| 403 | - $result = []; |
|
| 404 | - if (!is_null($this->id)) { |
|
| 405 | - $result['id'] = $this->id; |
|
| 406 | - } |
|
| 407 | - $result['mountPoint'] = $this->mountPoint; |
|
| 408 | - $result['backend'] = $this->backend->getIdentifier(); |
|
| 409 | - $result['authMechanism'] = $this->authMechanism->getIdentifier(); |
|
| 410 | - $result['backendOptions'] = $this->backendOptions; |
|
| 411 | - if (!is_null($this->priority)) { |
|
| 412 | - $result['priority'] = $this->priority; |
|
| 413 | - } |
|
| 414 | - if (!empty($this->applicableUsers)) { |
|
| 415 | - $result['applicableUsers'] = $this->applicableUsers; |
|
| 416 | - } |
|
| 417 | - if (!empty($this->applicableGroups)) { |
|
| 418 | - $result['applicableGroups'] = $this->applicableGroups; |
|
| 419 | - } |
|
| 420 | - if (!empty($this->mountOptions)) { |
|
| 421 | - $result['mountOptions'] = $this->mountOptions; |
|
| 422 | - } |
|
| 423 | - if (!is_null($this->status)) { |
|
| 424 | - $result['status'] = $this->status; |
|
| 425 | - } |
|
| 426 | - if (!is_null($this->statusMessage)) { |
|
| 427 | - $result['statusMessage'] = $this->statusMessage; |
|
| 428 | - } |
|
| 429 | - $result['userProvided'] = $this->authMechanism instanceof IUserProvided; |
|
| 430 | - $result['type'] = ($this->getType() === self::MOUNT_TYPE_PERSONAl) ? 'personal': 'system'; |
|
| 431 | - return $result; |
|
| 432 | - } |
|
| 39 | + public const MOUNT_TYPE_ADMIN = 1; |
|
| 40 | + public const MOUNT_TYPE_PERSONAl = 2; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Storage config id |
|
| 44 | + * |
|
| 45 | + * @var int |
|
| 46 | + */ |
|
| 47 | + private $id; |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Backend |
|
| 51 | + * |
|
| 52 | + * @var Backend |
|
| 53 | + */ |
|
| 54 | + private $backend; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Authentication mechanism |
|
| 58 | + * |
|
| 59 | + * @var AuthMechanism |
|
| 60 | + */ |
|
| 61 | + private $authMechanism; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Backend options |
|
| 65 | + * |
|
| 66 | + * @var array |
|
| 67 | + */ |
|
| 68 | + private $backendOptions = []; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Mount point path, relative to the user's "files" folder |
|
| 72 | + * |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + private $mountPoint; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Storage status |
|
| 79 | + * |
|
| 80 | + * @var int |
|
| 81 | + */ |
|
| 82 | + private $status; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Status message |
|
| 86 | + * |
|
| 87 | + * @var string |
|
| 88 | + */ |
|
| 89 | + private $statusMessage; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Priority |
|
| 93 | + * |
|
| 94 | + * @var int |
|
| 95 | + */ |
|
| 96 | + private $priority; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * List of users who have access to this storage |
|
| 100 | + * |
|
| 101 | + * @var string[] |
|
| 102 | + */ |
|
| 103 | + private $applicableUsers = []; |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * List of groups that have access to this storage |
|
| 107 | + * |
|
| 108 | + * @var string[] |
|
| 109 | + */ |
|
| 110 | + private $applicableGroups = []; |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Mount-specific options |
|
| 114 | + * |
|
| 115 | + * @var array |
|
| 116 | + */ |
|
| 117 | + private $mountOptions = []; |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Whether it's a personal or admin mount |
|
| 121 | + * |
|
| 122 | + * @var int |
|
| 123 | + */ |
|
| 124 | + private $type; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Creates a storage config |
|
| 128 | + * |
|
| 129 | + * @param int|null $id config id or null for a new config |
|
| 130 | + */ |
|
| 131 | + public function __construct($id = null) { |
|
| 132 | + $this->id = $id; |
|
| 133 | + $this->mountOptions['enable_sharing'] = false; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Returns the configuration id |
|
| 138 | + * |
|
| 139 | + * @return int |
|
| 140 | + */ |
|
| 141 | + public function getId() { |
|
| 142 | + return $this->id; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Sets the configuration id |
|
| 147 | + * |
|
| 148 | + * @param int $id configuration id |
|
| 149 | + */ |
|
| 150 | + public function setId($id) { |
|
| 151 | + $this->id = $id; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Returns mount point path relative to the user's |
|
| 156 | + * "files" folder. |
|
| 157 | + * |
|
| 158 | + * @return string path |
|
| 159 | + */ |
|
| 160 | + public function getMountPoint() { |
|
| 161 | + return $this->mountPoint; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Sets mount point path relative to the user's |
|
| 166 | + * "files" folder. |
|
| 167 | + * The path will be normalized. |
|
| 168 | + * |
|
| 169 | + * @param string $mountPoint path |
|
| 170 | + */ |
|
| 171 | + public function setMountPoint($mountPoint) { |
|
| 172 | + $this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @return Backend |
|
| 177 | + */ |
|
| 178 | + public function getBackend() { |
|
| 179 | + return $this->backend; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param Backend $backend |
|
| 184 | + */ |
|
| 185 | + public function setBackend(Backend $backend) { |
|
| 186 | + $this->backend = $backend; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @return AuthMechanism |
|
| 191 | + */ |
|
| 192 | + public function getAuthMechanism() { |
|
| 193 | + return $this->authMechanism; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param AuthMechanism $authMechanism |
|
| 198 | + */ |
|
| 199 | + public function setAuthMechanism(AuthMechanism $authMechanism) { |
|
| 200 | + $this->authMechanism = $authMechanism; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Returns the external storage backend-specific options |
|
| 205 | + * |
|
| 206 | + * @return array backend options |
|
| 207 | + */ |
|
| 208 | + public function getBackendOptions() { |
|
| 209 | + return $this->backendOptions; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Sets the external storage backend-specific options |
|
| 214 | + * |
|
| 215 | + * @param array $backendOptions backend options |
|
| 216 | + */ |
|
| 217 | + public function setBackendOptions($backendOptions) { |
|
| 218 | + if ($this->getBackend() instanceof Backend) { |
|
| 219 | + $parameters = $this->getBackend()->getParameters(); |
|
| 220 | + foreach ($backendOptions as $key => $value) { |
|
| 221 | + if (isset($parameters[$key])) { |
|
| 222 | + switch ($parameters[$key]->getType()) { |
|
| 223 | + case \OCA\Files_External\Lib\DefinitionParameter::VALUE_BOOLEAN: |
|
| 224 | + $value = (bool)$value; |
|
| 225 | + break; |
|
| 226 | + } |
|
| 227 | + $backendOptions[$key] = $value; |
|
| 228 | + } |
|
| 229 | + } |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + $this->backendOptions = $backendOptions; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * @param string $key |
|
| 237 | + * @return mixed |
|
| 238 | + */ |
|
| 239 | + public function getBackendOption($key) { |
|
| 240 | + if (isset($this->backendOptions[$key])) { |
|
| 241 | + return $this->backendOptions[$key]; |
|
| 242 | + } |
|
| 243 | + return null; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * @param string $key |
|
| 248 | + * @param mixed $value |
|
| 249 | + */ |
|
| 250 | + public function setBackendOption($key, $value) { |
|
| 251 | + $this->backendOptions[$key] = $value; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Returns the mount priority |
|
| 256 | + * |
|
| 257 | + * @return int priority |
|
| 258 | + */ |
|
| 259 | + public function getPriority() { |
|
| 260 | + return $this->priority; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Sets the mount priotity |
|
| 265 | + * |
|
| 266 | + * @param int $priority priority |
|
| 267 | + */ |
|
| 268 | + public function setPriority($priority) { |
|
| 269 | + $this->priority = $priority; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * Returns the users for which to mount this storage |
|
| 274 | + * |
|
| 275 | + * @return string[] applicable users |
|
| 276 | + */ |
|
| 277 | + public function getApplicableUsers() { |
|
| 278 | + return $this->applicableUsers; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * Sets the users for which to mount this storage |
|
| 283 | + * |
|
| 284 | + * @param string[]|null $applicableUsers applicable users |
|
| 285 | + */ |
|
| 286 | + public function setApplicableUsers($applicableUsers) { |
|
| 287 | + if (is_null($applicableUsers)) { |
|
| 288 | + $applicableUsers = []; |
|
| 289 | + } |
|
| 290 | + $this->applicableUsers = $applicableUsers; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Returns the groups for which to mount this storage |
|
| 295 | + * |
|
| 296 | + * @return string[] applicable groups |
|
| 297 | + */ |
|
| 298 | + public function getApplicableGroups() { |
|
| 299 | + return $this->applicableGroups; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Sets the groups for which to mount this storage |
|
| 304 | + * |
|
| 305 | + * @param string[]|null $applicableGroups applicable groups |
|
| 306 | + */ |
|
| 307 | + public function setApplicableGroups($applicableGroups) { |
|
| 308 | + if (is_null($applicableGroups)) { |
|
| 309 | + $applicableGroups = []; |
|
| 310 | + } |
|
| 311 | + $this->applicableGroups = $applicableGroups; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Returns the mount-specific options |
|
| 316 | + * |
|
| 317 | + * @return array mount specific options |
|
| 318 | + */ |
|
| 319 | + public function getMountOptions() { |
|
| 320 | + return $this->mountOptions; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Sets the mount-specific options |
|
| 325 | + * |
|
| 326 | + * @param array $mountOptions applicable groups |
|
| 327 | + */ |
|
| 328 | + public function setMountOptions($mountOptions) { |
|
| 329 | + if (is_null($mountOptions)) { |
|
| 330 | + $mountOptions = []; |
|
| 331 | + } |
|
| 332 | + $this->mountOptions = $mountOptions; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @param string $key |
|
| 337 | + * @return mixed |
|
| 338 | + */ |
|
| 339 | + public function getMountOption($key) { |
|
| 340 | + if (isset($this->mountOptions[$key])) { |
|
| 341 | + return $this->mountOptions[$key]; |
|
| 342 | + } |
|
| 343 | + return null; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * @param string $key |
|
| 348 | + * @param mixed $value |
|
| 349 | + */ |
|
| 350 | + public function setMountOption($key, $value) { |
|
| 351 | + $this->mountOptions[$key] = $value; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Gets the storage status, whether the config worked last time |
|
| 356 | + * |
|
| 357 | + * @return int $status status |
|
| 358 | + */ |
|
| 359 | + public function getStatus() { |
|
| 360 | + return $this->status; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * Gets the message describing the storage status |
|
| 365 | + * |
|
| 366 | + * @return string|null |
|
| 367 | + */ |
|
| 368 | + public function getStatusMessage() { |
|
| 369 | + return $this->statusMessage; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Sets the storage status, whether the config worked last time |
|
| 374 | + * |
|
| 375 | + * @param int $status status |
|
| 376 | + * @param string|null $message optional message |
|
| 377 | + */ |
|
| 378 | + public function setStatus($status, $message = null) { |
|
| 379 | + $this->status = $status; |
|
| 380 | + $this->statusMessage = $message; |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl |
|
| 385 | + */ |
|
| 386 | + public function getType() { |
|
| 387 | + return $this->type; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl |
|
| 392 | + */ |
|
| 393 | + public function setType($type) { |
|
| 394 | + $this->type = $type; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + /** |
|
| 398 | + * Serialize config to JSON |
|
| 399 | + * |
|
| 400 | + * @return array |
|
| 401 | + */ |
|
| 402 | + public function jsonSerialize() { |
|
| 403 | + $result = []; |
|
| 404 | + if (!is_null($this->id)) { |
|
| 405 | + $result['id'] = $this->id; |
|
| 406 | + } |
|
| 407 | + $result['mountPoint'] = $this->mountPoint; |
|
| 408 | + $result['backend'] = $this->backend->getIdentifier(); |
|
| 409 | + $result['authMechanism'] = $this->authMechanism->getIdentifier(); |
|
| 410 | + $result['backendOptions'] = $this->backendOptions; |
|
| 411 | + if (!is_null($this->priority)) { |
|
| 412 | + $result['priority'] = $this->priority; |
|
| 413 | + } |
|
| 414 | + if (!empty($this->applicableUsers)) { |
|
| 415 | + $result['applicableUsers'] = $this->applicableUsers; |
|
| 416 | + } |
|
| 417 | + if (!empty($this->applicableGroups)) { |
|
| 418 | + $result['applicableGroups'] = $this->applicableGroups; |
|
| 419 | + } |
|
| 420 | + if (!empty($this->mountOptions)) { |
|
| 421 | + $result['mountOptions'] = $this->mountOptions; |
|
| 422 | + } |
|
| 423 | + if (!is_null($this->status)) { |
|
| 424 | + $result['status'] = $this->status; |
|
| 425 | + } |
|
| 426 | + if (!is_null($this->statusMessage)) { |
|
| 427 | + $result['statusMessage'] = $this->statusMessage; |
|
| 428 | + } |
|
| 429 | + $result['userProvided'] = $this->authMechanism instanceof IUserProvided; |
|
| 430 | + $result['type'] = ($this->getType() === self::MOUNT_TYPE_PERSONAl) ? 'personal': 'system'; |
|
| 431 | + return $result; |
|
| 432 | + } |
|
| 433 | 433 | } |
@@ -78,108 +78,108 @@ |
||
| 78 | 78 | */ |
| 79 | 79 | class Application extends App implements IBackendProvider, IAuthMechanismProvider, IBootstrap { |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Application constructor. |
|
| 83 | - * |
|
| 84 | - * @throws \OCP\AppFramework\QueryException |
|
| 85 | - */ |
|
| 86 | - public function __construct(array $urlParams = []) { |
|
| 87 | - parent::__construct('files_external', $urlParams); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function register(IRegistrationContext $context): void { |
|
| 91 | - $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); |
|
| 92 | - $context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function boot(IBootContext $context): void { |
|
| 96 | - $context->injectFn(function (IMountProviderCollection $mountProviderCollection, ConfigAdapter $configAdapter) { |
|
| 97 | - $mountProviderCollection->registerProvider($configAdapter); |
|
| 98 | - }); |
|
| 99 | - \OCA\Files\App::getNavigationManager()->add(function () { |
|
| 100 | - $l = \OC::$server->getL10N('files_external'); |
|
| 101 | - return [ |
|
| 102 | - 'id' => 'extstoragemounts', |
|
| 103 | - 'appname' => 'files_external', |
|
| 104 | - 'script' => 'list.php', |
|
| 105 | - 'order' => 30, |
|
| 106 | - 'name' => $l->t('External storage'), |
|
| 107 | - ]; |
|
| 108 | - }); |
|
| 109 | - $context->injectFn(function (BackendService $backendService, UserPlaceholderHandler $userConfigHandler) { |
|
| 110 | - $backendService->registerBackendProvider($this); |
|
| 111 | - $backendService->registerAuthMechanismProvider($this); |
|
| 112 | - $backendService->registerConfigHandler('user', function () use ($userConfigHandler) { |
|
| 113 | - return $userConfigHandler; |
|
| 114 | - }); |
|
| 115 | - }); |
|
| 116 | - |
|
| 117 | - // force-load auth mechanisms since some will register hooks |
|
| 118 | - // TODO: obsolete these and use the TokenProvider to get the user's password from the session |
|
| 119 | - $this->getAuthMechanisms(); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @{inheritdoc} |
|
| 124 | - */ |
|
| 125 | - public function getBackends() { |
|
| 126 | - $container = $this->getContainer(); |
|
| 127 | - |
|
| 128 | - $backends = [ |
|
| 129 | - $container->query(Local::class), |
|
| 130 | - $container->query(FTP::class), |
|
| 131 | - $container->query(DAV::class), |
|
| 132 | - $container->query(OwnCloud::class), |
|
| 133 | - $container->query(SFTP::class), |
|
| 134 | - $container->query(AmazonS3::class), |
|
| 135 | - $container->query(Swift::class), |
|
| 136 | - $container->query(SFTP_Key::class), |
|
| 137 | - $container->query(SMB::class), |
|
| 138 | - $container->query(SMB_OC::class), |
|
| 139 | - ]; |
|
| 140 | - |
|
| 141 | - return $backends; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @{inheritdoc} |
|
| 146 | - */ |
|
| 147 | - public function getAuthMechanisms() { |
|
| 148 | - $container = $this->getContainer(); |
|
| 149 | - |
|
| 150 | - return [ |
|
| 151 | - // AuthMechanism::SCHEME_NULL mechanism |
|
| 152 | - $container->query(NullMechanism::class), |
|
| 153 | - |
|
| 154 | - // AuthMechanism::SCHEME_BUILTIN mechanism |
|
| 155 | - $container->query(Builtin::class), |
|
| 156 | - |
|
| 157 | - // AuthMechanism::SCHEME_PASSWORD mechanisms |
|
| 158 | - $container->query(Password::class), |
|
| 159 | - $container->query(SessionCredentials::class), |
|
| 160 | - $container->query(LoginCredentials::class), |
|
| 161 | - $container->query(UserProvided::class), |
|
| 162 | - $container->query(GlobalAuth::class), |
|
| 163 | - $container->query(UserGlobalAuth::class), |
|
| 164 | - |
|
| 165 | - // AuthMechanism::SCHEME_OAUTH1 mechanisms |
|
| 166 | - $container->query(OAuth1::class), |
|
| 167 | - |
|
| 168 | - // AuthMechanism::SCHEME_OAUTH2 mechanisms |
|
| 169 | - $container->query(OAuth2::class), |
|
| 170 | - |
|
| 171 | - // AuthMechanism::SCHEME_PUBLICKEY mechanisms |
|
| 172 | - $container->query(RSA::class), |
|
| 173 | - $container->query(RSAPrivateKey::class), |
|
| 174 | - |
|
| 175 | - // AuthMechanism::SCHEME_OPENSTACK mechanisms |
|
| 176 | - $container->query(OpenStackV2::class), |
|
| 177 | - $container->query(OpenStackV3::class), |
|
| 178 | - $container->query(Rackspace::class), |
|
| 179 | - |
|
| 180 | - // Specialized mechanisms |
|
| 181 | - $container->query(AccessKey::class), |
|
| 182 | - $container->query(KerberosAuth::class), |
|
| 183 | - ]; |
|
| 184 | - } |
|
| 81 | + /** |
|
| 82 | + * Application constructor. |
|
| 83 | + * |
|
| 84 | + * @throws \OCP\AppFramework\QueryException |
|
| 85 | + */ |
|
| 86 | + public function __construct(array $urlParams = []) { |
|
| 87 | + parent::__construct('files_external', $urlParams); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function register(IRegistrationContext $context): void { |
|
| 91 | + $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); |
|
| 92 | + $context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function boot(IBootContext $context): void { |
|
| 96 | + $context->injectFn(function (IMountProviderCollection $mountProviderCollection, ConfigAdapter $configAdapter) { |
|
| 97 | + $mountProviderCollection->registerProvider($configAdapter); |
|
| 98 | + }); |
|
| 99 | + \OCA\Files\App::getNavigationManager()->add(function () { |
|
| 100 | + $l = \OC::$server->getL10N('files_external'); |
|
| 101 | + return [ |
|
| 102 | + 'id' => 'extstoragemounts', |
|
| 103 | + 'appname' => 'files_external', |
|
| 104 | + 'script' => 'list.php', |
|
| 105 | + 'order' => 30, |
|
| 106 | + 'name' => $l->t('External storage'), |
|
| 107 | + ]; |
|
| 108 | + }); |
|
| 109 | + $context->injectFn(function (BackendService $backendService, UserPlaceholderHandler $userConfigHandler) { |
|
| 110 | + $backendService->registerBackendProvider($this); |
|
| 111 | + $backendService->registerAuthMechanismProvider($this); |
|
| 112 | + $backendService->registerConfigHandler('user', function () use ($userConfigHandler) { |
|
| 113 | + return $userConfigHandler; |
|
| 114 | + }); |
|
| 115 | + }); |
|
| 116 | + |
|
| 117 | + // force-load auth mechanisms since some will register hooks |
|
| 118 | + // TODO: obsolete these and use the TokenProvider to get the user's password from the session |
|
| 119 | + $this->getAuthMechanisms(); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @{inheritdoc} |
|
| 124 | + */ |
|
| 125 | + public function getBackends() { |
|
| 126 | + $container = $this->getContainer(); |
|
| 127 | + |
|
| 128 | + $backends = [ |
|
| 129 | + $container->query(Local::class), |
|
| 130 | + $container->query(FTP::class), |
|
| 131 | + $container->query(DAV::class), |
|
| 132 | + $container->query(OwnCloud::class), |
|
| 133 | + $container->query(SFTP::class), |
|
| 134 | + $container->query(AmazonS3::class), |
|
| 135 | + $container->query(Swift::class), |
|
| 136 | + $container->query(SFTP_Key::class), |
|
| 137 | + $container->query(SMB::class), |
|
| 138 | + $container->query(SMB_OC::class), |
|
| 139 | + ]; |
|
| 140 | + |
|
| 141 | + return $backends; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @{inheritdoc} |
|
| 146 | + */ |
|
| 147 | + public function getAuthMechanisms() { |
|
| 148 | + $container = $this->getContainer(); |
|
| 149 | + |
|
| 150 | + return [ |
|
| 151 | + // AuthMechanism::SCHEME_NULL mechanism |
|
| 152 | + $container->query(NullMechanism::class), |
|
| 153 | + |
|
| 154 | + // AuthMechanism::SCHEME_BUILTIN mechanism |
|
| 155 | + $container->query(Builtin::class), |
|
| 156 | + |
|
| 157 | + // AuthMechanism::SCHEME_PASSWORD mechanisms |
|
| 158 | + $container->query(Password::class), |
|
| 159 | + $container->query(SessionCredentials::class), |
|
| 160 | + $container->query(LoginCredentials::class), |
|
| 161 | + $container->query(UserProvided::class), |
|
| 162 | + $container->query(GlobalAuth::class), |
|
| 163 | + $container->query(UserGlobalAuth::class), |
|
| 164 | + |
|
| 165 | + // AuthMechanism::SCHEME_OAUTH1 mechanisms |
|
| 166 | + $container->query(OAuth1::class), |
|
| 167 | + |
|
| 168 | + // AuthMechanism::SCHEME_OAUTH2 mechanisms |
|
| 169 | + $container->query(OAuth2::class), |
|
| 170 | + |
|
| 171 | + // AuthMechanism::SCHEME_PUBLICKEY mechanisms |
|
| 172 | + $container->query(RSA::class), |
|
| 173 | + $container->query(RSAPrivateKey::class), |
|
| 174 | + |
|
| 175 | + // AuthMechanism::SCHEME_OPENSTACK mechanisms |
|
| 176 | + $container->query(OpenStackV2::class), |
|
| 177 | + $container->query(OpenStackV3::class), |
|
| 178 | + $container->query(Rackspace::class), |
|
| 179 | + |
|
| 180 | + // Specialized mechanisms |
|
| 181 | + $container->query(AccessKey::class), |
|
| 182 | + $container->query(KerberosAuth::class), |
|
| 183 | + ]; |
|
| 184 | + } |
|
| 185 | 185 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | use OCP\Group\Events\GroupDeletedEvent; |
| 72 | 72 | use OCP\User\Events\UserDeletedEvent; |
| 73 | 73 | |
| 74 | -require_once __DIR__ . '/../../3rdparty/autoload.php'; |
|
| 74 | +require_once __DIR__.'/../../3rdparty/autoload.php'; |
|
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | 77 | * @package OCA\Files_External\AppInfo |
@@ -93,10 +93,10 @@ discard block |
||
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | public function boot(IBootContext $context): void { |
| 96 | - $context->injectFn(function (IMountProviderCollection $mountProviderCollection, ConfigAdapter $configAdapter) { |
|
| 96 | + $context->injectFn(function(IMountProviderCollection $mountProviderCollection, ConfigAdapter $configAdapter) { |
|
| 97 | 97 | $mountProviderCollection->registerProvider($configAdapter); |
| 98 | 98 | }); |
| 99 | - \OCA\Files\App::getNavigationManager()->add(function () { |
|
| 99 | + \OCA\Files\App::getNavigationManager()->add(function() { |
|
| 100 | 100 | $l = \OC::$server->getL10N('files_external'); |
| 101 | 101 | return [ |
| 102 | 102 | 'id' => 'extstoragemounts', |
@@ -106,10 +106,10 @@ discard block |
||
| 106 | 106 | 'name' => $l->t('External storage'), |
| 107 | 107 | ]; |
| 108 | 108 | }); |
| 109 | - $context->injectFn(function (BackendService $backendService, UserPlaceholderHandler $userConfigHandler) { |
|
| 109 | + $context->injectFn(function(BackendService $backendService, UserPlaceholderHandler $userConfigHandler) { |
|
| 110 | 110 | $backendService->registerBackendProvider($this); |
| 111 | 111 | $backendService->registerAuthMechanismProvider($this); |
| 112 | - $backendService->registerConfigHandler('user', function () use ($userConfigHandler) { |
|
| 112 | + $backendService->registerConfigHandler('user', function() use ($userConfigHandler) { |
|
| 113 | 113 | return $userConfigHandler; |
| 114 | 114 | }); |
| 115 | 115 | }); |
@@ -29,17 +29,17 @@ |
||
| 29 | 29 | use OCP\User\Events\UserDeletedEvent; |
| 30 | 30 | |
| 31 | 31 | class UserDeletedListener implements IEventListener { |
| 32 | - /** @var DBConfigService */ |
|
| 33 | - private $config; |
|
| 32 | + /** @var DBConfigService */ |
|
| 33 | + private $config; |
|
| 34 | 34 | |
| 35 | - public function __construct(DBConfigService $config) { |
|
| 36 | - $this->config = $config; |
|
| 37 | - } |
|
| 35 | + public function __construct(DBConfigService $config) { |
|
| 36 | + $this->config = $config; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function handle(Event $event): void { |
|
| 40 | - if (!$event instanceof UserDeletedEvent) { |
|
| 41 | - return; |
|
| 42 | - } |
|
| 43 | - $this->config->modifyMountsOnUserDelete($event->getUser()->getUID()); |
|
| 44 | - } |
|
| 39 | + public function handle(Event $event): void { |
|
| 40 | + if (!$event instanceof UserDeletedEvent) { |
|
| 41 | + return; |
|
| 42 | + } |
|
| 43 | + $this->config->modifyMountsOnUserDelete($event->getUser()->getUID()); |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -29,17 +29,17 @@ |
||
| 29 | 29 | use OCP\Group\Events\GroupDeletedEvent; |
| 30 | 30 | |
| 31 | 31 | class GroupDeletedListener implements IEventListener { |
| 32 | - /** @var DBConfigService */ |
|
| 33 | - private $config; |
|
| 32 | + /** @var DBConfigService */ |
|
| 33 | + private $config; |
|
| 34 | 34 | |
| 35 | - public function __construct(DBConfigService $config) { |
|
| 36 | - $this->config = $config; |
|
| 37 | - } |
|
| 35 | + public function __construct(DBConfigService $config) { |
|
| 36 | + $this->config = $config; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function handle(Event $event): void { |
|
| 40 | - if (!$event instanceof GroupDeletedEvent) { |
|
| 41 | - return; |
|
| 42 | - } |
|
| 43 | - $this->config->modifyMountsOnGroupDelete($event->getGroup()->getGID()); |
|
| 44 | - } |
|
| 39 | + public function handle(Event $event): void { |
|
| 40 | + if (!$event instanceof GroupDeletedEvent) { |
|
| 41 | + return; |
|
| 42 | + } |
|
| 43 | + $this->config->modifyMountsOnGroupDelete($event->getGroup()->getGID()); |
|
| 44 | + } |
|
| 45 | 45 | } |