@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | public function getAdminMountsForMultiple($type, array $values) { |
| 180 | 180 | $builder = $this->connection->getQueryBuilder(); |
| 181 | - $params = array_map(function ($value) use ($builder) { |
|
| 181 | + $params = array_map(function($value) use ($builder) { |
|
| 182 | 182 | return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
| 183 | 183 | }, $values); |
| 184 | 184 | |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
| 232 | 232 | ]); |
| 233 | 233 | $query->execute(); |
| 234 | - return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
| 234 | + return (int) $this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | } |
| 368 | 368 | $uniqueMounts = array_values($uniqueMounts); |
| 369 | 369 | |
| 370 | - $mountIds = array_map(function ($mount) { |
|
| 370 | + $mountIds = array_map(function($mount) { |
|
| 371 | 371 | return $mount['mount_id']; |
| 372 | 372 | }, $uniqueMounts); |
| 373 | 373 | $mountIds = array_values(array_unique($mountIds)); |
@@ -376,9 +376,9 @@ discard block |
||
| 376 | 376 | $config = $this->getConfigForMounts($mountIds); |
| 377 | 377 | $options = $this->getOptionsForMounts($mountIds); |
| 378 | 378 | |
| 379 | - return array_map(function ($mount, $applicable, $config, $options) { |
|
| 380 | - $mount['type'] = (int)$mount['type']; |
|
| 381 | - $mount['priority'] = (int)$mount['priority']; |
|
| 379 | + return array_map(function($mount, $applicable, $config, $options) { |
|
| 380 | + $mount['type'] = (int) $mount['type']; |
|
| 381 | + $mount['priority'] = (int) $mount['priority']; |
|
| 382 | 382 | $mount['applicable'] = $applicable; |
| 383 | 383 | $mount['config'] = $config; |
| 384 | 384 | $mount['options'] = $options; |
@@ -400,7 +400,7 @@ discard block |
||
| 400 | 400 | } |
| 401 | 401 | $builder = $this->connection->getQueryBuilder(); |
| 402 | 402 | $fields[] = 'mount_id'; |
| 403 | - $placeHolders = array_map(function ($id) use ($builder) { |
|
| 403 | + $placeHolders = array_map(function($id) use ($builder) { |
|
| 404 | 404 | return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
| 405 | 405 | }, $mountIds); |
| 406 | 406 | $query = $builder->select($fields) |
@@ -414,7 +414,7 @@ discard block |
||
| 414 | 414 | } |
| 415 | 415 | foreach ($rows as $row) { |
| 416 | 416 | if (isset($row['type'])) { |
| 417 | - $row['type'] = (int)$row['type']; |
|
| 417 | + $row['type'] = (int) $row['type']; |
|
| 418 | 418 | } |
| 419 | 419 | $result[$row['mount_id']][] = $row; |
| 420 | 420 | } |
@@ -445,8 +445,8 @@ discard block |
||
| 445 | 445 | public function getOptionsForMounts($mountIds) { |
| 446 | 446 | $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
| 447 | 447 | $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
| 448 | - return array_map(function (array $options) { |
|
| 449 | - return array_map(function ($option) { |
|
| 448 | + return array_map(function(array $options) { |
|
| 449 | + return array_map(function($option) { |
|
| 450 | 450 | return json_decode($option); |
| 451 | 451 | }, $options); |
| 452 | 452 | }, $optionsMap); |
@@ -457,16 +457,16 @@ discard block |
||
| 457 | 457 | * @return array ['key1' => $value1, ...] |
| 458 | 458 | */ |
| 459 | 459 | private function createKeyValueMap(array $keyValuePairs) { |
| 460 | - $decryptedPairts = array_map(function ($pair) { |
|
| 460 | + $decryptedPairts = array_map(function($pair) { |
|
| 461 | 461 | if ($pair['key'] === 'password') { |
| 462 | 462 | $pair['value'] = $this->decryptValue($pair['value']); |
| 463 | 463 | } |
| 464 | 464 | return $pair; |
| 465 | 465 | }, $keyValuePairs); |
| 466 | - $keys = array_map(function ($pair) { |
|
| 466 | + $keys = array_map(function($pair) { |
|
| 467 | 467 | return $pair['key']; |
| 468 | 468 | }, $decryptedPairts); |
| 469 | - $values = array_map(function ($pair) { |
|
| 469 | + $values = array_map(function($pair) { |
|
| 470 | 470 | return $pair['value']; |
| 471 | 471 | }, $decryptedPairts); |
| 472 | 472 | |
@@ -32,460 +32,460 @@ |
||
| 32 | 32 | * Stores the mount config in the database |
| 33 | 33 | */ |
| 34 | 34 | class DBConfigService { |
| 35 | - const MOUNT_TYPE_ADMIN = 1; |
|
| 36 | - const MOUNT_TYPE_PERSONAl = 2; |
|
| 37 | - |
|
| 38 | - const APPLICABLE_TYPE_GLOBAL = 1; |
|
| 39 | - const APPLICABLE_TYPE_GROUP = 2; |
|
| 40 | - const APPLICABLE_TYPE_USER = 3; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var IDBConnection |
|
| 44 | - */ |
|
| 45 | - private $connection; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @var ICrypto |
|
| 49 | - */ |
|
| 50 | - private $crypto; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * DBConfigService constructor. |
|
| 54 | - * |
|
| 55 | - * @param IDBConnection $connection |
|
| 56 | - * @param ICrypto $crypto |
|
| 57 | - */ |
|
| 58 | - public function __construct(IDBConnection $connection, ICrypto $crypto) { |
|
| 59 | - $this->connection = $connection; |
|
| 60 | - $this->crypto = $crypto; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @param int $mountId |
|
| 65 | - * @return array |
|
| 66 | - */ |
|
| 67 | - public function getMountById($mountId) { |
|
| 68 | - $builder = $this->connection->getQueryBuilder(); |
|
| 69 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 70 | - ->from('external_mounts', 'm') |
|
| 71 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 72 | - $mounts = $this->getMountsFromQuery($query); |
|
| 73 | - if (count($mounts) > 0) { |
|
| 74 | - return $mounts[0]; |
|
| 75 | - } else { |
|
| 76 | - return null; |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get all configured mounts |
|
| 82 | - * |
|
| 83 | - * @return array |
|
| 84 | - */ |
|
| 85 | - public function getAllMounts() { |
|
| 86 | - $builder = $this->connection->getQueryBuilder(); |
|
| 87 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 88 | - ->from('external_mounts'); |
|
| 89 | - return $this->getMountsFromQuery($query); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function getMountsForUser($userId, $groupIds) { |
|
| 93 | - $builder = $this->connection->getQueryBuilder(); |
|
| 94 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 95 | - ->from('external_mounts', 'm') |
|
| 96 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 97 | - ->where($builder->expr()->orX( |
|
| 98 | - $builder->expr()->andX( // global mounts |
|
| 99 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)), |
|
| 100 | - $builder->expr()->isNull('a.value') |
|
| 101 | - ), |
|
| 102 | - $builder->expr()->andX( // mounts for user |
|
| 103 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), |
|
| 104 | - $builder->expr()->eq('a.value', $builder->createNamedParameter($userId)) |
|
| 105 | - ), |
|
| 106 | - $builder->expr()->andX( // mounts for group |
|
| 107 | - $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), |
|
| 108 | - $builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 109 | - ) |
|
| 110 | - )); |
|
| 111 | - |
|
| 112 | - return $this->getMountsFromQuery($query); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Get admin defined mounts |
|
| 117 | - * |
|
| 118 | - * @return array |
|
| 119 | - * @suppress SqlInjectionChecker |
|
| 120 | - */ |
|
| 121 | - public function getAdminMounts() { |
|
| 122 | - $builder = $this->connection->getQueryBuilder(); |
|
| 123 | - $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 124 | - ->from('external_mounts') |
|
| 125 | - ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 126 | - return $this->getMountsFromQuery($query); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - protected function getForQuery(IQueryBuilder $builder, $type, $value) { |
|
| 130 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 131 | - ->from('external_mounts', 'm') |
|
| 132 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 133 | - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 134 | - |
|
| 135 | - if (is_null($value)) { |
|
| 136 | - $query = $query->andWhere($builder->expr()->isNull('a.value')); |
|
| 137 | - } else { |
|
| 138 | - $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - return $query; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Get mounts by applicable |
|
| 146 | - * |
|
| 147 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 148 | - * @param string|null $value user_id, group_id or null for global mounts |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 151 | - public function getMountsFor($type, $value) { |
|
| 152 | - $builder = $this->connection->getQueryBuilder(); |
|
| 153 | - $query = $this->getForQuery($builder, $type, $value); |
|
| 154 | - |
|
| 155 | - return $this->getMountsFromQuery($query); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Get admin defined mounts by applicable |
|
| 160 | - * |
|
| 161 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 162 | - * @param string|null $value user_id, group_id or null for global mounts |
|
| 163 | - * @return array |
|
| 164 | - * @suppress SqlInjectionChecker |
|
| 165 | - */ |
|
| 166 | - public function getAdminMountsFor($type, $value) { |
|
| 167 | - $builder = $this->connection->getQueryBuilder(); |
|
| 168 | - $query = $this->getForQuery($builder, $type, $value); |
|
| 169 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 170 | - |
|
| 171 | - return $this->getMountsFromQuery($query); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Get admin defined mounts for multiple applicable |
|
| 176 | - * |
|
| 177 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 178 | - * @param string[] $values user_ids or group_ids |
|
| 179 | - * @return array |
|
| 180 | - * @suppress SqlInjectionChecker |
|
| 181 | - */ |
|
| 182 | - public function getAdminMountsForMultiple($type, array $values) { |
|
| 183 | - $builder = $this->connection->getQueryBuilder(); |
|
| 184 | - $params = array_map(function ($value) use ($builder) { |
|
| 185 | - return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
|
| 186 | - }, $values); |
|
| 187 | - |
|
| 188 | - $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 189 | - ->from('external_mounts', 'm') |
|
| 190 | - ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 191 | - ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
| 192 | - ->andWhere($builder->expr()->in('a.value', $params)); |
|
| 193 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 194 | - |
|
| 195 | - return $this->getMountsFromQuery($query); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * Get user defined mounts by applicable |
|
| 200 | - * |
|
| 201 | - * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 202 | - * @param string|null $value user_id, group_id or null for global mounts |
|
| 203 | - * @return array |
|
| 204 | - * @suppress SqlInjectionChecker |
|
| 205 | - */ |
|
| 206 | - public function getUserMountsFor($type, $value) { |
|
| 207 | - $builder = $this->connection->getQueryBuilder(); |
|
| 208 | - $query = $this->getForQuery($builder, $type, $value); |
|
| 209 | - $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); |
|
| 210 | - |
|
| 211 | - return $this->getMountsFromQuery($query); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Add a mount to the database |
|
| 216 | - * |
|
| 217 | - * @param string $mountPoint |
|
| 218 | - * @param string $storageBackend |
|
| 219 | - * @param string $authBackend |
|
| 220 | - * @param int $priority |
|
| 221 | - * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL |
|
| 222 | - * @return int the id of the new mount |
|
| 223 | - */ |
|
| 224 | - public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { |
|
| 225 | - if (!$priority) { |
|
| 226 | - $priority = 100; |
|
| 227 | - } |
|
| 228 | - $builder = $this->connection->getQueryBuilder(); |
|
| 229 | - $query = $builder->insert('external_mounts') |
|
| 230 | - ->values([ |
|
| 231 | - 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), |
|
| 232 | - 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), |
|
| 233 | - 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), |
|
| 234 | - 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
|
| 235 | - 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
|
| 236 | - ]); |
|
| 237 | - $query->execute(); |
|
| 238 | - return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Remove a mount from the database |
|
| 243 | - * |
|
| 244 | - * @param int $mountId |
|
| 245 | - */ |
|
| 246 | - public function removeMount($mountId) { |
|
| 247 | - $builder = $this->connection->getQueryBuilder(); |
|
| 248 | - $query = $builder->delete('external_mounts') |
|
| 249 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 250 | - $query->execute(); |
|
| 251 | - |
|
| 252 | - $query = $builder->delete('external_applicable') |
|
| 253 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 254 | - $query->execute(); |
|
| 255 | - |
|
| 256 | - $query = $builder->delete('external_config') |
|
| 257 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 258 | - $query->execute(); |
|
| 259 | - |
|
| 260 | - $query = $builder->delete('external_options') |
|
| 261 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 262 | - $query->execute(); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @param int $mountId |
|
| 267 | - * @param string $newMountPoint |
|
| 268 | - */ |
|
| 269 | - public function setMountPoint($mountId, $newMountPoint) { |
|
| 270 | - $builder = $this->connection->getQueryBuilder(); |
|
| 271 | - |
|
| 272 | - $query = $builder->update('external_mounts') |
|
| 273 | - ->set('mount_point', $builder->createNamedParameter($newMountPoint)) |
|
| 274 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 275 | - |
|
| 276 | - $query->execute(); |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @param int $mountId |
|
| 281 | - * @param string $newAuthBackend |
|
| 282 | - */ |
|
| 283 | - public function setAuthBackend($mountId, $newAuthBackend) { |
|
| 284 | - $builder = $this->connection->getQueryBuilder(); |
|
| 285 | - |
|
| 286 | - $query = $builder->update('external_mounts') |
|
| 287 | - ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) |
|
| 288 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 289 | - |
|
| 290 | - $query->execute(); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @param int $mountId |
|
| 295 | - * @param string $key |
|
| 296 | - * @param string $value |
|
| 297 | - */ |
|
| 298 | - public function setConfig($mountId, $key, $value) { |
|
| 299 | - if ($key === 'password') { |
|
| 300 | - $value = $this->encryptValue($value); |
|
| 301 | - } |
|
| 302 | - $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ |
|
| 303 | - 'mount_id' => $mountId, |
|
| 304 | - 'key' => $key, |
|
| 305 | - 'value' => $value |
|
| 306 | - ], ['mount_id', 'key']); |
|
| 307 | - if ($count === 0) { |
|
| 308 | - $builder = $this->connection->getQueryBuilder(); |
|
| 309 | - $query = $builder->update('external_config') |
|
| 310 | - ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) |
|
| 311 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 312 | - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 313 | - $query->execute(); |
|
| 314 | - } |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * @param int $mountId |
|
| 319 | - * @param string $key |
|
| 320 | - * @param string $value |
|
| 321 | - */ |
|
| 322 | - public function setOption($mountId, $key, $value) { |
|
| 323 | - |
|
| 324 | - $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ |
|
| 325 | - 'mount_id' => $mountId, |
|
| 326 | - 'key' => $key, |
|
| 327 | - 'value' => json_encode($value) |
|
| 328 | - ], ['mount_id', 'key']); |
|
| 329 | - if ($count === 0) { |
|
| 330 | - $builder = $this->connection->getQueryBuilder(); |
|
| 331 | - $query = $builder->update('external_options') |
|
| 332 | - ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) |
|
| 333 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 334 | - ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 335 | - $query->execute(); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - public function addApplicable($mountId, $type, $value) { |
|
| 340 | - $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ |
|
| 341 | - 'mount_id' => $mountId, |
|
| 342 | - 'type' => $type, |
|
| 343 | - 'value' => $value |
|
| 344 | - ], ['mount_id', 'type', 'value']); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - public function removeApplicable($mountId, $type, $value) { |
|
| 348 | - $builder = $this->connection->getQueryBuilder(); |
|
| 349 | - $query = $builder->delete('external_applicable') |
|
| 350 | - ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 351 | - ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 352 | - |
|
| 353 | - if (is_null($value)) { |
|
| 354 | - $query = $query->andWhere($builder->expr()->isNull('value')); |
|
| 355 | - } else { |
|
| 356 | - $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $query->execute(); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - private function getMountsFromQuery(IQueryBuilder $query) { |
|
| 363 | - $result = $query->execute(); |
|
| 364 | - $mounts = $result->fetchAll(); |
|
| 365 | - $uniqueMounts = []; |
|
| 366 | - foreach ($mounts as $mount) { |
|
| 367 | - $id = $mount['mount_id']; |
|
| 368 | - if (!isset($uniqueMounts[$id])) { |
|
| 369 | - $uniqueMounts[$id] = $mount; |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - $uniqueMounts = array_values($uniqueMounts); |
|
| 373 | - |
|
| 374 | - $mountIds = array_map(function ($mount) { |
|
| 375 | - return $mount['mount_id']; |
|
| 376 | - }, $uniqueMounts); |
|
| 377 | - $mountIds = array_values(array_unique($mountIds)); |
|
| 378 | - |
|
| 379 | - $applicable = $this->getApplicableForMounts($mountIds); |
|
| 380 | - $config = $this->getConfigForMounts($mountIds); |
|
| 381 | - $options = $this->getOptionsForMounts($mountIds); |
|
| 382 | - |
|
| 383 | - return array_map(function ($mount, $applicable, $config, $options) { |
|
| 384 | - $mount['type'] = (int)$mount['type']; |
|
| 385 | - $mount['priority'] = (int)$mount['priority']; |
|
| 386 | - $mount['applicable'] = $applicable; |
|
| 387 | - $mount['config'] = $config; |
|
| 388 | - $mount['options'] = $options; |
|
| 389 | - return $mount; |
|
| 390 | - }, $uniqueMounts, $applicable, $config, $options); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * Get mount options from a table grouped by mount id |
|
| 395 | - * |
|
| 396 | - * @param string $table |
|
| 397 | - * @param string[] $fields |
|
| 398 | - * @param int[] $mountIds |
|
| 399 | - * @return array [$mountId => [['field1' => $value1, ...], ...], ...] |
|
| 400 | - */ |
|
| 401 | - private function selectForMounts($table, array $fields, array $mountIds) { |
|
| 402 | - if (count($mountIds) === 0) { |
|
| 403 | - return []; |
|
| 404 | - } |
|
| 405 | - $builder = $this->connection->getQueryBuilder(); |
|
| 406 | - $fields[] = 'mount_id'; |
|
| 407 | - $placeHolders = array_map(function ($id) use ($builder) { |
|
| 408 | - return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
|
| 409 | - }, $mountIds); |
|
| 410 | - $query = $builder->select($fields) |
|
| 411 | - ->from($table) |
|
| 412 | - ->where($builder->expr()->in('mount_id', $placeHolders)); |
|
| 413 | - $rows = $query->execute()->fetchAll(); |
|
| 414 | - |
|
| 415 | - $result = []; |
|
| 416 | - foreach ($mountIds as $mountId) { |
|
| 417 | - $result[$mountId] = []; |
|
| 418 | - } |
|
| 419 | - foreach ($rows as $row) { |
|
| 420 | - if (isset($row['type'])) { |
|
| 421 | - $row['type'] = (int)$row['type']; |
|
| 422 | - } |
|
| 423 | - $result[$row['mount_id']][] = $row; |
|
| 424 | - } |
|
| 425 | - return $result; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * @param int[] $mountIds |
|
| 430 | - * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] |
|
| 431 | - */ |
|
| 432 | - public function getApplicableForMounts($mountIds) { |
|
| 433 | - return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * @param int[] $mountIds |
|
| 438 | - * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 439 | - */ |
|
| 440 | - public function getConfigForMounts($mountIds) { |
|
| 441 | - $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); |
|
| 442 | - return array_map([$this, 'createKeyValueMap'], $mountConfigs); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * @param int[] $mountIds |
|
| 447 | - * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 448 | - */ |
|
| 449 | - public function getOptionsForMounts($mountIds) { |
|
| 450 | - $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
|
| 451 | - $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
|
| 452 | - return array_map(function (array $options) { |
|
| 453 | - return array_map(function ($option) { |
|
| 454 | - return json_decode($option); |
|
| 455 | - }, $options); |
|
| 456 | - }, $optionsMap); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] |
|
| 461 | - * @return array ['key1' => $value1, ...] |
|
| 462 | - */ |
|
| 463 | - private function createKeyValueMap(array $keyValuePairs) { |
|
| 464 | - $decryptedPairts = array_map(function ($pair) { |
|
| 465 | - if ($pair['key'] === 'password') { |
|
| 466 | - $pair['value'] = $this->decryptValue($pair['value']); |
|
| 467 | - } |
|
| 468 | - return $pair; |
|
| 469 | - }, $keyValuePairs); |
|
| 470 | - $keys = array_map(function ($pair) { |
|
| 471 | - return $pair['key']; |
|
| 472 | - }, $decryptedPairts); |
|
| 473 | - $values = array_map(function ($pair) { |
|
| 474 | - return $pair['value']; |
|
| 475 | - }, $decryptedPairts); |
|
| 476 | - |
|
| 477 | - return array_combine($keys, $values); |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - private function encryptValue($value) { |
|
| 481 | - return $this->crypto->encrypt($value); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - private function decryptValue($value) { |
|
| 485 | - try { |
|
| 486 | - return $this->crypto->decrypt($value); |
|
| 487 | - } catch (\Exception $e) { |
|
| 488 | - return $value; |
|
| 489 | - } |
|
| 490 | - } |
|
| 35 | + const MOUNT_TYPE_ADMIN = 1; |
|
| 36 | + const MOUNT_TYPE_PERSONAl = 2; |
|
| 37 | + |
|
| 38 | + const APPLICABLE_TYPE_GLOBAL = 1; |
|
| 39 | + const APPLICABLE_TYPE_GROUP = 2; |
|
| 40 | + const APPLICABLE_TYPE_USER = 3; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var IDBConnection |
|
| 44 | + */ |
|
| 45 | + private $connection; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @var ICrypto |
|
| 49 | + */ |
|
| 50 | + private $crypto; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * DBConfigService constructor. |
|
| 54 | + * |
|
| 55 | + * @param IDBConnection $connection |
|
| 56 | + * @param ICrypto $crypto |
|
| 57 | + */ |
|
| 58 | + public function __construct(IDBConnection $connection, ICrypto $crypto) { |
|
| 59 | + $this->connection = $connection; |
|
| 60 | + $this->crypto = $crypto; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @param int $mountId |
|
| 65 | + * @return array |
|
| 66 | + */ |
|
| 67 | + public function getMountById($mountId) { |
|
| 68 | + $builder = $this->connection->getQueryBuilder(); |
|
| 69 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 70 | + ->from('external_mounts', 'm') |
|
| 71 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 72 | + $mounts = $this->getMountsFromQuery($query); |
|
| 73 | + if (count($mounts) > 0) { |
|
| 74 | + return $mounts[0]; |
|
| 75 | + } else { |
|
| 76 | + return null; |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get all configured mounts |
|
| 82 | + * |
|
| 83 | + * @return array |
|
| 84 | + */ |
|
| 85 | + public function getAllMounts() { |
|
| 86 | + $builder = $this->connection->getQueryBuilder(); |
|
| 87 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 88 | + ->from('external_mounts'); |
|
| 89 | + return $this->getMountsFromQuery($query); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function getMountsForUser($userId, $groupIds) { |
|
| 93 | + $builder = $this->connection->getQueryBuilder(); |
|
| 94 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 95 | + ->from('external_mounts', 'm') |
|
| 96 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 97 | + ->where($builder->expr()->orX( |
|
| 98 | + $builder->expr()->andX( // global mounts |
|
| 99 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GLOBAL, IQueryBuilder::PARAM_INT)), |
|
| 100 | + $builder->expr()->isNull('a.value') |
|
| 101 | + ), |
|
| 102 | + $builder->expr()->andX( // mounts for user |
|
| 103 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_USER, IQueryBuilder::PARAM_INT)), |
|
| 104 | + $builder->expr()->eq('a.value', $builder->createNamedParameter($userId)) |
|
| 105 | + ), |
|
| 106 | + $builder->expr()->andX( // mounts for group |
|
| 107 | + $builder->expr()->eq('a.type', $builder->createNamedParameter(self::APPLICABLE_TYPE_GROUP, IQueryBuilder::PARAM_INT)), |
|
| 108 | + $builder->expr()->in('a.value', $builder->createNamedParameter($groupIds, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 109 | + ) |
|
| 110 | + )); |
|
| 111 | + |
|
| 112 | + return $this->getMountsFromQuery($query); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Get admin defined mounts |
|
| 117 | + * |
|
| 118 | + * @return array |
|
| 119 | + * @suppress SqlInjectionChecker |
|
| 120 | + */ |
|
| 121 | + public function getAdminMounts() { |
|
| 122 | + $builder = $this->connection->getQueryBuilder(); |
|
| 123 | + $query = $builder->select(['mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'type']) |
|
| 124 | + ->from('external_mounts') |
|
| 125 | + ->where($builder->expr()->eq('type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 126 | + return $this->getMountsFromQuery($query); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + protected function getForQuery(IQueryBuilder $builder, $type, $value) { |
|
| 130 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 131 | + ->from('external_mounts', 'm') |
|
| 132 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 133 | + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 134 | + |
|
| 135 | + if (is_null($value)) { |
|
| 136 | + $query = $query->andWhere($builder->expr()->isNull('a.value')); |
|
| 137 | + } else { |
|
| 138 | + $query = $query->andWhere($builder->expr()->eq('a.value', $builder->createNamedParameter($value))); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + return $query; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Get mounts by applicable |
|
| 146 | + * |
|
| 147 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 148 | + * @param string|null $value user_id, group_id or null for global mounts |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | + public function getMountsFor($type, $value) { |
|
| 152 | + $builder = $this->connection->getQueryBuilder(); |
|
| 153 | + $query = $this->getForQuery($builder, $type, $value); |
|
| 154 | + |
|
| 155 | + return $this->getMountsFromQuery($query); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Get admin defined mounts by applicable |
|
| 160 | + * |
|
| 161 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 162 | + * @param string|null $value user_id, group_id or null for global mounts |
|
| 163 | + * @return array |
|
| 164 | + * @suppress SqlInjectionChecker |
|
| 165 | + */ |
|
| 166 | + public function getAdminMountsFor($type, $value) { |
|
| 167 | + $builder = $this->connection->getQueryBuilder(); |
|
| 168 | + $query = $this->getForQuery($builder, $type, $value); |
|
| 169 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 170 | + |
|
| 171 | + return $this->getMountsFromQuery($query); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Get admin defined mounts for multiple applicable |
|
| 176 | + * |
|
| 177 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 178 | + * @param string[] $values user_ids or group_ids |
|
| 179 | + * @return array |
|
| 180 | + * @suppress SqlInjectionChecker |
|
| 181 | + */ |
|
| 182 | + public function getAdminMountsForMultiple($type, array $values) { |
|
| 183 | + $builder = $this->connection->getQueryBuilder(); |
|
| 184 | + $params = array_map(function ($value) use ($builder) { |
|
| 185 | + return $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR); |
|
| 186 | + }, $values); |
|
| 187 | + |
|
| 188 | + $query = $builder->select(['m.mount_id', 'mount_point', 'storage_backend', 'auth_backend', 'priority', 'm.type']) |
|
| 189 | + ->from('external_mounts', 'm') |
|
| 190 | + ->innerJoin('m', 'external_applicable', 'a', $builder->expr()->eq('m.mount_id', 'a.mount_id')) |
|
| 191 | + ->where($builder->expr()->eq('a.type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
| 192 | + ->andWhere($builder->expr()->in('a.value', $params)); |
|
| 193 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_ADMIN, IQueryBuilder::PARAM_INT))); |
|
| 194 | + |
|
| 195 | + return $this->getMountsFromQuery($query); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * Get user defined mounts by applicable |
|
| 200 | + * |
|
| 201 | + * @param int $type any of the self::APPLICABLE_TYPE_ constants |
|
| 202 | + * @param string|null $value user_id, group_id or null for global mounts |
|
| 203 | + * @return array |
|
| 204 | + * @suppress SqlInjectionChecker |
|
| 205 | + */ |
|
| 206 | + public function getUserMountsFor($type, $value) { |
|
| 207 | + $builder = $this->connection->getQueryBuilder(); |
|
| 208 | + $query = $this->getForQuery($builder, $type, $value); |
|
| 209 | + $query->andWhere($builder->expr()->eq('m.type', $builder->expr()->literal(self::MOUNT_TYPE_PERSONAl, IQueryBuilder::PARAM_INT))); |
|
| 210 | + |
|
| 211 | + return $this->getMountsFromQuery($query); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Add a mount to the database |
|
| 216 | + * |
|
| 217 | + * @param string $mountPoint |
|
| 218 | + * @param string $storageBackend |
|
| 219 | + * @param string $authBackend |
|
| 220 | + * @param int $priority |
|
| 221 | + * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAL |
|
| 222 | + * @return int the id of the new mount |
|
| 223 | + */ |
|
| 224 | + public function addMount($mountPoint, $storageBackend, $authBackend, $priority, $type) { |
|
| 225 | + if (!$priority) { |
|
| 226 | + $priority = 100; |
|
| 227 | + } |
|
| 228 | + $builder = $this->connection->getQueryBuilder(); |
|
| 229 | + $query = $builder->insert('external_mounts') |
|
| 230 | + ->values([ |
|
| 231 | + 'mount_point' => $builder->createNamedParameter($mountPoint, IQueryBuilder::PARAM_STR), |
|
| 232 | + 'storage_backend' => $builder->createNamedParameter($storageBackend, IQueryBuilder::PARAM_STR), |
|
| 233 | + 'auth_backend' => $builder->createNamedParameter($authBackend, IQueryBuilder::PARAM_STR), |
|
| 234 | + 'priority' => $builder->createNamedParameter($priority, IQueryBuilder::PARAM_INT), |
|
| 235 | + 'type' => $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT) |
|
| 236 | + ]); |
|
| 237 | + $query->execute(); |
|
| 238 | + return (int)$this->connection->lastInsertId('*PREFIX*external_mounts'); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Remove a mount from the database |
|
| 243 | + * |
|
| 244 | + * @param int $mountId |
|
| 245 | + */ |
|
| 246 | + public function removeMount($mountId) { |
|
| 247 | + $builder = $this->connection->getQueryBuilder(); |
|
| 248 | + $query = $builder->delete('external_mounts') |
|
| 249 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 250 | + $query->execute(); |
|
| 251 | + |
|
| 252 | + $query = $builder->delete('external_applicable') |
|
| 253 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 254 | + $query->execute(); |
|
| 255 | + |
|
| 256 | + $query = $builder->delete('external_config') |
|
| 257 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 258 | + $query->execute(); |
|
| 259 | + |
|
| 260 | + $query = $builder->delete('external_options') |
|
| 261 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 262 | + $query->execute(); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @param int $mountId |
|
| 267 | + * @param string $newMountPoint |
|
| 268 | + */ |
|
| 269 | + public function setMountPoint($mountId, $newMountPoint) { |
|
| 270 | + $builder = $this->connection->getQueryBuilder(); |
|
| 271 | + |
|
| 272 | + $query = $builder->update('external_mounts') |
|
| 273 | + ->set('mount_point', $builder->createNamedParameter($newMountPoint)) |
|
| 274 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 275 | + |
|
| 276 | + $query->execute(); |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @param int $mountId |
|
| 281 | + * @param string $newAuthBackend |
|
| 282 | + */ |
|
| 283 | + public function setAuthBackend($mountId, $newAuthBackend) { |
|
| 284 | + $builder = $this->connection->getQueryBuilder(); |
|
| 285 | + |
|
| 286 | + $query = $builder->update('external_mounts') |
|
| 287 | + ->set('auth_backend', $builder->createNamedParameter($newAuthBackend)) |
|
| 288 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))); |
|
| 289 | + |
|
| 290 | + $query->execute(); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @param int $mountId |
|
| 295 | + * @param string $key |
|
| 296 | + * @param string $value |
|
| 297 | + */ |
|
| 298 | + public function setConfig($mountId, $key, $value) { |
|
| 299 | + if ($key === 'password') { |
|
| 300 | + $value = $this->encryptValue($value); |
|
| 301 | + } |
|
| 302 | + $count = $this->connection->insertIfNotExist('*PREFIX*external_config', [ |
|
| 303 | + 'mount_id' => $mountId, |
|
| 304 | + 'key' => $key, |
|
| 305 | + 'value' => $value |
|
| 306 | + ], ['mount_id', 'key']); |
|
| 307 | + if ($count === 0) { |
|
| 308 | + $builder = $this->connection->getQueryBuilder(); |
|
| 309 | + $query = $builder->update('external_config') |
|
| 310 | + ->set('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR)) |
|
| 311 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 312 | + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 313 | + $query->execute(); |
|
| 314 | + } |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * @param int $mountId |
|
| 319 | + * @param string $key |
|
| 320 | + * @param string $value |
|
| 321 | + */ |
|
| 322 | + public function setOption($mountId, $key, $value) { |
|
| 323 | + |
|
| 324 | + $count = $this->connection->insertIfNotExist('*PREFIX*external_options', [ |
|
| 325 | + 'mount_id' => $mountId, |
|
| 326 | + 'key' => $key, |
|
| 327 | + 'value' => json_encode($value) |
|
| 328 | + ], ['mount_id', 'key']); |
|
| 329 | + if ($count === 0) { |
|
| 330 | + $builder = $this->connection->getQueryBuilder(); |
|
| 331 | + $query = $builder->update('external_options') |
|
| 332 | + ->set('value', $builder->createNamedParameter(json_encode($value), IQueryBuilder::PARAM_STR)) |
|
| 333 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 334 | + ->andWhere($builder->expr()->eq('key', $builder->createNamedParameter($key, IQueryBuilder::PARAM_STR))); |
|
| 335 | + $query->execute(); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + public function addApplicable($mountId, $type, $value) { |
|
| 340 | + $this->connection->insertIfNotExist('*PREFIX*external_applicable', [ |
|
| 341 | + 'mount_id' => $mountId, |
|
| 342 | + 'type' => $type, |
|
| 343 | + 'value' => $value |
|
| 344 | + ], ['mount_id', 'type', 'value']); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + public function removeApplicable($mountId, $type, $value) { |
|
| 348 | + $builder = $this->connection->getQueryBuilder(); |
|
| 349 | + $query = $builder->delete('external_applicable') |
|
| 350 | + ->where($builder->expr()->eq('mount_id', $builder->createNamedParameter($mountId, IQueryBuilder::PARAM_INT))) |
|
| 351 | + ->andWhere($builder->expr()->eq('type', $builder->createNamedParameter($type, IQueryBuilder::PARAM_INT))); |
|
| 352 | + |
|
| 353 | + if (is_null($value)) { |
|
| 354 | + $query = $query->andWhere($builder->expr()->isNull('value')); |
|
| 355 | + } else { |
|
| 356 | + $query = $query->andWhere($builder->expr()->eq('value', $builder->createNamedParameter($value, IQueryBuilder::PARAM_STR))); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $query->execute(); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + private function getMountsFromQuery(IQueryBuilder $query) { |
|
| 363 | + $result = $query->execute(); |
|
| 364 | + $mounts = $result->fetchAll(); |
|
| 365 | + $uniqueMounts = []; |
|
| 366 | + foreach ($mounts as $mount) { |
|
| 367 | + $id = $mount['mount_id']; |
|
| 368 | + if (!isset($uniqueMounts[$id])) { |
|
| 369 | + $uniqueMounts[$id] = $mount; |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + $uniqueMounts = array_values($uniqueMounts); |
|
| 373 | + |
|
| 374 | + $mountIds = array_map(function ($mount) { |
|
| 375 | + return $mount['mount_id']; |
|
| 376 | + }, $uniqueMounts); |
|
| 377 | + $mountIds = array_values(array_unique($mountIds)); |
|
| 378 | + |
|
| 379 | + $applicable = $this->getApplicableForMounts($mountIds); |
|
| 380 | + $config = $this->getConfigForMounts($mountIds); |
|
| 381 | + $options = $this->getOptionsForMounts($mountIds); |
|
| 382 | + |
|
| 383 | + return array_map(function ($mount, $applicable, $config, $options) { |
|
| 384 | + $mount['type'] = (int)$mount['type']; |
|
| 385 | + $mount['priority'] = (int)$mount['priority']; |
|
| 386 | + $mount['applicable'] = $applicable; |
|
| 387 | + $mount['config'] = $config; |
|
| 388 | + $mount['options'] = $options; |
|
| 389 | + return $mount; |
|
| 390 | + }, $uniqueMounts, $applicable, $config, $options); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * Get mount options from a table grouped by mount id |
|
| 395 | + * |
|
| 396 | + * @param string $table |
|
| 397 | + * @param string[] $fields |
|
| 398 | + * @param int[] $mountIds |
|
| 399 | + * @return array [$mountId => [['field1' => $value1, ...], ...], ...] |
|
| 400 | + */ |
|
| 401 | + private function selectForMounts($table, array $fields, array $mountIds) { |
|
| 402 | + if (count($mountIds) === 0) { |
|
| 403 | + return []; |
|
| 404 | + } |
|
| 405 | + $builder = $this->connection->getQueryBuilder(); |
|
| 406 | + $fields[] = 'mount_id'; |
|
| 407 | + $placeHolders = array_map(function ($id) use ($builder) { |
|
| 408 | + return $builder->createPositionalParameter($id, IQueryBuilder::PARAM_INT); |
|
| 409 | + }, $mountIds); |
|
| 410 | + $query = $builder->select($fields) |
|
| 411 | + ->from($table) |
|
| 412 | + ->where($builder->expr()->in('mount_id', $placeHolders)); |
|
| 413 | + $rows = $query->execute()->fetchAll(); |
|
| 414 | + |
|
| 415 | + $result = []; |
|
| 416 | + foreach ($mountIds as $mountId) { |
|
| 417 | + $result[$mountId] = []; |
|
| 418 | + } |
|
| 419 | + foreach ($rows as $row) { |
|
| 420 | + if (isset($row['type'])) { |
|
| 421 | + $row['type'] = (int)$row['type']; |
|
| 422 | + } |
|
| 423 | + $result[$row['mount_id']][] = $row; |
|
| 424 | + } |
|
| 425 | + return $result; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * @param int[] $mountIds |
|
| 430 | + * @return array [$id => [['type' => $type, 'value' => $value], ...], ...] |
|
| 431 | + */ |
|
| 432 | + public function getApplicableForMounts($mountIds) { |
|
| 433 | + return $this->selectForMounts('external_applicable', ['type', 'value'], $mountIds); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * @param int[] $mountIds |
|
| 438 | + * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 439 | + */ |
|
| 440 | + public function getConfigForMounts($mountIds) { |
|
| 441 | + $mountConfigs = $this->selectForMounts('external_config', ['key', 'value'], $mountIds); |
|
| 442 | + return array_map([$this, 'createKeyValueMap'], $mountConfigs); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * @param int[] $mountIds |
|
| 447 | + * @return array [$id => ['key1' => $value1, ...], ...] |
|
| 448 | + */ |
|
| 449 | + public function getOptionsForMounts($mountIds) { |
|
| 450 | + $mountOptions = $this->selectForMounts('external_options', ['key', 'value'], $mountIds); |
|
| 451 | + $optionsMap = array_map([$this, 'createKeyValueMap'], $mountOptions); |
|
| 452 | + return array_map(function (array $options) { |
|
| 453 | + return array_map(function ($option) { |
|
| 454 | + return json_decode($option); |
|
| 455 | + }, $options); |
|
| 456 | + }, $optionsMap); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * @param array $keyValuePairs [['key'=>$key, 'value=>$value], ...] |
|
| 461 | + * @return array ['key1' => $value1, ...] |
|
| 462 | + */ |
|
| 463 | + private function createKeyValueMap(array $keyValuePairs) { |
|
| 464 | + $decryptedPairts = array_map(function ($pair) { |
|
| 465 | + if ($pair['key'] === 'password') { |
|
| 466 | + $pair['value'] = $this->decryptValue($pair['value']); |
|
| 467 | + } |
|
| 468 | + return $pair; |
|
| 469 | + }, $keyValuePairs); |
|
| 470 | + $keys = array_map(function ($pair) { |
|
| 471 | + return $pair['key']; |
|
| 472 | + }, $decryptedPairts); |
|
| 473 | + $values = array_map(function ($pair) { |
|
| 474 | + return $pair['value']; |
|
| 475 | + }, $decryptedPairts); |
|
| 476 | + |
|
| 477 | + return array_combine($keys, $values); |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + private function encryptValue($value) { |
|
| 481 | + return $this->crypto->encrypt($value); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + private function decryptValue($value) { |
|
| 485 | + try { |
|
| 486 | + return $this->crypto->decrypt($value); |
|
| 487 | + } catch (\Exception $e) { |
|
| 488 | + return $value; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | 491 | } |
@@ -30,46 +30,46 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | trait UserTrait { |
| 32 | 32 | |
| 33 | - /** @var IUserSession */ |
|
| 34 | - protected $userSession; |
|
| 33 | + /** @var IUserSession */ |
|
| 34 | + protected $userSession; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * User override |
|
| 38 | - * |
|
| 39 | - * @var IUser|null |
|
| 40 | - */ |
|
| 41 | - private $user = null; |
|
| 36 | + /** |
|
| 37 | + * User override |
|
| 38 | + * |
|
| 39 | + * @var IUser|null |
|
| 40 | + */ |
|
| 41 | + private $user = null; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @return IUser|null |
|
| 45 | - */ |
|
| 46 | - protected function getUser() { |
|
| 47 | - if ($this->user) { |
|
| 48 | - return $this->user; |
|
| 49 | - } |
|
| 50 | - return $this->userSession->getUser(); |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * @return IUser|null |
|
| 45 | + */ |
|
| 46 | + protected function getUser() { |
|
| 47 | + if ($this->user) { |
|
| 48 | + return $this->user; |
|
| 49 | + } |
|
| 50 | + return $this->userSession->getUser(); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Override the user from the session |
|
| 55 | - * Unset with ->resetUser() when finished! |
|
| 56 | - * |
|
| 57 | - * @param IUser |
|
| 58 | - * @return self |
|
| 59 | - */ |
|
| 60 | - public function setUser(IUser $user) { |
|
| 61 | - $this->user = $user; |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 53 | + /** |
|
| 54 | + * Override the user from the session |
|
| 55 | + * Unset with ->resetUser() when finished! |
|
| 56 | + * |
|
| 57 | + * @param IUser |
|
| 58 | + * @return self |
|
| 59 | + */ |
|
| 60 | + public function setUser(IUser $user) { |
|
| 61 | + $this->user = $user; |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Reset the user override |
|
| 67 | - * |
|
| 68 | - * @return self |
|
| 69 | - */ |
|
| 70 | - public function resetUser() { |
|
| 71 | - $this->user = null; |
|
| 72 | - return $this; |
|
| 73 | - } |
|
| 65 | + /** |
|
| 66 | + * Reset the user override |
|
| 67 | + * |
|
| 68 | + * @return self |
|
| 69 | + */ |
|
| 70 | + public function resetUser() { |
|
| 71 | + $this->user = null; |
|
| 72 | + return $this; |
|
| 73 | + } |
|
| 74 | 74 | } |
| 75 | 75 | |
@@ -28,26 +28,26 @@ |
||
| 28 | 28 | |
| 29 | 29 | class DummyUserSession implements IUserSession { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @var IUser |
|
| 33 | - */ |
|
| 34 | - private $user; |
|
| 31 | + /** |
|
| 32 | + * @var IUser |
|
| 33 | + */ |
|
| 34 | + private $user; |
|
| 35 | 35 | |
| 36 | - public function login($user, $password) { |
|
| 37 | - } |
|
| 36 | + public function login($user, $password) { |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - public function logout() { |
|
| 40 | - } |
|
| 39 | + public function logout() { |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function setUser($user) { |
|
| 43 | - $this->user = $user; |
|
| 44 | - } |
|
| 42 | + public function setUser($user) { |
|
| 43 | + $this->user = $user; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function getUser() { |
|
| 47 | - return $this->user; |
|
| 48 | - } |
|
| 46 | + public function getUser() { |
|
| 47 | + return $this->user; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function isLoggedIn() { |
|
| 51 | - return !is_null($this->user); |
|
| 52 | - } |
|
| 50 | + public function isLoggedIn() { |
|
| 51 | + return !is_null($this->user); |
|
| 52 | + } |
|
| 53 | 53 | } |
@@ -30,30 +30,30 @@ |
||
| 30 | 30 | |
| 31 | 31 | class Export extends ListCommand { |
| 32 | 32 | |
| 33 | - protected function configure() { |
|
| 34 | - $this |
|
| 35 | - ->setName('files_external:export') |
|
| 36 | - ->setDescription('Export mount configurations') |
|
| 37 | - ->addArgument( |
|
| 38 | - 'user_id', |
|
| 39 | - InputArgument::OPTIONAL, |
|
| 40 | - 'user id to export the personal mounts for, if no user is provided admin mounts will be exported' |
|
| 41 | - )->addOption( |
|
| 42 | - 'all', |
|
| 43 | - 'a', |
|
| 44 | - InputOption::VALUE_NONE, |
|
| 45 | - 'show both system wide mounts and all personal mounts' |
|
| 46 | - ); |
|
| 47 | - } |
|
| 33 | + protected function configure() { |
|
| 34 | + $this |
|
| 35 | + ->setName('files_external:export') |
|
| 36 | + ->setDescription('Export mount configurations') |
|
| 37 | + ->addArgument( |
|
| 38 | + 'user_id', |
|
| 39 | + InputArgument::OPTIONAL, |
|
| 40 | + 'user id to export the personal mounts for, if no user is provided admin mounts will be exported' |
|
| 41 | + )->addOption( |
|
| 42 | + 'all', |
|
| 43 | + 'a', |
|
| 44 | + InputOption::VALUE_NONE, |
|
| 45 | + 'show both system wide mounts and all personal mounts' |
|
| 46 | + ); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 50 | - $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 51 | - $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 52 | - $listInput->setArgument('user_id', $input->getArgument('user_id')); |
|
| 53 | - $listInput->setOption('all', $input->getOption('all')); |
|
| 54 | - $listInput->setOption('output', 'json_pretty'); |
|
| 55 | - $listInput->setOption('show-password', true); |
|
| 56 | - $listInput->setOption('full', true); |
|
| 57 | - $listCommand->execute($listInput, $output); |
|
| 58 | - } |
|
| 49 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 50 | + $listCommand = new ListCommand($this->globalService, $this->userService, $this->userSession, $this->userManager); |
|
| 51 | + $listInput = new ArrayInput([], $listCommand->getDefinition()); |
|
| 52 | + $listInput->setArgument('user_id', $input->getArgument('user_id')); |
|
| 53 | + $listInput->setOption('all', $input->getOption('all')); |
|
| 54 | + $listInput->setOption('output', 'json_pretty'); |
|
| 55 | + $listInput->setOption('show-password', true); |
|
| 56 | + $listInput->setOption('full', true); |
|
| 57 | + $listCommand->execute($listInput, $output); |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | try { |
| 104 | 104 | $mount = $this->globalService->getStorage($mountId); |
| 105 | 105 | } catch (NotFoundException $e) { |
| 106 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>'); |
|
| 106 | + $output->writeln('<error>Mount with id "'.$mountId.' not found, check "occ files_external:list" to get available mounts</error>'); |
|
| 107 | 107 | return 404; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -123,13 +123,13 @@ discard block |
||
| 123 | 123 | if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { |
| 124 | 124 | foreach ($addUsers as $addUser) { |
| 125 | 125 | if (!$this->userManager->userExists($addUser)) { |
| 126 | - $output->writeln('<error>User "' . $addUser . '" not found</error>'); |
|
| 126 | + $output->writeln('<error>User "'.$addUser.'" not found</error>'); |
|
| 127 | 127 | return 404; |
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | 130 | foreach ($addGroups as $addGroup) { |
| 131 | 131 | if (!$this->groupManager->groupExists($addGroup)) { |
| 132 | - $output->writeln('<error>Group "' . $addGroup . '" not found</error>'); |
|
| 132 | + $output->writeln('<error>Group "'.$addGroup.'" not found</error>'); |
|
| 133 | 133 | return 404; |
| 134 | 134 | } |
| 135 | 135 | } |
@@ -35,122 +35,122 @@ |
||
| 35 | 35 | use Symfony\Component\Console\Output\OutputInterface; |
| 36 | 36 | |
| 37 | 37 | class Applicable extends Base { |
| 38 | - /** |
|
| 39 | - * @var GlobalStoragesService |
|
| 40 | - */ |
|
| 41 | - protected $globalService; |
|
| 38 | + /** |
|
| 39 | + * @var GlobalStoragesService |
|
| 40 | + */ |
|
| 41 | + protected $globalService; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @var IUserManager |
|
| 45 | - */ |
|
| 46 | - private $userManager; |
|
| 43 | + /** |
|
| 44 | + * @var IUserManager |
|
| 45 | + */ |
|
| 46 | + private $userManager; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @var IGroupManager |
|
| 50 | - */ |
|
| 51 | - private $groupManager; |
|
| 48 | + /** |
|
| 49 | + * @var IGroupManager |
|
| 50 | + */ |
|
| 51 | + private $groupManager; |
|
| 52 | 52 | |
| 53 | - function __construct( |
|
| 54 | - GlobalStoragesService $globalService, |
|
| 55 | - IUserManager $userManager, |
|
| 56 | - IGroupManager $groupManager |
|
| 57 | - ) { |
|
| 58 | - parent::__construct(); |
|
| 59 | - $this->globalService = $globalService; |
|
| 60 | - $this->userManager = $userManager; |
|
| 61 | - $this->groupManager = $groupManager; |
|
| 62 | - } |
|
| 53 | + function __construct( |
|
| 54 | + GlobalStoragesService $globalService, |
|
| 55 | + IUserManager $userManager, |
|
| 56 | + IGroupManager $groupManager |
|
| 57 | + ) { |
|
| 58 | + parent::__construct(); |
|
| 59 | + $this->globalService = $globalService; |
|
| 60 | + $this->userManager = $userManager; |
|
| 61 | + $this->groupManager = $groupManager; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - protected function configure() { |
|
| 65 | - $this |
|
| 66 | - ->setName('files_external:applicable') |
|
| 67 | - ->setDescription('Manage applicable users and groups for a mount') |
|
| 68 | - ->addArgument( |
|
| 69 | - 'mount_id', |
|
| 70 | - InputArgument::REQUIRED, |
|
| 71 | - 'The id of the mount to edit' |
|
| 72 | - )->addOption( |
|
| 73 | - 'add-user', |
|
| 74 | - '', |
|
| 75 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 76 | - 'user to add as applicable' |
|
| 77 | - )->addOption( |
|
| 78 | - 'remove-user', |
|
| 79 | - '', |
|
| 80 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 81 | - 'user to remove as applicable' |
|
| 82 | - )->addOption( |
|
| 83 | - 'add-group', |
|
| 84 | - '', |
|
| 85 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 86 | - 'group to add as applicable' |
|
| 87 | - )->addOption( |
|
| 88 | - 'remove-group', |
|
| 89 | - '', |
|
| 90 | - InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 91 | - 'group to remove as applicable' |
|
| 92 | - )->addOption( |
|
| 93 | - 'remove-all', |
|
| 94 | - '', |
|
| 95 | - InputOption::VALUE_NONE, |
|
| 96 | - 'Set the mount to be globally applicable' |
|
| 97 | - ); |
|
| 98 | - parent::configure(); |
|
| 99 | - } |
|
| 64 | + protected function configure() { |
|
| 65 | + $this |
|
| 66 | + ->setName('files_external:applicable') |
|
| 67 | + ->setDescription('Manage applicable users and groups for a mount') |
|
| 68 | + ->addArgument( |
|
| 69 | + 'mount_id', |
|
| 70 | + InputArgument::REQUIRED, |
|
| 71 | + 'The id of the mount to edit' |
|
| 72 | + )->addOption( |
|
| 73 | + 'add-user', |
|
| 74 | + '', |
|
| 75 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 76 | + 'user to add as applicable' |
|
| 77 | + )->addOption( |
|
| 78 | + 'remove-user', |
|
| 79 | + '', |
|
| 80 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 81 | + 'user to remove as applicable' |
|
| 82 | + )->addOption( |
|
| 83 | + 'add-group', |
|
| 84 | + '', |
|
| 85 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 86 | + 'group to add as applicable' |
|
| 87 | + )->addOption( |
|
| 88 | + 'remove-group', |
|
| 89 | + '', |
|
| 90 | + InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, |
|
| 91 | + 'group to remove as applicable' |
|
| 92 | + )->addOption( |
|
| 93 | + 'remove-all', |
|
| 94 | + '', |
|
| 95 | + InputOption::VALUE_NONE, |
|
| 96 | + 'Set the mount to be globally applicable' |
|
| 97 | + ); |
|
| 98 | + parent::configure(); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 102 | - $mountId = $input->getArgument('mount_id'); |
|
| 103 | - try { |
|
| 104 | - $mount = $this->globalService->getStorage($mountId); |
|
| 105 | - } catch (NotFoundException $e) { |
|
| 106 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>'); |
|
| 107 | - return 404; |
|
| 108 | - } |
|
| 101 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 102 | + $mountId = $input->getArgument('mount_id'); |
|
| 103 | + try { |
|
| 104 | + $mount = $this->globalService->getStorage($mountId); |
|
| 105 | + } catch (NotFoundException $e) { |
|
| 106 | + $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts</error>'); |
|
| 107 | + return 404; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
| 111 | - $output->writeln('<error>Can\'t change applicables on personal mounts</error>'); |
|
| 112 | - return 1; |
|
| 113 | - } |
|
| 110 | + if ($mount->getType() === StorageConfig::MOUNT_TYPE_PERSONAl) { |
|
| 111 | + $output->writeln('<error>Can\'t change applicables on personal mounts</error>'); |
|
| 112 | + return 1; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - $addUsers = $input->getOption('add-user'); |
|
| 116 | - $removeUsers = $input->getOption('remove-user'); |
|
| 117 | - $addGroups = $input->getOption('add-group'); |
|
| 118 | - $removeGroups = $input->getOption('remove-group'); |
|
| 115 | + $addUsers = $input->getOption('add-user'); |
|
| 116 | + $removeUsers = $input->getOption('remove-user'); |
|
| 117 | + $addGroups = $input->getOption('add-group'); |
|
| 118 | + $removeGroups = $input->getOption('remove-group'); |
|
| 119 | 119 | |
| 120 | - $applicableUsers = $mount->getApplicableUsers(); |
|
| 121 | - $applicableGroups = $mount->getApplicableGroups(); |
|
| 120 | + $applicableUsers = $mount->getApplicableUsers(); |
|
| 121 | + $applicableGroups = $mount->getApplicableGroups(); |
|
| 122 | 122 | |
| 123 | - if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { |
|
| 124 | - foreach ($addUsers as $addUser) { |
|
| 125 | - if (!$this->userManager->userExists($addUser)) { |
|
| 126 | - $output->writeln('<error>User "' . $addUser . '" not found</error>'); |
|
| 127 | - return 404; |
|
| 128 | - } |
|
| 129 | - } |
|
| 130 | - foreach ($addGroups as $addGroup) { |
|
| 131 | - if (!$this->groupManager->groupExists($addGroup)) { |
|
| 132 | - $output->writeln('<error>Group "' . $addGroup . '" not found</error>'); |
|
| 133 | - return 404; |
|
| 134 | - } |
|
| 135 | - } |
|
| 123 | + if ((count($addUsers) + count($removeUsers) + count($addGroups) + count($removeGroups) > 0) || $input->getOption('remove-all')) { |
|
| 124 | + foreach ($addUsers as $addUser) { |
|
| 125 | + if (!$this->userManager->userExists($addUser)) { |
|
| 126 | + $output->writeln('<error>User "' . $addUser . '" not found</error>'); |
|
| 127 | + return 404; |
|
| 128 | + } |
|
| 129 | + } |
|
| 130 | + foreach ($addGroups as $addGroup) { |
|
| 131 | + if (!$this->groupManager->groupExists($addGroup)) { |
|
| 132 | + $output->writeln('<error>Group "' . $addGroup . '" not found</error>'); |
|
| 133 | + return 404; |
|
| 134 | + } |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - if ($input->getOption('remove-all')) { |
|
| 138 | - $applicableUsers = []; |
|
| 139 | - $applicableGroups = []; |
|
| 140 | - } else { |
|
| 141 | - $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); |
|
| 142 | - $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); |
|
| 143 | - $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); |
|
| 144 | - $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); |
|
| 145 | - } |
|
| 146 | - $mount->setApplicableUsers($applicableUsers); |
|
| 147 | - $mount->setApplicableGroups($applicableGroups); |
|
| 148 | - $this->globalService->updateStorage($mount); |
|
| 149 | - } |
|
| 137 | + if ($input->getOption('remove-all')) { |
|
| 138 | + $applicableUsers = []; |
|
| 139 | + $applicableGroups = []; |
|
| 140 | + } else { |
|
| 141 | + $applicableUsers = array_unique(array_merge($applicableUsers, $addUsers)); |
|
| 142 | + $applicableUsers = array_values(array_diff($applicableUsers, $removeUsers)); |
|
| 143 | + $applicableGroups = array_unique(array_merge($applicableGroups, $addGroups)); |
|
| 144 | + $applicableGroups = array_values(array_diff($applicableGroups, $removeGroups)); |
|
| 145 | + } |
|
| 146 | + $mount->setApplicableUsers($applicableUsers); |
|
| 147 | + $mount->setApplicableGroups($applicableGroups); |
|
| 148 | + $this->globalService->updateStorage($mount); |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - $this->writeArrayInOutputFormat($input, $output, [ |
|
| 152 | - 'users' => $applicableUsers, |
|
| 153 | - 'groups' => $applicableGroups |
|
| 154 | - ]); |
|
| 155 | - } |
|
| 151 | + $this->writeArrayInOutputFormat($input, $output, [ |
|
| 152 | + 'users' => $applicableUsers, |
|
| 153 | + 'groups' => $applicableGroups |
|
| 154 | + ]); |
|
| 155 | + } |
|
| 156 | 156 | } |
@@ -68,7 +68,7 @@ |
||
| 68 | 68 | try { |
| 69 | 69 | $mount = $this->globalService->getStorage($mountId); |
| 70 | 70 | } catch (NotFoundException $e) { |
| 71 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 71 | + $output->writeln('<error>Mount with id "'.$mountId.' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 72 | 72 | return 404; |
| 73 | 73 | } |
| 74 | 74 | |
@@ -32,87 +32,87 @@ |
||
| 32 | 32 | use Symfony\Component\Console\Output\OutputInterface; |
| 33 | 33 | |
| 34 | 34 | class Config extends Base { |
| 35 | - /** |
|
| 36 | - * @var GlobalStoragesService |
|
| 37 | - */ |
|
| 38 | - protected $globalService; |
|
| 35 | + /** |
|
| 36 | + * @var GlobalStoragesService |
|
| 37 | + */ |
|
| 38 | + protected $globalService; |
|
| 39 | 39 | |
| 40 | - function __construct(GlobalStoragesService $globalService) { |
|
| 41 | - parent::__construct(); |
|
| 42 | - $this->globalService = $globalService; |
|
| 43 | - } |
|
| 40 | + function __construct(GlobalStoragesService $globalService) { |
|
| 41 | + parent::__construct(); |
|
| 42 | + $this->globalService = $globalService; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - protected function configure() { |
|
| 46 | - $this |
|
| 47 | - ->setName('files_external:config') |
|
| 48 | - ->setDescription('Manage backend configuration for a mount') |
|
| 49 | - ->addArgument( |
|
| 50 | - 'mount_id', |
|
| 51 | - InputArgument::REQUIRED, |
|
| 52 | - 'The id of the mount to edit' |
|
| 53 | - )->addArgument( |
|
| 54 | - 'key', |
|
| 55 | - InputArgument::REQUIRED, |
|
| 56 | - 'key of the config option to set/get' |
|
| 57 | - )->addArgument( |
|
| 58 | - 'value', |
|
| 59 | - InputArgument::OPTIONAL, |
|
| 60 | - 'value to set the config option to, when no value is provided the existing value will be printed' |
|
| 61 | - ); |
|
| 62 | - parent::configure(); |
|
| 63 | - } |
|
| 45 | + protected function configure() { |
|
| 46 | + $this |
|
| 47 | + ->setName('files_external:config') |
|
| 48 | + ->setDescription('Manage backend configuration for a mount') |
|
| 49 | + ->addArgument( |
|
| 50 | + 'mount_id', |
|
| 51 | + InputArgument::REQUIRED, |
|
| 52 | + 'The id of the mount to edit' |
|
| 53 | + )->addArgument( |
|
| 54 | + 'key', |
|
| 55 | + InputArgument::REQUIRED, |
|
| 56 | + 'key of the config option to set/get' |
|
| 57 | + )->addArgument( |
|
| 58 | + 'value', |
|
| 59 | + InputArgument::OPTIONAL, |
|
| 60 | + 'value to set the config option to, when no value is provided the existing value will be printed' |
|
| 61 | + ); |
|
| 62 | + parent::configure(); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 66 | - $mountId = $input->getArgument('mount_id'); |
|
| 67 | - $key = $input->getArgument('key'); |
|
| 68 | - try { |
|
| 69 | - $mount = $this->globalService->getStorage($mountId); |
|
| 70 | - } catch (NotFoundException $e) { |
|
| 71 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 72 | - return 404; |
|
| 73 | - } |
|
| 65 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 66 | + $mountId = $input->getArgument('mount_id'); |
|
| 67 | + $key = $input->getArgument('key'); |
|
| 68 | + try { |
|
| 69 | + $mount = $this->globalService->getStorage($mountId); |
|
| 70 | + } catch (NotFoundException $e) { |
|
| 71 | + $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 72 | + return 404; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - $value = $input->getArgument('value'); |
|
| 76 | - if ($value) { |
|
| 77 | - $this->setOption($mount, $key, $value, $output); |
|
| 78 | - } else { |
|
| 79 | - $this->getOption($mount, $key, $output); |
|
| 80 | - } |
|
| 81 | - } |
|
| 75 | + $value = $input->getArgument('value'); |
|
| 76 | + if ($value) { |
|
| 77 | + $this->setOption($mount, $key, $value, $output); |
|
| 78 | + } else { |
|
| 79 | + $this->getOption($mount, $key, $output); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * @param StorageConfig $mount |
|
| 85 | - * @param string $key |
|
| 86 | - * @param OutputInterface $output |
|
| 87 | - */ |
|
| 88 | - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
| 89 | - if ($key === 'mountpoint' || $key === 'mount_point') { |
|
| 90 | - $value = $mount->getMountPoint(); |
|
| 91 | - } else { |
|
| 92 | - $value = $mount->getBackendOption($key); |
|
| 93 | - } |
|
| 94 | - if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly |
|
| 95 | - $value = json_encode($value); |
|
| 96 | - } |
|
| 97 | - $output->writeln($value); |
|
| 98 | - } |
|
| 83 | + /** |
|
| 84 | + * @param StorageConfig $mount |
|
| 85 | + * @param string $key |
|
| 86 | + * @param OutputInterface $output |
|
| 87 | + */ |
|
| 88 | + protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
| 89 | + if ($key === 'mountpoint' || $key === 'mount_point') { |
|
| 90 | + $value = $mount->getMountPoint(); |
|
| 91 | + } else { |
|
| 92 | + $value = $mount->getBackendOption($key); |
|
| 93 | + } |
|
| 94 | + if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly |
|
| 95 | + $value = json_encode($value); |
|
| 96 | + } |
|
| 97 | + $output->writeln($value); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @param StorageConfig $mount |
|
| 102 | - * @param string $key |
|
| 103 | - * @param string $value |
|
| 104 | - * @param OutputInterface $output |
|
| 105 | - */ |
|
| 106 | - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
| 107 | - $decoded = json_decode($value, true); |
|
| 108 | - if (!is_null($decoded) && json_encode($decoded) === $value) { |
|
| 109 | - $value = $decoded; |
|
| 110 | - } |
|
| 111 | - if ($key === 'mountpoint' || $key === 'mount_point') { |
|
| 112 | - $mount->setMountPoint($value); |
|
| 113 | - } else { |
|
| 114 | - $mount->setBackendOption($key, $value); |
|
| 115 | - } |
|
| 116 | - $this->globalService->updateStorage($mount); |
|
| 117 | - } |
|
| 100 | + /** |
|
| 101 | + * @param StorageConfig $mount |
|
| 102 | + * @param string $key |
|
| 103 | + * @param string $value |
|
| 104 | + * @param OutputInterface $output |
|
| 105 | + */ |
|
| 106 | + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
| 107 | + $decoded = json_decode($value, true); |
|
| 108 | + if (!is_null($decoded) && json_encode($decoded) === $value) { |
|
| 109 | + $value = $decoded; |
|
| 110 | + } |
|
| 111 | + if ($key === 'mountpoint' || $key === 'mount_point') { |
|
| 112 | + $mount->setMountPoint($value); |
|
| 113 | + } else { |
|
| 114 | + $mount->setBackendOption($key, $value); |
|
| 115 | + } |
|
| 116 | + $this->globalService->updateStorage($mount); |
|
| 117 | + } |
|
| 118 | 118 | } |
@@ -37,108 +37,108 @@ |
||
| 37 | 37 | use Symfony\Component\Console\Output\OutputInterface; |
| 38 | 38 | |
| 39 | 39 | class Verify extends Base { |
| 40 | - /** |
|
| 41 | - * @var GlobalStoragesService |
|
| 42 | - */ |
|
| 43 | - protected $globalService; |
|
| 40 | + /** |
|
| 41 | + * @var GlobalStoragesService |
|
| 42 | + */ |
|
| 43 | + protected $globalService; |
|
| 44 | 44 | |
| 45 | - function __construct(GlobalStoragesService $globalService) { |
|
| 46 | - parent::__construct(); |
|
| 47 | - $this->globalService = $globalService; |
|
| 48 | - } |
|
| 45 | + function __construct(GlobalStoragesService $globalService) { |
|
| 46 | + parent::__construct(); |
|
| 47 | + $this->globalService = $globalService; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - protected function configure() { |
|
| 51 | - $this |
|
| 52 | - ->setName('files_external:verify') |
|
| 53 | - ->setDescription('Verify mount configuration') |
|
| 54 | - ->addArgument( |
|
| 55 | - 'mount_id', |
|
| 56 | - InputArgument::REQUIRED, |
|
| 57 | - 'The id of the mount to check' |
|
| 58 | - )->addOption( |
|
| 59 | - 'config', |
|
| 60 | - 'c', |
|
| 61 | - InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
| 62 | - 'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails' |
|
| 63 | - ); |
|
| 64 | - parent::configure(); |
|
| 65 | - } |
|
| 50 | + protected function configure() { |
|
| 51 | + $this |
|
| 52 | + ->setName('files_external:verify') |
|
| 53 | + ->setDescription('Verify mount configuration') |
|
| 54 | + ->addArgument( |
|
| 55 | + 'mount_id', |
|
| 56 | + InputArgument::REQUIRED, |
|
| 57 | + 'The id of the mount to check' |
|
| 58 | + )->addOption( |
|
| 59 | + 'config', |
|
| 60 | + 'c', |
|
| 61 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
|
| 62 | + 'Additional config option to set before checking in key=value pairs, required for certain auth backends such as login credentails' |
|
| 63 | + ); |
|
| 64 | + parent::configure(); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 68 | - $mountId = $input->getArgument('mount_id'); |
|
| 69 | - $configInput = $input->getOption('config'); |
|
| 67 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 68 | + $mountId = $input->getArgument('mount_id'); |
|
| 69 | + $configInput = $input->getOption('config'); |
|
| 70 | 70 | |
| 71 | - try { |
|
| 72 | - $mount = $this->globalService->getStorage($mountId); |
|
| 73 | - } catch (NotFoundException $e) { |
|
| 74 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 75 | - return 404; |
|
| 76 | - } |
|
| 71 | + try { |
|
| 72 | + $mount = $this->globalService->getStorage($mountId); |
|
| 73 | + } catch (NotFoundException $e) { |
|
| 74 | + $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 75 | + return 404; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $this->updateStorageStatus($mount, $configInput, $output); |
|
| 78 | + $this->updateStorageStatus($mount, $configInput, $output); |
|
| 79 | 79 | |
| 80 | - $this->writeArrayInOutputFormat($input, $output, [ |
|
| 81 | - 'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()), |
|
| 82 | - 'code' => $mount->getStatus(), |
|
| 83 | - 'message' => $mount->getStatusMessage() |
|
| 84 | - ]); |
|
| 85 | - } |
|
| 80 | + $this->writeArrayInOutputFormat($input, $output, [ |
|
| 81 | + 'status' => StorageNotAvailableException::getStateCodeName($mount->getStatus()), |
|
| 82 | + 'code' => $mount->getStatus(), |
|
| 83 | + 'message' => $mount->getStatusMessage() |
|
| 84 | + ]); |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - private function manipulateStorageConfig(StorageConfig $storage) { |
|
| 88 | - /** @var AuthMechanism */ |
|
| 89 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 90 | - $authMechanism->manipulateStorageConfig($storage); |
|
| 91 | - /** @var Backend */ |
|
| 92 | - $backend = $storage->getBackend(); |
|
| 93 | - $backend->manipulateStorageConfig($storage); |
|
| 94 | - } |
|
| 87 | + private function manipulateStorageConfig(StorageConfig $storage) { |
|
| 88 | + /** @var AuthMechanism */ |
|
| 89 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 90 | + $authMechanism->manipulateStorageConfig($storage); |
|
| 91 | + /** @var Backend */ |
|
| 92 | + $backend = $storage->getBackend(); |
|
| 93 | + $backend->manipulateStorageConfig($storage); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output) { |
|
| 97 | - try { |
|
| 98 | - try { |
|
| 99 | - $this->manipulateStorageConfig($storage); |
|
| 100 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 101 | - if (count($configInput) === 0) { // extra config options might solve the error |
|
| 102 | - throw $e; |
|
| 103 | - } |
|
| 104 | - } |
|
| 96 | + private function updateStorageStatus(StorageConfig &$storage, $configInput, OutputInterface $output) { |
|
| 97 | + try { |
|
| 98 | + try { |
|
| 99 | + $this->manipulateStorageConfig($storage); |
|
| 100 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 101 | + if (count($configInput) === 0) { // extra config options might solve the error |
|
| 102 | + throw $e; |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - foreach ($configInput as $configOption) { |
|
| 107 | - if (!strpos($configOption, '=')) { |
|
| 108 | - $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
| 109 | - return; |
|
| 110 | - } |
|
| 111 | - list($key, $value) = explode('=', $configOption, 2); |
|
| 112 | - $storage->setBackendOption($key, $value); |
|
| 113 | - } |
|
| 106 | + foreach ($configInput as $configOption) { |
|
| 107 | + if (!strpos($configOption, '=')) { |
|
| 108 | + $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
| 109 | + return; |
|
| 110 | + } |
|
| 111 | + list($key, $value) = explode('=', $configOption, 2); |
|
| 112 | + $storage->setBackendOption($key, $value); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** @var Backend */ |
|
| 116 | - $backend = $storage->getBackend(); |
|
| 117 | - // update status (can be time-consuming) |
|
| 118 | - $storage->setStatus( |
|
| 119 | - \OC_Mount_Config::getBackendStatus( |
|
| 120 | - $backend->getStorageClass(), |
|
| 121 | - $storage->getBackendOptions(), |
|
| 122 | - false |
|
| 123 | - ) |
|
| 124 | - ); |
|
| 125 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 126 | - $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; |
|
| 127 | - $storage->setStatus( |
|
| 128 | - $status, |
|
| 129 | - $e->getMessage() |
|
| 130 | - ); |
|
| 131 | - } catch (StorageNotAvailableException $e) { |
|
| 132 | - $storage->setStatus( |
|
| 133 | - $e->getCode(), |
|
| 134 | - $e->getMessage() |
|
| 135 | - ); |
|
| 136 | - } catch (\Exception $e) { |
|
| 137 | - // FIXME: convert storage exceptions to StorageNotAvailableException |
|
| 138 | - $storage->setStatus( |
|
| 139 | - StorageNotAvailableException::STATUS_ERROR, |
|
| 140 | - get_class($e) . ': ' . $e->getMessage() |
|
| 141 | - ); |
|
| 142 | - } |
|
| 143 | - } |
|
| 115 | + /** @var Backend */ |
|
| 116 | + $backend = $storage->getBackend(); |
|
| 117 | + // update status (can be time-consuming) |
|
| 118 | + $storage->setStatus( |
|
| 119 | + \OC_Mount_Config::getBackendStatus( |
|
| 120 | + $backend->getStorageClass(), |
|
| 121 | + $storage->getBackendOptions(), |
|
| 122 | + false |
|
| 123 | + ) |
|
| 124 | + ); |
|
| 125 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 126 | + $status = $e->getCode() ? $e->getCode() : StorageNotAvailableException::STATUS_INDETERMINATE; |
|
| 127 | + $storage->setStatus( |
|
| 128 | + $status, |
|
| 129 | + $e->getMessage() |
|
| 130 | + ); |
|
| 131 | + } catch (StorageNotAvailableException $e) { |
|
| 132 | + $storage->setStatus( |
|
| 133 | + $e->getCode(), |
|
| 134 | + $e->getMessage() |
|
| 135 | + ); |
|
| 136 | + } catch (\Exception $e) { |
|
| 137 | + // FIXME: convert storage exceptions to StorageNotAvailableException |
|
| 138 | + $storage->setStatus( |
|
| 139 | + StorageNotAvailableException::STATUS_ERROR, |
|
| 140 | + get_class($e) . ': ' . $e->getMessage() |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | try { |
| 72 | 72 | $mount = $this->globalService->getStorage($mountId); |
| 73 | 73 | } catch (NotFoundException $e) { |
| 74 | - $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 74 | + $output->writeln('<error>Mount with id "'.$mountId.' not found, check "occ files_external:list" to get available mounts"</error>'); |
|
| 75 | 75 | return 404; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -105,7 +105,7 @@ discard block |
||
| 105 | 105 | |
| 106 | 106 | foreach ($configInput as $configOption) { |
| 107 | 107 | if (!strpos($configOption, '=')) { |
| 108 | - $output->writeln('<error>Invalid mount configuration option "' . $configOption . '"</error>'); |
|
| 108 | + $output->writeln('<error>Invalid mount configuration option "'.$configOption.'"</error>'); |
|
| 109 | 109 | return; |
| 110 | 110 | } |
| 111 | 111 | list($key, $value) = explode('=', $configOption, 2); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | // FIXME: convert storage exceptions to StorageNotAvailableException |
| 138 | 138 | $storage->setStatus( |
| 139 | 139 | StorageNotAvailableException::STATUS_ERROR, |
| 140 | - get_class($e) . ': ' . $e->getMessage() |
|
| 140 | + get_class($e).': '.$e->getMessage() |
|
| 141 | 141 | ); |
| 142 | 142 | } |
| 143 | 143 | } |
@@ -28,50 +28,50 @@ |
||
| 28 | 28 | use Symfony\Component\Console\Output\OutputInterface; |
| 29 | 29 | |
| 30 | 30 | class Option extends Config { |
| 31 | - protected function configure() { |
|
| 32 | - $this |
|
| 33 | - ->setName('files_external:option') |
|
| 34 | - ->setDescription('Manage mount options for a mount') |
|
| 35 | - ->addArgument( |
|
| 36 | - 'mount_id', |
|
| 37 | - InputArgument::REQUIRED, |
|
| 38 | - 'The id of the mount to edit' |
|
| 39 | - )->addArgument( |
|
| 40 | - 'key', |
|
| 41 | - InputArgument::REQUIRED, |
|
| 42 | - 'key of the mount option to set/get' |
|
| 43 | - )->addArgument( |
|
| 44 | - 'value', |
|
| 45 | - InputArgument::OPTIONAL, |
|
| 46 | - 'value to set the mount option to, when no value is provided the existing value will be printed' |
|
| 47 | - ); |
|
| 48 | - } |
|
| 31 | + protected function configure() { |
|
| 32 | + $this |
|
| 33 | + ->setName('files_external:option') |
|
| 34 | + ->setDescription('Manage mount options for a mount') |
|
| 35 | + ->addArgument( |
|
| 36 | + 'mount_id', |
|
| 37 | + InputArgument::REQUIRED, |
|
| 38 | + 'The id of the mount to edit' |
|
| 39 | + )->addArgument( |
|
| 40 | + 'key', |
|
| 41 | + InputArgument::REQUIRED, |
|
| 42 | + 'key of the mount option to set/get' |
|
| 43 | + )->addArgument( |
|
| 44 | + 'value', |
|
| 45 | + InputArgument::OPTIONAL, |
|
| 46 | + 'value to set the mount option to, when no value is provided the existing value will be printed' |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @param StorageConfig $mount |
|
| 52 | - * @param string $key |
|
| 53 | - * @param OutputInterface $output |
|
| 54 | - */ |
|
| 55 | - protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
| 56 | - $value = $mount->getMountOption($key); |
|
| 57 | - if (!is_string($value)) { // show bools and objects correctly |
|
| 58 | - $value = json_encode($value); |
|
| 59 | - } |
|
| 60 | - $output->writeln($value); |
|
| 61 | - } |
|
| 50 | + /** |
|
| 51 | + * @param StorageConfig $mount |
|
| 52 | + * @param string $key |
|
| 53 | + * @param OutputInterface $output |
|
| 54 | + */ |
|
| 55 | + protected function getOption(StorageConfig $mount, $key, OutputInterface $output) { |
|
| 56 | + $value = $mount->getMountOption($key); |
|
| 57 | + if (!is_string($value)) { // show bools and objects correctly |
|
| 58 | + $value = json_encode($value); |
|
| 59 | + } |
|
| 60 | + $output->writeln($value); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @param StorageConfig $mount |
|
| 65 | - * @param string $key |
|
| 66 | - * @param string $value |
|
| 67 | - * @param OutputInterface $output |
|
| 68 | - */ |
|
| 69 | - protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
| 70 | - $decoded = json_decode($value, true); |
|
| 71 | - if (!is_null($decoded)) { |
|
| 72 | - $value = $decoded; |
|
| 73 | - } |
|
| 74 | - $mount->setMountOption($key, $value); |
|
| 75 | - $this->globalService->updateStorage($mount); |
|
| 76 | - } |
|
| 63 | + /** |
|
| 64 | + * @param StorageConfig $mount |
|
| 65 | + * @param string $key |
|
| 66 | + * @param string $value |
|
| 67 | + * @param OutputInterface $output |
|
| 68 | + */ |
|
| 69 | + protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) { |
|
| 70 | + $decoded = json_decode($value, true); |
|
| 71 | + if (!is_null($decoded)) { |
|
| 72 | + $value = $decoded; |
|
| 73 | + } |
|
| 74 | + $mount->setMountOption($key, $value); |
|
| 75 | + $this->globalService->updateStorage($mount); |
|
| 76 | + } |
|
| 77 | 77 | } |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | return 1; |
| 121 | 121 | } |
| 122 | 122 | if (!$storage instanceof INotifyStorage) { |
| 123 | - $output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>'); |
|
| 123 | + $output->writeln('<error>Mount of type "'.$mount->getBackend()->getText().'" does not support active update notifications</error>'); |
|
| 124 | 124 | return 1; |
| 125 | 125 | } |
| 126 | 126 | |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | $path = trim($input->getOption('path'), '/'); |
| 130 | 130 | $notifyHandler = $storage->notify($path); |
| 131 | 131 | $this->selfTest($storage, $notifyHandler, $verbose, $output); |
| 132 | - $notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output) { |
|
| 132 | + $notifyHandler->listen(function(IChange $change) use ($mount, $verbose, $output) { |
|
| 133 | 133 | if ($verbose) { |
| 134 | 134 | $this->logUpdate($change, $output); |
| 135 | 135 | } |
@@ -171,9 +171,9 @@ discard block |
||
| 171 | 171 | return; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - $text .= ' ' . $change->getPath(); |
|
| 174 | + $text .= ' '.$change->getPath(); |
|
| 175 | 175 | if ($change instanceof IRenameChange) { |
| 176 | - $text .= ' to ' . $change->getTargetPath(); |
|
| 176 | + $text .= ' to '.$change->getTargetPath(); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $output->writeln($text); |
@@ -41,186 +41,186 @@ |
||
| 41 | 41 | use Symfony\Component\Console\Output\OutputInterface; |
| 42 | 42 | |
| 43 | 43 | class Notify extends Base { |
| 44 | - /** @var GlobalStoragesService */ |
|
| 45 | - private $globalService; |
|
| 46 | - /** @var IDBConnection */ |
|
| 47 | - private $connection; |
|
| 48 | - /** @var \OCP\DB\QueryBuilder\IQueryBuilder */ |
|
| 49 | - private $updateQuery; |
|
| 50 | - |
|
| 51 | - function __construct(GlobalStoragesService $globalService, IDBConnection $connection) { |
|
| 52 | - parent::__construct(); |
|
| 53 | - $this->globalService = $globalService; |
|
| 54 | - $this->connection = $connection; |
|
| 55 | - // the query builder doesn't really like subqueries with parameters |
|
| 56 | - $this->updateQuery = $this->connection->prepare( |
|
| 57 | - 'UPDATE *PREFIX*filecache SET size = -1 |
|
| 44 | + /** @var GlobalStoragesService */ |
|
| 45 | + private $globalService; |
|
| 46 | + /** @var IDBConnection */ |
|
| 47 | + private $connection; |
|
| 48 | + /** @var \OCP\DB\QueryBuilder\IQueryBuilder */ |
|
| 49 | + private $updateQuery; |
|
| 50 | + |
|
| 51 | + function __construct(GlobalStoragesService $globalService, IDBConnection $connection) { |
|
| 52 | + parent::__construct(); |
|
| 53 | + $this->globalService = $globalService; |
|
| 54 | + $this->connection = $connection; |
|
| 55 | + // the query builder doesn't really like subqueries with parameters |
|
| 56 | + $this->updateQuery = $this->connection->prepare( |
|
| 57 | + 'UPDATE *PREFIX*filecache SET size = -1 |
|
| 58 | 58 | WHERE `path` = ? |
| 59 | 59 | AND `storage` IN (SELECT storage_id FROM *PREFIX*mounts WHERE mount_id = ?)' |
| 60 | - ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - protected function configure() { |
|
| 64 | - $this |
|
| 65 | - ->setName('files_external:notify') |
|
| 66 | - ->setDescription('Listen for active update notifications for a configured external mount') |
|
| 67 | - ->addArgument( |
|
| 68 | - 'mount_id', |
|
| 69 | - InputArgument::REQUIRED, |
|
| 70 | - 'the mount id of the mount to listen to' |
|
| 71 | - )->addOption( |
|
| 72 | - 'user', |
|
| 73 | - 'u', |
|
| 74 | - InputOption::VALUE_REQUIRED, |
|
| 75 | - 'The username for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
| 76 | - )->addOption( |
|
| 77 | - 'password', |
|
| 78 | - 'p', |
|
| 79 | - InputOption::VALUE_REQUIRED, |
|
| 80 | - 'The password for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
| 81 | - )->addOption( |
|
| 82 | - 'path', |
|
| 83 | - '', |
|
| 84 | - InputOption::VALUE_REQUIRED, |
|
| 85 | - 'The directory in the storage to listen for updates in', |
|
| 86 | - '/' |
|
| 87 | - ); |
|
| 88 | - parent::configure(); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 92 | - $mount = $this->globalService->getStorage($input->getArgument('mount_id')); |
|
| 93 | - if (is_null($mount)) { |
|
| 94 | - $output->writeln('<error>Mount not found</error>'); |
|
| 95 | - return 1; |
|
| 96 | - } |
|
| 97 | - $noAuth = false; |
|
| 98 | - try { |
|
| 99 | - $authBackend = $mount->getAuthMechanism(); |
|
| 100 | - $authBackend->manipulateStorageConfig($mount); |
|
| 101 | - } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 102 | - $noAuth = true; |
|
| 103 | - } catch (StorageNotAvailableException $e) { |
|
| 104 | - $noAuth = true; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - if ($input->getOption('user')) { |
|
| 108 | - $mount->setBackendOption('user', $input->getOption('user')); |
|
| 109 | - } else if (isset($_ENV['NOTIFY_USER'])) { |
|
| 110 | - $mount->setBackendOption('user', $_ENV['NOTIFY_USER']); |
|
| 111 | - } else if (isset($_SERVER['NOTIFY_USER'])) { |
|
| 112 | - $mount->setBackendOption('user', $_SERVER['NOTIFY_USER']); |
|
| 113 | - } |
|
| 114 | - if ($input->getOption('password')) { |
|
| 115 | - $mount->setBackendOption('password', $input->getOption('password')); |
|
| 116 | - } else if (isset($_ENV['NOTIFY_PASSWORD'])) { |
|
| 117 | - $mount->setBackendOption('password', $_ENV['NOTIFY_PASSWORD']); |
|
| 118 | - } else if (isset($_SERVER['NOTIFY_PASSWORD'])) { |
|
| 119 | - $mount->setBackendOption('password', $_SERVER['NOTIFY_PASSWORD']); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - try { |
|
| 123 | - $storage = $this->createStorage($mount); |
|
| 124 | - } catch (\Exception $e) { |
|
| 125 | - $output->writeln('<error>Error while trying to create storage</error>'); |
|
| 126 | - if ($noAuth) { |
|
| 127 | - $output->writeln('<error>Username and/or password required</error>'); |
|
| 128 | - } |
|
| 129 | - return 1; |
|
| 130 | - } |
|
| 131 | - if (!$storage instanceof INotifyStorage) { |
|
| 132 | - $output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>'); |
|
| 133 | - return 1; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - $verbose = $input->getOption('verbose'); |
|
| 137 | - |
|
| 138 | - $path = trim($input->getOption('path'), '/'); |
|
| 139 | - $notifyHandler = $storage->notify($path); |
|
| 140 | - $this->selfTest($storage, $notifyHandler, $verbose, $output); |
|
| 141 | - $notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output) { |
|
| 142 | - if ($verbose) { |
|
| 143 | - $this->logUpdate($change, $output); |
|
| 144 | - } |
|
| 145 | - if ($change instanceof IRenameChange) { |
|
| 146 | - $this->markParentAsOutdated($mount->getId(), $change->getTargetPath()); |
|
| 147 | - } |
|
| 148 | - $this->markParentAsOutdated($mount->getId(), $change->getPath()); |
|
| 149 | - }); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - private function createStorage(StorageConfig $mount) { |
|
| 153 | - $class = $mount->getBackend()->getStorageClass(); |
|
| 154 | - return new $class($mount->getBackendOptions()); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - private function markParentAsOutdated($mountId, $path) { |
|
| 158 | - $parent = dirname($path); |
|
| 159 | - if ($parent === '.') { |
|
| 160 | - $parent = ''; |
|
| 161 | - } |
|
| 162 | - $this->updateQuery->execute([$parent, $mountId]); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - private function logUpdate(IChange $change, OutputInterface $output) { |
|
| 166 | - switch ($change->getType()) { |
|
| 167 | - case INotifyStorage::NOTIFY_ADDED: |
|
| 168 | - $text = 'added'; |
|
| 169 | - break; |
|
| 170 | - case INotifyStorage::NOTIFY_MODIFIED: |
|
| 171 | - $text = 'modified'; |
|
| 172 | - break; |
|
| 173 | - case INotifyStorage::NOTIFY_REMOVED: |
|
| 174 | - $text = 'removed'; |
|
| 175 | - break; |
|
| 176 | - case INotifyStorage::NOTIFY_RENAMED: |
|
| 177 | - $text = 'renamed'; |
|
| 178 | - break; |
|
| 179 | - default: |
|
| 180 | - return; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $text .= ' ' . $change->getPath(); |
|
| 184 | - if ($change instanceof IRenameChange) { |
|
| 185 | - $text .= ' to ' . $change->getTargetPath(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - $output->writeln($text); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, $verbose, OutputInterface $output) { |
|
| 192 | - usleep(100 * 1000); //give time for the notify to start |
|
| 193 | - $storage->file_put_contents('/.nc_test_file.txt', 'test content'); |
|
| 194 | - $storage->mkdir('/.nc_test_folder'); |
|
| 195 | - $storage->file_put_contents('/.nc_test_folder/subfile.txt', 'test content'); |
|
| 196 | - |
|
| 197 | - usleep(100 * 1000); //time for all changes to be processed |
|
| 198 | - $changes = $notifyHandler->getChanges(); |
|
| 199 | - |
|
| 200 | - $storage->unlink('/.nc_test_file.txt'); |
|
| 201 | - $storage->unlink('/.nc_test_folder/subfile.txt'); |
|
| 202 | - $storage->rmdir('/.nc_test_folder'); |
|
| 203 | - |
|
| 204 | - usleep(100 * 1000); //time for all changes to be processed |
|
| 205 | - $notifyHandler->getChanges(); // flush |
|
| 206 | - |
|
| 207 | - $foundRootChange = false; |
|
| 208 | - $foundSubfolderChange = false; |
|
| 209 | - |
|
| 210 | - foreach ($changes as $change) { |
|
| 211 | - if ($change->getPath() === '/.nc_test_file.txt' || $change->getPath() === '.nc_test_file.txt') { |
|
| 212 | - $foundRootChange = true; |
|
| 213 | - } else if ($change->getPath() === '/.nc_test_folder/subfile.txt' || $change->getPath() === '.nc_test_folder/subfile.txt') { |
|
| 214 | - $foundSubfolderChange = true; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if ($foundRootChange && $foundSubfolderChange && $verbose) { |
|
| 219 | - $output->writeln('<info>Self-test successful</info>'); |
|
| 220 | - } else if ($foundRootChange && !$foundSubfolderChange) { |
|
| 221 | - $output->writeln('<error>Error while running self-test, change is subfolder not detected</error>'); |
|
| 222 | - } else if (!$foundRootChange) { |
|
| 223 | - $output->writeln('<error>Error while running self-test, no changes detected</error>'); |
|
| 224 | - } |
|
| 225 | - } |
|
| 60 | + ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + protected function configure() { |
|
| 64 | + $this |
|
| 65 | + ->setName('files_external:notify') |
|
| 66 | + ->setDescription('Listen for active update notifications for a configured external mount') |
|
| 67 | + ->addArgument( |
|
| 68 | + 'mount_id', |
|
| 69 | + InputArgument::REQUIRED, |
|
| 70 | + 'the mount id of the mount to listen to' |
|
| 71 | + )->addOption( |
|
| 72 | + 'user', |
|
| 73 | + 'u', |
|
| 74 | + InputOption::VALUE_REQUIRED, |
|
| 75 | + 'The username for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
| 76 | + )->addOption( |
|
| 77 | + 'password', |
|
| 78 | + 'p', |
|
| 79 | + InputOption::VALUE_REQUIRED, |
|
| 80 | + 'The password for the remote mount (required only for some mount configuration that don\'t store credentials)' |
|
| 81 | + )->addOption( |
|
| 82 | + 'path', |
|
| 83 | + '', |
|
| 84 | + InputOption::VALUE_REQUIRED, |
|
| 85 | + 'The directory in the storage to listen for updates in', |
|
| 86 | + '/' |
|
| 87 | + ); |
|
| 88 | + parent::configure(); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + protected function execute(InputInterface $input, OutputInterface $output) { |
|
| 92 | + $mount = $this->globalService->getStorage($input->getArgument('mount_id')); |
|
| 93 | + if (is_null($mount)) { |
|
| 94 | + $output->writeln('<error>Mount not found</error>'); |
|
| 95 | + return 1; |
|
| 96 | + } |
|
| 97 | + $noAuth = false; |
|
| 98 | + try { |
|
| 99 | + $authBackend = $mount->getAuthMechanism(); |
|
| 100 | + $authBackend->manipulateStorageConfig($mount); |
|
| 101 | + } catch (InsufficientDataForMeaningfulAnswerException $e) { |
|
| 102 | + $noAuth = true; |
|
| 103 | + } catch (StorageNotAvailableException $e) { |
|
| 104 | + $noAuth = true; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + if ($input->getOption('user')) { |
|
| 108 | + $mount->setBackendOption('user', $input->getOption('user')); |
|
| 109 | + } else if (isset($_ENV['NOTIFY_USER'])) { |
|
| 110 | + $mount->setBackendOption('user', $_ENV['NOTIFY_USER']); |
|
| 111 | + } else if (isset($_SERVER['NOTIFY_USER'])) { |
|
| 112 | + $mount->setBackendOption('user', $_SERVER['NOTIFY_USER']); |
|
| 113 | + } |
|
| 114 | + if ($input->getOption('password')) { |
|
| 115 | + $mount->setBackendOption('password', $input->getOption('password')); |
|
| 116 | + } else if (isset($_ENV['NOTIFY_PASSWORD'])) { |
|
| 117 | + $mount->setBackendOption('password', $_ENV['NOTIFY_PASSWORD']); |
|
| 118 | + } else if (isset($_SERVER['NOTIFY_PASSWORD'])) { |
|
| 119 | + $mount->setBackendOption('password', $_SERVER['NOTIFY_PASSWORD']); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + try { |
|
| 123 | + $storage = $this->createStorage($mount); |
|
| 124 | + } catch (\Exception $e) { |
|
| 125 | + $output->writeln('<error>Error while trying to create storage</error>'); |
|
| 126 | + if ($noAuth) { |
|
| 127 | + $output->writeln('<error>Username and/or password required</error>'); |
|
| 128 | + } |
|
| 129 | + return 1; |
|
| 130 | + } |
|
| 131 | + if (!$storage instanceof INotifyStorage) { |
|
| 132 | + $output->writeln('<error>Mount of type "' . $mount->getBackend()->getText() . '" does not support active update notifications</error>'); |
|
| 133 | + return 1; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + $verbose = $input->getOption('verbose'); |
|
| 137 | + |
|
| 138 | + $path = trim($input->getOption('path'), '/'); |
|
| 139 | + $notifyHandler = $storage->notify($path); |
|
| 140 | + $this->selfTest($storage, $notifyHandler, $verbose, $output); |
|
| 141 | + $notifyHandler->listen(function (IChange $change) use ($mount, $verbose, $output) { |
|
| 142 | + if ($verbose) { |
|
| 143 | + $this->logUpdate($change, $output); |
|
| 144 | + } |
|
| 145 | + if ($change instanceof IRenameChange) { |
|
| 146 | + $this->markParentAsOutdated($mount->getId(), $change->getTargetPath()); |
|
| 147 | + } |
|
| 148 | + $this->markParentAsOutdated($mount->getId(), $change->getPath()); |
|
| 149 | + }); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + private function createStorage(StorageConfig $mount) { |
|
| 153 | + $class = $mount->getBackend()->getStorageClass(); |
|
| 154 | + return new $class($mount->getBackendOptions()); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + private function markParentAsOutdated($mountId, $path) { |
|
| 158 | + $parent = dirname($path); |
|
| 159 | + if ($parent === '.') { |
|
| 160 | + $parent = ''; |
|
| 161 | + } |
|
| 162 | + $this->updateQuery->execute([$parent, $mountId]); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + private function logUpdate(IChange $change, OutputInterface $output) { |
|
| 166 | + switch ($change->getType()) { |
|
| 167 | + case INotifyStorage::NOTIFY_ADDED: |
|
| 168 | + $text = 'added'; |
|
| 169 | + break; |
|
| 170 | + case INotifyStorage::NOTIFY_MODIFIED: |
|
| 171 | + $text = 'modified'; |
|
| 172 | + break; |
|
| 173 | + case INotifyStorage::NOTIFY_REMOVED: |
|
| 174 | + $text = 'removed'; |
|
| 175 | + break; |
|
| 176 | + case INotifyStorage::NOTIFY_RENAMED: |
|
| 177 | + $text = 'renamed'; |
|
| 178 | + break; |
|
| 179 | + default: |
|
| 180 | + return; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $text .= ' ' . $change->getPath(); |
|
| 184 | + if ($change instanceof IRenameChange) { |
|
| 185 | + $text .= ' to ' . $change->getTargetPath(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + $output->writeln($text); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + private function selfTest(IStorage $storage, INotifyHandler $notifyHandler, $verbose, OutputInterface $output) { |
|
| 192 | + usleep(100 * 1000); //give time for the notify to start |
|
| 193 | + $storage->file_put_contents('/.nc_test_file.txt', 'test content'); |
|
| 194 | + $storage->mkdir('/.nc_test_folder'); |
|
| 195 | + $storage->file_put_contents('/.nc_test_folder/subfile.txt', 'test content'); |
|
| 196 | + |
|
| 197 | + usleep(100 * 1000); //time for all changes to be processed |
|
| 198 | + $changes = $notifyHandler->getChanges(); |
|
| 199 | + |
|
| 200 | + $storage->unlink('/.nc_test_file.txt'); |
|
| 201 | + $storage->unlink('/.nc_test_folder/subfile.txt'); |
|
| 202 | + $storage->rmdir('/.nc_test_folder'); |
|
| 203 | + |
|
| 204 | + usleep(100 * 1000); //time for all changes to be processed |
|
| 205 | + $notifyHandler->getChanges(); // flush |
|
| 206 | + |
|
| 207 | + $foundRootChange = false; |
|
| 208 | + $foundSubfolderChange = false; |
|
| 209 | + |
|
| 210 | + foreach ($changes as $change) { |
|
| 211 | + if ($change->getPath() === '/.nc_test_file.txt' || $change->getPath() === '.nc_test_file.txt') { |
|
| 212 | + $foundRootChange = true; |
|
| 213 | + } else if ($change->getPath() === '/.nc_test_folder/subfile.txt' || $change->getPath() === '.nc_test_folder/subfile.txt') { |
|
| 214 | + $foundSubfolderChange = true; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if ($foundRootChange && $foundSubfolderChange && $verbose) { |
|
| 219 | + $output->writeln('<info>Self-test successful</info>'); |
|
| 220 | + } else if ($foundRootChange && !$foundSubfolderChange) { |
|
| 221 | + $output->writeln('<error>Error while running self-test, change is subfolder not detected</error>'); |
|
| 222 | + } else if (!$foundRootChange) { |
|
| 223 | + $output->writeln('<error>Error while running self-test, no changes detected</error>'); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | 226 | } |