@@ -72,17 +72,17 @@ discard block |
||
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | protected function getStorageConfigFromDBMount(array $mount) { |
| 75 | - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 75 | + $applicableUsers = array_filter($mount['applicable'], function($applicable) { |
|
| 76 | 76 | return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
| 77 | 77 | }); |
| 78 | - $applicableUsers = array_map(function ($applicable) { |
|
| 78 | + $applicableUsers = array_map(function($applicable) { |
|
| 79 | 79 | return $applicable['value']; |
| 80 | 80 | }, $applicableUsers); |
| 81 | 81 | |
| 82 | - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 82 | + $applicableGroups = array_filter($mount['applicable'], function($applicable) { |
|
| 83 | 83 | return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
| 84 | 84 | }); |
| 85 | - $applicableGroups = array_map(function ($applicable) { |
|
| 85 | + $applicableGroups = array_map(function($applicable) { |
|
| 86 | 86 | return $applicable['value']; |
| 87 | 87 | }, $applicableGroups); |
| 88 | 88 | |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $mount['priority'] |
| 99 | 99 | ); |
| 100 | 100 | $config->setType($mount['type']); |
| 101 | - $config->setId((int)$mount['mount_id']); |
|
| 101 | + $config->setId((int) $mount['mount_id']); |
|
| 102 | 102 | return $config; |
| 103 | 103 | } catch (\UnexpectedValueException $e) { |
| 104 | 104 | // don't die if a storage backend doesn't exist |
@@ -126,11 +126,11 @@ discard block |
||
| 126 | 126 | protected function readConfig() { |
| 127 | 127 | $mounts = $this->readDBConfig(); |
| 128 | 128 | $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
| 129 | - $configs = array_filter($configs, function ($config) { |
|
| 129 | + $configs = array_filter($configs, function($config) { |
|
| 130 | 130 | return $config instanceof StorageConfig; |
| 131 | 131 | }); |
| 132 | 132 | |
| 133 | - $keys = array_map(function (StorageConfig $config) { |
|
| 133 | + $keys = array_map(function(StorageConfig $config) { |
|
| 134 | 134 | return $config->getId(); |
| 135 | 135 | }, $configs); |
| 136 | 136 | |
@@ -149,14 +149,14 @@ discard block |
||
| 149 | 149 | $mount = $this->dbConfig->getMountById($id); |
| 150 | 150 | |
| 151 | 151 | if (!is_array($mount)) { |
| 152 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 152 | + throw new NotFoundException('Storage with ID "'.$id.'" not found'); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | $config = $this->getStorageConfigFromDBMount($mount); |
| 156 | 156 | if ($this->isApplicable($config)) { |
| 157 | 157 | return $config; |
| 158 | 158 | } else { |
| 159 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 159 | + throw new NotFoundException('Storage with ID "'.$id.'" not found'); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
@@ -379,13 +379,13 @@ discard block |
||
| 379 | 379 | $existingMount = $this->dbConfig->getMountById($id); |
| 380 | 380 | |
| 381 | 381 | if (!is_array($existingMount)) { |
| 382 | - throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
| 382 | + throw new NotFoundException('Storage with ID "'.$id.'" not found while updating storage'); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
| 386 | 386 | |
| 387 | 387 | if ($oldStorage->getBackend() instanceof InvalidBackend) { |
| 388 | - throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
| 388 | + throw new NotFoundException('Storage with id "'.$id.'" cannot be edited due to missing backend'); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | $existingMount = $this->dbConfig->getMountById($id); |
| 463 | 463 | |
| 464 | 464 | if (!is_array($existingMount)) { |
| 465 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 465 | + throw new NotFoundException('Storage with ID "'.$id.'" not found'); |
|
| 466 | 466 | } |
| 467 | 467 | |
| 468 | 468 | $this->dbConfig->removeMount($id); |
@@ -28,448 +28,448 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | abstract class StoragesService { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param BackendService $backendService |
|
| 33 | - * @param DBConfigService $dbConfig |
|
| 34 | - * @param IUserMountCache $userMountCache |
|
| 35 | - * @param IEventDispatcher $eventDispatcher |
|
| 36 | - */ |
|
| 37 | - public function __construct( |
|
| 38 | - protected BackendService $backendService, |
|
| 39 | - protected DBConfigService $dbConfig, |
|
| 40 | - protected IUserMountCache $userMountCache, |
|
| 41 | - protected IEventDispatcher $eventDispatcher, |
|
| 42 | - ) { |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - protected function readDBConfig() { |
|
| 46 | - return $this->dbConfig->getAdminMounts(); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - protected function getStorageConfigFromDBMount(array $mount) { |
|
| 50 | - $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 51 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
| 52 | - }); |
|
| 53 | - $applicableUsers = array_map(function ($applicable) { |
|
| 54 | - return $applicable['value']; |
|
| 55 | - }, $applicableUsers); |
|
| 56 | - |
|
| 57 | - $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 58 | - return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
| 59 | - }); |
|
| 60 | - $applicableGroups = array_map(function ($applicable) { |
|
| 61 | - return $applicable['value']; |
|
| 62 | - }, $applicableGroups); |
|
| 63 | - |
|
| 64 | - try { |
|
| 65 | - $config = $this->createStorage( |
|
| 66 | - $mount['mount_point'], |
|
| 67 | - $mount['storage_backend'], |
|
| 68 | - $mount['auth_backend'], |
|
| 69 | - $mount['config'], |
|
| 70 | - $mount['options'], |
|
| 71 | - array_values($applicableUsers), |
|
| 72 | - array_values($applicableGroups), |
|
| 73 | - $mount['priority'] |
|
| 74 | - ); |
|
| 75 | - $config->setType($mount['type']); |
|
| 76 | - $config->setId((int)$mount['mount_id']); |
|
| 77 | - return $config; |
|
| 78 | - } catch (\UnexpectedValueException $e) { |
|
| 79 | - // don't die if a storage backend doesn't exist |
|
| 80 | - Server::get(LoggerInterface::class)->error('Could not load storage.', [ |
|
| 81 | - 'app' => 'files_external', |
|
| 82 | - 'exception' => $e, |
|
| 83 | - ]); |
|
| 84 | - return null; |
|
| 85 | - } catch (\InvalidArgumentException $e) { |
|
| 86 | - Server::get(LoggerInterface::class)->error('Could not load storage.', [ |
|
| 87 | - 'app' => 'files_external', |
|
| 88 | - 'exception' => $e, |
|
| 89 | - ]); |
|
| 90 | - return null; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Read the external storage config |
|
| 96 | - * |
|
| 97 | - * @return array map of storage id to storage config |
|
| 98 | - */ |
|
| 99 | - protected function readConfig() { |
|
| 100 | - $mounts = $this->readDBConfig(); |
|
| 101 | - $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
| 102 | - $configs = array_filter($configs, function ($config) { |
|
| 103 | - return $config instanceof StorageConfig; |
|
| 104 | - }); |
|
| 105 | - |
|
| 106 | - $keys = array_map(function (StorageConfig $config) { |
|
| 107 | - return $config->getId(); |
|
| 108 | - }, $configs); |
|
| 109 | - |
|
| 110 | - return array_combine($keys, $configs); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Get a storage with status |
|
| 115 | - * |
|
| 116 | - * @param int $id storage id |
|
| 117 | - * |
|
| 118 | - * @return StorageConfig |
|
| 119 | - * @throws NotFoundException if the storage with the given id was not found |
|
| 120 | - */ |
|
| 121 | - public function getStorage(int $id) { |
|
| 122 | - $mount = $this->dbConfig->getMountById($id); |
|
| 123 | - |
|
| 124 | - if (!is_array($mount)) { |
|
| 125 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $config = $this->getStorageConfigFromDBMount($mount); |
|
| 129 | - if ($this->isApplicable($config)) { |
|
| 130 | - return $config; |
|
| 131 | - } else { |
|
| 132 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 133 | - } |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Check whether this storage service should provide access to a storage |
|
| 138 | - * |
|
| 139 | - * @param StorageConfig $config |
|
| 140 | - * @return bool |
|
| 141 | - */ |
|
| 142 | - abstract protected function isApplicable(StorageConfig $config); |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Gets all storages, valid or not |
|
| 146 | - * |
|
| 147 | - * @return StorageConfig[] array of storage configs |
|
| 148 | - */ |
|
| 149 | - public function getAllStorages() { |
|
| 150 | - return $this->readConfig(); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Gets all valid storages |
|
| 155 | - * |
|
| 156 | - * @return StorageConfig[] |
|
| 157 | - */ |
|
| 158 | - public function getStorages() { |
|
| 159 | - return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Validate storage |
|
| 164 | - * FIXME: De-duplicate with StoragesController::validate() |
|
| 165 | - * |
|
| 166 | - * @param StorageConfig $storage |
|
| 167 | - * @return bool |
|
| 168 | - */ |
|
| 169 | - protected function validateStorage(StorageConfig $storage) { |
|
| 170 | - /** @var Backend */ |
|
| 171 | - $backend = $storage->getBackend(); |
|
| 172 | - /** @var AuthMechanism */ |
|
| 173 | - $authMechanism = $storage->getAuthMechanism(); |
|
| 174 | - |
|
| 175 | - if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
| 176 | - // not permitted to use backend |
|
| 177 | - return false; |
|
| 178 | - } |
|
| 179 | - if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
| 180 | - // not permitted to use auth mechanism |
|
| 181 | - return false; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return true; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Get the visibility type for this controller, used in validation |
|
| 189 | - * |
|
| 190 | - * @return int BackendService::VISIBILITY_* constants |
|
| 191 | - */ |
|
| 192 | - abstract public function getVisibilityType(); |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * @return integer |
|
| 196 | - */ |
|
| 197 | - protected function getType() { |
|
| 198 | - return DBConfigService::MOUNT_TYPE_ADMIN; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Add new storage to the configuration |
|
| 203 | - * |
|
| 204 | - * @param StorageConfig $newStorage storage attributes |
|
| 205 | - * |
|
| 206 | - * @return StorageConfig storage config, with added id |
|
| 207 | - */ |
|
| 208 | - public function addStorage(StorageConfig $newStorage) { |
|
| 209 | - $allStorages = $this->readConfig(); |
|
| 210 | - |
|
| 211 | - $configId = $this->dbConfig->addMount( |
|
| 212 | - $newStorage->getMountPoint(), |
|
| 213 | - $newStorage->getBackend()->getIdentifier(), |
|
| 214 | - $newStorage->getAuthMechanism()->getIdentifier(), |
|
| 215 | - $newStorage->getPriority(), |
|
| 216 | - $this->getType() |
|
| 217 | - ); |
|
| 218 | - |
|
| 219 | - $newStorage->setId($configId); |
|
| 220 | - |
|
| 221 | - foreach ($newStorage->getApplicableUsers() as $user) { |
|
| 222 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 223 | - } |
|
| 224 | - foreach ($newStorage->getApplicableGroups() as $group) { |
|
| 225 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 226 | - } |
|
| 227 | - foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
| 228 | - $this->dbConfig->setConfig($configId, $key, $value); |
|
| 229 | - } |
|
| 230 | - foreach ($newStorage->getMountOptions() as $key => $value) { |
|
| 231 | - $this->dbConfig->setOption($configId, $key, $value); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
| 235 | - $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - // add new storage |
|
| 239 | - $allStorages[$configId] = $newStorage; |
|
| 240 | - |
|
| 241 | - $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
| 242 | - |
|
| 243 | - $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
| 244 | - return $newStorage; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Create a storage from its parameters |
|
| 249 | - * |
|
| 250 | - * @param string $mountPoint storage mount point |
|
| 251 | - * @param string $backendIdentifier backend identifier |
|
| 252 | - * @param string $authMechanismIdentifier authentication mechanism identifier |
|
| 253 | - * @param array $backendOptions backend-specific options |
|
| 254 | - * @param array|null $mountOptions mount-specific options |
|
| 255 | - * @param array|null $applicableUsers users for which to mount the storage |
|
| 256 | - * @param array|null $applicableGroups groups for which to mount the storage |
|
| 257 | - * @param int|null $priority priority |
|
| 258 | - * |
|
| 259 | - * @return StorageConfig |
|
| 260 | - */ |
|
| 261 | - public function createStorage( |
|
| 262 | - $mountPoint, |
|
| 263 | - $backendIdentifier, |
|
| 264 | - $authMechanismIdentifier, |
|
| 265 | - $backendOptions, |
|
| 266 | - $mountOptions = null, |
|
| 267 | - $applicableUsers = null, |
|
| 268 | - $applicableGroups = null, |
|
| 269 | - $priority = null, |
|
| 270 | - ) { |
|
| 271 | - $backend = $this->backendService->getBackend($backendIdentifier); |
|
| 272 | - if (!$backend) { |
|
| 273 | - $backend = new InvalidBackend($backendIdentifier); |
|
| 274 | - } |
|
| 275 | - $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
| 276 | - if (!$authMechanism) { |
|
| 277 | - $authMechanism = new InvalidAuth($authMechanismIdentifier); |
|
| 278 | - } |
|
| 279 | - $newStorage = new StorageConfig(); |
|
| 280 | - $newStorage->setMountPoint($mountPoint); |
|
| 281 | - $newStorage->setBackend($backend); |
|
| 282 | - $newStorage->setAuthMechanism($authMechanism); |
|
| 283 | - $newStorage->setBackendOptions($backendOptions); |
|
| 284 | - if (isset($mountOptions)) { |
|
| 285 | - $newStorage->setMountOptions($mountOptions); |
|
| 286 | - } |
|
| 287 | - if (isset($applicableUsers)) { |
|
| 288 | - $newStorage->setApplicableUsers($applicableUsers); |
|
| 289 | - } |
|
| 290 | - if (isset($applicableGroups)) { |
|
| 291 | - $newStorage->setApplicableGroups($applicableGroups); |
|
| 292 | - } |
|
| 293 | - if (isset($priority)) { |
|
| 294 | - $newStorage->setPriority($priority); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return $newStorage; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Triggers the given hook signal for all the applicables given |
|
| 302 | - * |
|
| 303 | - * @param string $signal signal |
|
| 304 | - * @param string $mountPoint hook mount point param |
|
| 305 | - * @param string $mountType hook mount type param |
|
| 306 | - * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
| 307 | - */ |
|
| 308 | - protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void { |
|
| 309 | - $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null)); |
|
| 310 | - foreach ($applicableArray as $applicable) { |
|
| 311 | - Util::emitHook( |
|
| 312 | - Filesystem::CLASSNAME, |
|
| 313 | - $signal, |
|
| 314 | - [ |
|
| 315 | - Filesystem::signal_param_path => $mountPoint, |
|
| 316 | - Filesystem::signal_param_mount_type => $mountType, |
|
| 317 | - Filesystem::signal_param_users => $applicable, |
|
| 318 | - ] |
|
| 319 | - ); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * Triggers $signal for all applicable users of the given |
|
| 325 | - * storage |
|
| 326 | - * |
|
| 327 | - * @param StorageConfig $storage storage data |
|
| 328 | - * @param string $signal signal to trigger |
|
| 329 | - */ |
|
| 330 | - abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * Triggers signal_create_mount or signal_delete_mount to |
|
| 334 | - * accommodate for additions/deletions in applicableUsers |
|
| 335 | - * and applicableGroups fields. |
|
| 336 | - * |
|
| 337 | - * @param StorageConfig $oldStorage old storage data |
|
| 338 | - * @param StorageConfig $newStorage new storage data |
|
| 339 | - */ |
|
| 340 | - abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Update storage to the configuration |
|
| 344 | - * |
|
| 345 | - * @param StorageConfig $updatedStorage storage attributes |
|
| 346 | - * |
|
| 347 | - * @return StorageConfig storage config |
|
| 348 | - * @throws NotFoundException if the given storage does not exist in the config |
|
| 349 | - */ |
|
| 350 | - public function updateStorage(StorageConfig $updatedStorage) { |
|
| 351 | - $id = $updatedStorage->getId(); |
|
| 352 | - |
|
| 353 | - $existingMount = $this->dbConfig->getMountById($id); |
|
| 354 | - |
|
| 355 | - if (!is_array($existingMount)) { |
|
| 356 | - throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 360 | - |
|
| 361 | - if ($oldStorage->getBackend() instanceof InvalidBackend) { |
|
| 362 | - throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
| 366 | - $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
| 367 | - $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
| 368 | - $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
| 369 | - |
|
| 370 | - $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
| 371 | - $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
| 372 | - $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
| 373 | - $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
| 374 | - $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
| 375 | - $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
| 376 | - |
|
| 377 | - foreach ($removedUsers as $user) { |
|
| 378 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 379 | - } |
|
| 380 | - foreach ($removedGroups as $group) { |
|
| 381 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 382 | - } |
|
| 383 | - foreach ($addedUsers as $user) { |
|
| 384 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 385 | - } |
|
| 386 | - foreach ($addedGroups as $group) { |
|
| 387 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - if ($wasGlobal && !$isGlobal) { |
|
| 391 | - $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 392 | - } elseif (!$wasGlobal && $isGlobal) { |
|
| 393 | - $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
| 397 | - $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
| 398 | - |
|
| 399 | - foreach ($changedConfig as $key => $value) { |
|
| 400 | - if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { |
|
| 401 | - $this->dbConfig->setConfig($id, $key, $value); |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - foreach ($changedOptions as $key => $value) { |
|
| 405 | - $this->dbConfig->setOption($id, $key, $value); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
| 409 | - $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
| 413 | - $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
| 417 | - |
|
| 418 | - if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
| 419 | - $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
| 420 | - } else { |
|
| 421 | - $storageId = $this->getStorageId($updatedStorage); |
|
| 422 | - foreach ($removedUsers as $userId) { |
|
| 423 | - $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $this->getStorage($id); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - /** |
|
| 431 | - * Delete the storage with the given id. |
|
| 432 | - * |
|
| 433 | - * @param int $id storage id |
|
| 434 | - * |
|
| 435 | - * @throws NotFoundException if no storage was found with the given id |
|
| 436 | - */ |
|
| 437 | - public function removeStorage(int $id) { |
|
| 438 | - $existingMount = $this->dbConfig->getMountById($id); |
|
| 439 | - |
|
| 440 | - if (!is_array($existingMount)) { |
|
| 441 | - throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - $this->dbConfig->removeMount($id); |
|
| 445 | - |
|
| 446 | - $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 447 | - $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
| 448 | - |
|
| 449 | - // delete oc_storages entries and oc_filecache |
|
| 450 | - Storage::cleanByMountId($id); |
|
| 451 | - } |
|
| 452 | - |
|
| 453 | - /** |
|
| 454 | - * Construct the storage implementation |
|
| 455 | - * |
|
| 456 | - * @param StorageConfig $storageConfig |
|
| 457 | - * @return int |
|
| 458 | - */ |
|
| 459 | - private function getStorageId(StorageConfig $storageConfig) { |
|
| 460 | - try { |
|
| 461 | - $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 462 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 463 | - $storage = new $class($storageConfig->getBackendOptions()); |
|
| 464 | - |
|
| 465 | - // auth mechanism should fire first |
|
| 466 | - $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
| 467 | - $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
| 468 | - |
|
| 469 | - /** @var \OC\Files\Storage\Storage $storage */ |
|
| 470 | - return $storage->getStorageCache()->getNumericId(); |
|
| 471 | - } catch (\Exception $e) { |
|
| 472 | - return -1; |
|
| 473 | - } |
|
| 474 | - } |
|
| 31 | + /** |
|
| 32 | + * @param BackendService $backendService |
|
| 33 | + * @param DBConfigService $dbConfig |
|
| 34 | + * @param IUserMountCache $userMountCache |
|
| 35 | + * @param IEventDispatcher $eventDispatcher |
|
| 36 | + */ |
|
| 37 | + public function __construct( |
|
| 38 | + protected BackendService $backendService, |
|
| 39 | + protected DBConfigService $dbConfig, |
|
| 40 | + protected IUserMountCache $userMountCache, |
|
| 41 | + protected IEventDispatcher $eventDispatcher, |
|
| 42 | + ) { |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + protected function readDBConfig() { |
|
| 46 | + return $this->dbConfig->getAdminMounts(); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + protected function getStorageConfigFromDBMount(array $mount) { |
|
| 50 | + $applicableUsers = array_filter($mount['applicable'], function ($applicable) { |
|
| 51 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_USER; |
|
| 52 | + }); |
|
| 53 | + $applicableUsers = array_map(function ($applicable) { |
|
| 54 | + return $applicable['value']; |
|
| 55 | + }, $applicableUsers); |
|
| 56 | + |
|
| 57 | + $applicableGroups = array_filter($mount['applicable'], function ($applicable) { |
|
| 58 | + return $applicable['type'] === DBConfigService::APPLICABLE_TYPE_GROUP; |
|
| 59 | + }); |
|
| 60 | + $applicableGroups = array_map(function ($applicable) { |
|
| 61 | + return $applicable['value']; |
|
| 62 | + }, $applicableGroups); |
|
| 63 | + |
|
| 64 | + try { |
|
| 65 | + $config = $this->createStorage( |
|
| 66 | + $mount['mount_point'], |
|
| 67 | + $mount['storage_backend'], |
|
| 68 | + $mount['auth_backend'], |
|
| 69 | + $mount['config'], |
|
| 70 | + $mount['options'], |
|
| 71 | + array_values($applicableUsers), |
|
| 72 | + array_values($applicableGroups), |
|
| 73 | + $mount['priority'] |
|
| 74 | + ); |
|
| 75 | + $config->setType($mount['type']); |
|
| 76 | + $config->setId((int)$mount['mount_id']); |
|
| 77 | + return $config; |
|
| 78 | + } catch (\UnexpectedValueException $e) { |
|
| 79 | + // don't die if a storage backend doesn't exist |
|
| 80 | + Server::get(LoggerInterface::class)->error('Could not load storage.', [ |
|
| 81 | + 'app' => 'files_external', |
|
| 82 | + 'exception' => $e, |
|
| 83 | + ]); |
|
| 84 | + return null; |
|
| 85 | + } catch (\InvalidArgumentException $e) { |
|
| 86 | + Server::get(LoggerInterface::class)->error('Could not load storage.', [ |
|
| 87 | + 'app' => 'files_external', |
|
| 88 | + 'exception' => $e, |
|
| 89 | + ]); |
|
| 90 | + return null; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Read the external storage config |
|
| 96 | + * |
|
| 97 | + * @return array map of storage id to storage config |
|
| 98 | + */ |
|
| 99 | + protected function readConfig() { |
|
| 100 | + $mounts = $this->readDBConfig(); |
|
| 101 | + $configs = array_map([$this, 'getStorageConfigFromDBMount'], $mounts); |
|
| 102 | + $configs = array_filter($configs, function ($config) { |
|
| 103 | + return $config instanceof StorageConfig; |
|
| 104 | + }); |
|
| 105 | + |
|
| 106 | + $keys = array_map(function (StorageConfig $config) { |
|
| 107 | + return $config->getId(); |
|
| 108 | + }, $configs); |
|
| 109 | + |
|
| 110 | + return array_combine($keys, $configs); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Get a storage with status |
|
| 115 | + * |
|
| 116 | + * @param int $id storage id |
|
| 117 | + * |
|
| 118 | + * @return StorageConfig |
|
| 119 | + * @throws NotFoundException if the storage with the given id was not found |
|
| 120 | + */ |
|
| 121 | + public function getStorage(int $id) { |
|
| 122 | + $mount = $this->dbConfig->getMountById($id); |
|
| 123 | + |
|
| 124 | + if (!is_array($mount)) { |
|
| 125 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $config = $this->getStorageConfigFromDBMount($mount); |
|
| 129 | + if ($this->isApplicable($config)) { |
|
| 130 | + return $config; |
|
| 131 | + } else { |
|
| 132 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 133 | + } |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Check whether this storage service should provide access to a storage |
|
| 138 | + * |
|
| 139 | + * @param StorageConfig $config |
|
| 140 | + * @return bool |
|
| 141 | + */ |
|
| 142 | + abstract protected function isApplicable(StorageConfig $config); |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Gets all storages, valid or not |
|
| 146 | + * |
|
| 147 | + * @return StorageConfig[] array of storage configs |
|
| 148 | + */ |
|
| 149 | + public function getAllStorages() { |
|
| 150 | + return $this->readConfig(); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Gets all valid storages |
|
| 155 | + * |
|
| 156 | + * @return StorageConfig[] |
|
| 157 | + */ |
|
| 158 | + public function getStorages() { |
|
| 159 | + return array_filter($this->getAllStorages(), [$this, 'validateStorage']); |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Validate storage |
|
| 164 | + * FIXME: De-duplicate with StoragesController::validate() |
|
| 165 | + * |
|
| 166 | + * @param StorageConfig $storage |
|
| 167 | + * @return bool |
|
| 168 | + */ |
|
| 169 | + protected function validateStorage(StorageConfig $storage) { |
|
| 170 | + /** @var Backend */ |
|
| 171 | + $backend = $storage->getBackend(); |
|
| 172 | + /** @var AuthMechanism */ |
|
| 173 | + $authMechanism = $storage->getAuthMechanism(); |
|
| 174 | + |
|
| 175 | + if (!$backend->isVisibleFor($this->getVisibilityType())) { |
|
| 176 | + // not permitted to use backend |
|
| 177 | + return false; |
|
| 178 | + } |
|
| 179 | + if (!$authMechanism->isVisibleFor($this->getVisibilityType())) { |
|
| 180 | + // not permitted to use auth mechanism |
|
| 181 | + return false; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return true; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Get the visibility type for this controller, used in validation |
|
| 189 | + * |
|
| 190 | + * @return int BackendService::VISIBILITY_* constants |
|
| 191 | + */ |
|
| 192 | + abstract public function getVisibilityType(); |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * @return integer |
|
| 196 | + */ |
|
| 197 | + protected function getType() { |
|
| 198 | + return DBConfigService::MOUNT_TYPE_ADMIN; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Add new storage to the configuration |
|
| 203 | + * |
|
| 204 | + * @param StorageConfig $newStorage storage attributes |
|
| 205 | + * |
|
| 206 | + * @return StorageConfig storage config, with added id |
|
| 207 | + */ |
|
| 208 | + public function addStorage(StorageConfig $newStorage) { |
|
| 209 | + $allStorages = $this->readConfig(); |
|
| 210 | + |
|
| 211 | + $configId = $this->dbConfig->addMount( |
|
| 212 | + $newStorage->getMountPoint(), |
|
| 213 | + $newStorage->getBackend()->getIdentifier(), |
|
| 214 | + $newStorage->getAuthMechanism()->getIdentifier(), |
|
| 215 | + $newStorage->getPriority(), |
|
| 216 | + $this->getType() |
|
| 217 | + ); |
|
| 218 | + |
|
| 219 | + $newStorage->setId($configId); |
|
| 220 | + |
|
| 221 | + foreach ($newStorage->getApplicableUsers() as $user) { |
|
| 222 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 223 | + } |
|
| 224 | + foreach ($newStorage->getApplicableGroups() as $group) { |
|
| 225 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 226 | + } |
|
| 227 | + foreach ($newStorage->getBackendOptions() as $key => $value) { |
|
| 228 | + $this->dbConfig->setConfig($configId, $key, $value); |
|
| 229 | + } |
|
| 230 | + foreach ($newStorage->getMountOptions() as $key => $value) { |
|
| 231 | + $this->dbConfig->setOption($configId, $key, $value); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + if (count($newStorage->getApplicableUsers()) === 0 && count($newStorage->getApplicableGroups()) === 0) { |
|
| 235 | + $this->dbConfig->addApplicable($configId, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + // add new storage |
|
| 239 | + $allStorages[$configId] = $newStorage; |
|
| 240 | + |
|
| 241 | + $this->triggerHooks($newStorage, Filesystem::signal_create_mount); |
|
| 242 | + |
|
| 243 | + $newStorage->setStatus(StorageNotAvailableException::STATUS_SUCCESS); |
|
| 244 | + return $newStorage; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Create a storage from its parameters |
|
| 249 | + * |
|
| 250 | + * @param string $mountPoint storage mount point |
|
| 251 | + * @param string $backendIdentifier backend identifier |
|
| 252 | + * @param string $authMechanismIdentifier authentication mechanism identifier |
|
| 253 | + * @param array $backendOptions backend-specific options |
|
| 254 | + * @param array|null $mountOptions mount-specific options |
|
| 255 | + * @param array|null $applicableUsers users for which to mount the storage |
|
| 256 | + * @param array|null $applicableGroups groups for which to mount the storage |
|
| 257 | + * @param int|null $priority priority |
|
| 258 | + * |
|
| 259 | + * @return StorageConfig |
|
| 260 | + */ |
|
| 261 | + public function createStorage( |
|
| 262 | + $mountPoint, |
|
| 263 | + $backendIdentifier, |
|
| 264 | + $authMechanismIdentifier, |
|
| 265 | + $backendOptions, |
|
| 266 | + $mountOptions = null, |
|
| 267 | + $applicableUsers = null, |
|
| 268 | + $applicableGroups = null, |
|
| 269 | + $priority = null, |
|
| 270 | + ) { |
|
| 271 | + $backend = $this->backendService->getBackend($backendIdentifier); |
|
| 272 | + if (!$backend) { |
|
| 273 | + $backend = new InvalidBackend($backendIdentifier); |
|
| 274 | + } |
|
| 275 | + $authMechanism = $this->backendService->getAuthMechanism($authMechanismIdentifier); |
|
| 276 | + if (!$authMechanism) { |
|
| 277 | + $authMechanism = new InvalidAuth($authMechanismIdentifier); |
|
| 278 | + } |
|
| 279 | + $newStorage = new StorageConfig(); |
|
| 280 | + $newStorage->setMountPoint($mountPoint); |
|
| 281 | + $newStorage->setBackend($backend); |
|
| 282 | + $newStorage->setAuthMechanism($authMechanism); |
|
| 283 | + $newStorage->setBackendOptions($backendOptions); |
|
| 284 | + if (isset($mountOptions)) { |
|
| 285 | + $newStorage->setMountOptions($mountOptions); |
|
| 286 | + } |
|
| 287 | + if (isset($applicableUsers)) { |
|
| 288 | + $newStorage->setApplicableUsers($applicableUsers); |
|
| 289 | + } |
|
| 290 | + if (isset($applicableGroups)) { |
|
| 291 | + $newStorage->setApplicableGroups($applicableGroups); |
|
| 292 | + } |
|
| 293 | + if (isset($priority)) { |
|
| 294 | + $newStorage->setPriority($priority); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return $newStorage; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Triggers the given hook signal for all the applicables given |
|
| 302 | + * |
|
| 303 | + * @param string $signal signal |
|
| 304 | + * @param string $mountPoint hook mount point param |
|
| 305 | + * @param string $mountType hook mount type param |
|
| 306 | + * @param array $applicableArray array of applicable users/groups for which to trigger the hook |
|
| 307 | + */ |
|
| 308 | + protected function triggerApplicableHooks($signal, $mountPoint, $mountType, $applicableArray): void { |
|
| 309 | + $this->eventDispatcher->dispatchTyped(new InvalidateMountCacheEvent(null)); |
|
| 310 | + foreach ($applicableArray as $applicable) { |
|
| 311 | + Util::emitHook( |
|
| 312 | + Filesystem::CLASSNAME, |
|
| 313 | + $signal, |
|
| 314 | + [ |
|
| 315 | + Filesystem::signal_param_path => $mountPoint, |
|
| 316 | + Filesystem::signal_param_mount_type => $mountType, |
|
| 317 | + Filesystem::signal_param_users => $applicable, |
|
| 318 | + ] |
|
| 319 | + ); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * Triggers $signal for all applicable users of the given |
|
| 325 | + * storage |
|
| 326 | + * |
|
| 327 | + * @param StorageConfig $storage storage data |
|
| 328 | + * @param string $signal signal to trigger |
|
| 329 | + */ |
|
| 330 | + abstract protected function triggerHooks(StorageConfig $storage, $signal); |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * Triggers signal_create_mount or signal_delete_mount to |
|
| 334 | + * accommodate for additions/deletions in applicableUsers |
|
| 335 | + * and applicableGroups fields. |
|
| 336 | + * |
|
| 337 | + * @param StorageConfig $oldStorage old storage data |
|
| 338 | + * @param StorageConfig $newStorage new storage data |
|
| 339 | + */ |
|
| 340 | + abstract protected function triggerChangeHooks(StorageConfig $oldStorage, StorageConfig $newStorage); |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Update storage to the configuration |
|
| 344 | + * |
|
| 345 | + * @param StorageConfig $updatedStorage storage attributes |
|
| 346 | + * |
|
| 347 | + * @return StorageConfig storage config |
|
| 348 | + * @throws NotFoundException if the given storage does not exist in the config |
|
| 349 | + */ |
|
| 350 | + public function updateStorage(StorageConfig $updatedStorage) { |
|
| 351 | + $id = $updatedStorage->getId(); |
|
| 352 | + |
|
| 353 | + $existingMount = $this->dbConfig->getMountById($id); |
|
| 354 | + |
|
| 355 | + if (!is_array($existingMount)) { |
|
| 356 | + throw new NotFoundException('Storage with ID "' . $id . '" not found while updating storage'); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $oldStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 360 | + |
|
| 361 | + if ($oldStorage->getBackend() instanceof InvalidBackend) { |
|
| 362 | + throw new NotFoundException('Storage with id "' . $id . '" cannot be edited due to missing backend'); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + $removedUsers = array_diff($oldStorage->getApplicableUsers(), $updatedStorage->getApplicableUsers()); |
|
| 366 | + $removedGroups = array_diff($oldStorage->getApplicableGroups(), $updatedStorage->getApplicableGroups()); |
|
| 367 | + $addedUsers = array_diff($updatedStorage->getApplicableUsers(), $oldStorage->getApplicableUsers()); |
|
| 368 | + $addedGroups = array_diff($updatedStorage->getApplicableGroups(), $oldStorage->getApplicableGroups()); |
|
| 369 | + |
|
| 370 | + $oldUserCount = count($oldStorage->getApplicableUsers()); |
|
| 371 | + $oldGroupCount = count($oldStorage->getApplicableGroups()); |
|
| 372 | + $newUserCount = count($updatedStorage->getApplicableUsers()); |
|
| 373 | + $newGroupCount = count($updatedStorage->getApplicableGroups()); |
|
| 374 | + $wasGlobal = ($oldUserCount + $oldGroupCount) === 0; |
|
| 375 | + $isGlobal = ($newUserCount + $newGroupCount) === 0; |
|
| 376 | + |
|
| 377 | + foreach ($removedUsers as $user) { |
|
| 378 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 379 | + } |
|
| 380 | + foreach ($removedGroups as $group) { |
|
| 381 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 382 | + } |
|
| 383 | + foreach ($addedUsers as $user) { |
|
| 384 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_USER, $user); |
|
| 385 | + } |
|
| 386 | + foreach ($addedGroups as $group) { |
|
| 387 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GROUP, $group); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + if ($wasGlobal && !$isGlobal) { |
|
| 391 | + $this->dbConfig->removeApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 392 | + } elseif (!$wasGlobal && $isGlobal) { |
|
| 393 | + $this->dbConfig->addApplicable($id, DBConfigService::APPLICABLE_TYPE_GLOBAL, null); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + $changedConfig = array_diff_assoc($updatedStorage->getBackendOptions(), $oldStorage->getBackendOptions()); |
|
| 397 | + $changedOptions = array_diff_assoc($updatedStorage->getMountOptions(), $oldStorage->getMountOptions()); |
|
| 398 | + |
|
| 399 | + foreach ($changedConfig as $key => $value) { |
|
| 400 | + if ($value !== DefinitionParameter::UNMODIFIED_PLACEHOLDER) { |
|
| 401 | + $this->dbConfig->setConfig($id, $key, $value); |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + foreach ($changedOptions as $key => $value) { |
|
| 405 | + $this->dbConfig->setOption($id, $key, $value); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + if ($updatedStorage->getMountPoint() !== $oldStorage->getMountPoint()) { |
|
| 409 | + $this->dbConfig->setMountPoint($id, $updatedStorage->getMountPoint()); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + if ($updatedStorage->getAuthMechanism()->getIdentifier() !== $oldStorage->getAuthMechanism()->getIdentifier()) { |
|
| 413 | + $this->dbConfig->setAuthBackend($id, $updatedStorage->getAuthMechanism()->getIdentifier()); |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + $this->triggerChangeHooks($oldStorage, $updatedStorage); |
|
| 417 | + |
|
| 418 | + if (($wasGlobal && !$isGlobal) || count($removedGroups) > 0) { // to expensive to properly handle these on the fly |
|
| 419 | + $this->userMountCache->remoteStorageMounts($this->getStorageId($updatedStorage)); |
|
| 420 | + } else { |
|
| 421 | + $storageId = $this->getStorageId($updatedStorage); |
|
| 422 | + foreach ($removedUsers as $userId) { |
|
| 423 | + $this->userMountCache->removeUserStorageMount($storageId, $userId); |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $this->getStorage($id); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + /** |
|
| 431 | + * Delete the storage with the given id. |
|
| 432 | + * |
|
| 433 | + * @param int $id storage id |
|
| 434 | + * |
|
| 435 | + * @throws NotFoundException if no storage was found with the given id |
|
| 436 | + */ |
|
| 437 | + public function removeStorage(int $id) { |
|
| 438 | + $existingMount = $this->dbConfig->getMountById($id); |
|
| 439 | + |
|
| 440 | + if (!is_array($existingMount)) { |
|
| 441 | + throw new NotFoundException('Storage with ID "' . $id . '" not found'); |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + $this->dbConfig->removeMount($id); |
|
| 445 | + |
|
| 446 | + $deletedStorage = $this->getStorageConfigFromDBMount($existingMount); |
|
| 447 | + $this->triggerHooks($deletedStorage, Filesystem::signal_delete_mount); |
|
| 448 | + |
|
| 449 | + // delete oc_storages entries and oc_filecache |
|
| 450 | + Storage::cleanByMountId($id); |
|
| 451 | + } |
|
| 452 | + |
|
| 453 | + /** |
|
| 454 | + * Construct the storage implementation |
|
| 455 | + * |
|
| 456 | + * @param StorageConfig $storageConfig |
|
| 457 | + * @return int |
|
| 458 | + */ |
|
| 459 | + private function getStorageId(StorageConfig $storageConfig) { |
|
| 460 | + try { |
|
| 461 | + $class = $storageConfig->getBackend()->getStorageClass(); |
|
| 462 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 463 | + $storage = new $class($storageConfig->getBackendOptions()); |
|
| 464 | + |
|
| 465 | + // auth mechanism should fire first |
|
| 466 | + $storage = $storageConfig->getBackend()->wrapStorage($storage); |
|
| 467 | + $storage = $storageConfig->getAuthMechanism()->wrapStorage($storage); |
|
| 468 | + |
|
| 469 | + /** @var \OC\Files\Storage\Storage $storage */ |
|
| 470 | + return $storage->getStorageCache()->getNumericId(); |
|
| 471 | + } catch (\Exception $e) { |
|
| 472 | + return -1; |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | 475 | } |
@@ -36,7 +36,7 @@ |
||
| 36 | 36 | * @package OC\AppFramework\Middleware\Security\Exceptions |
| 37 | 37 | */ |
| 38 | 38 | class NotAdminException extends SecurityException { |
| 39 | - public function __construct(string $message) { |
|
| 40 | - parent::__construct($message, Http::STATUS_FORBIDDEN); |
|
| 41 | - } |
|
| 39 | + public function __construct(string $message) { |
|
| 40 | + parent::__construct($message, Http::STATUS_FORBIDDEN); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -100,7 +100,7 @@ |
||
| 100 | 100 | |
| 101 | 101 | // casted to string because cast to float cause equality for non equal numbers |
| 102 | 102 | // and integer has the problem of limited size on 32 bit systems |
| 103 | - if ((string)$expectedSize !== (string)$actualSize) { |
|
| 103 | + if ((string) $expectedSize !== (string) $actualSize) { |
|
| 104 | 104 | throw new BadRequest("Chunks on server do not sum up to $expectedSize but to $actualSize bytes"); |
| 105 | 105 | } |
| 106 | 106 | } |
@@ -18,97 +18,97 @@ |
||
| 18 | 18 | |
| 19 | 19 | class ChunkingPlugin extends ServerPlugin { |
| 20 | 20 | |
| 21 | - /** @var Server */ |
|
| 22 | - private $server; |
|
| 23 | - /** @var FutureFile */ |
|
| 24 | - private $sourceNode; |
|
| 21 | + /** @var Server */ |
|
| 22 | + private $server; |
|
| 23 | + /** @var FutureFile */ |
|
| 24 | + private $sourceNode; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @inheritdoc |
|
| 28 | - */ |
|
| 29 | - public function initialize(Server $server) { |
|
| 30 | - $server->on('beforeMove', [$this, 'beforeMove']); |
|
| 31 | - $this->server = $server; |
|
| 32 | - } |
|
| 26 | + /** |
|
| 27 | + * @inheritdoc |
|
| 28 | + */ |
|
| 29 | + public function initialize(Server $server) { |
|
| 30 | + $server->on('beforeMove', [$this, 'beforeMove']); |
|
| 31 | + $this->server = $server; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @param string $sourcePath source path |
|
| 36 | - * @param string $destination destination path |
|
| 37 | - * @return bool|void |
|
| 38 | - * @throws BadRequest |
|
| 39 | - * @throws NotFound |
|
| 40 | - */ |
|
| 41 | - public function beforeMove($sourcePath, $destination) { |
|
| 42 | - $this->sourceNode = $this->server->tree->getNodeForPath($sourcePath); |
|
| 43 | - if (!$this->sourceNode instanceof FutureFile) { |
|
| 44 | - // skip handling as the source is not a chunked FutureFile |
|
| 45 | - return; |
|
| 46 | - } |
|
| 34 | + /** |
|
| 35 | + * @param string $sourcePath source path |
|
| 36 | + * @param string $destination destination path |
|
| 37 | + * @return bool|void |
|
| 38 | + * @throws BadRequest |
|
| 39 | + * @throws NotFound |
|
| 40 | + */ |
|
| 41 | + public function beforeMove($sourcePath, $destination) { |
|
| 42 | + $this->sourceNode = $this->server->tree->getNodeForPath($sourcePath); |
|
| 43 | + if (!$this->sourceNode instanceof FutureFile) { |
|
| 44 | + // skip handling as the source is not a chunked FutureFile |
|
| 45 | + return; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - try { |
|
| 49 | - /** @var INode $destinationNode */ |
|
| 50 | - $destinationNode = $this->server->tree->getNodeForPath($destination); |
|
| 51 | - if ($destinationNode instanceof Directory) { |
|
| 52 | - throw new BadRequest("The given destination $destination is a directory."); |
|
| 53 | - } |
|
| 54 | - } catch (NotFound $e) { |
|
| 55 | - // If the destination does not exist yet it's not a directory either ;) |
|
| 56 | - } |
|
| 48 | + try { |
|
| 49 | + /** @var INode $destinationNode */ |
|
| 50 | + $destinationNode = $this->server->tree->getNodeForPath($destination); |
|
| 51 | + if ($destinationNode instanceof Directory) { |
|
| 52 | + throw new BadRequest("The given destination $destination is a directory."); |
|
| 53 | + } |
|
| 54 | + } catch (NotFound $e) { |
|
| 55 | + // If the destination does not exist yet it's not a directory either ;) |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $this->verifySize(); |
|
| 59 | - return $this->performMove($sourcePath, $destination); |
|
| 60 | - } |
|
| 58 | + $this->verifySize(); |
|
| 59 | + return $this->performMove($sourcePath, $destination); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Move handler for future file. |
|
| 64 | - * |
|
| 65 | - * This overrides the default move behavior to prevent Sabre |
|
| 66 | - * to delete the target file before moving. Because deleting would |
|
| 67 | - * lose the file id and metadata. |
|
| 68 | - * |
|
| 69 | - * @param string $path source path |
|
| 70 | - * @param string $destination destination path |
|
| 71 | - * @return bool|void false to stop handling, void to skip this handler |
|
| 72 | - */ |
|
| 73 | - public function performMove($path, $destination) { |
|
| 74 | - $fileExists = $this->server->tree->nodeExists($destination); |
|
| 75 | - // do a move manually, skipping Sabre's default "delete" for existing nodes |
|
| 76 | - try { |
|
| 77 | - $this->server->tree->move($path, $destination); |
|
| 78 | - } catch (Forbidden $e) { |
|
| 79 | - $sourceNode = $this->server->tree->getNodeForPath($path); |
|
| 80 | - if ($sourceNode instanceof FutureFile) { |
|
| 81 | - $sourceNode->delete(); |
|
| 82 | - } |
|
| 83 | - throw $e; |
|
| 84 | - } |
|
| 62 | + /** |
|
| 63 | + * Move handler for future file. |
|
| 64 | + * |
|
| 65 | + * This overrides the default move behavior to prevent Sabre |
|
| 66 | + * to delete the target file before moving. Because deleting would |
|
| 67 | + * lose the file id and metadata. |
|
| 68 | + * |
|
| 69 | + * @param string $path source path |
|
| 70 | + * @param string $destination destination path |
|
| 71 | + * @return bool|void false to stop handling, void to skip this handler |
|
| 72 | + */ |
|
| 73 | + public function performMove($path, $destination) { |
|
| 74 | + $fileExists = $this->server->tree->nodeExists($destination); |
|
| 75 | + // do a move manually, skipping Sabre's default "delete" for existing nodes |
|
| 76 | + try { |
|
| 77 | + $this->server->tree->move($path, $destination); |
|
| 78 | + } catch (Forbidden $e) { |
|
| 79 | + $sourceNode = $this->server->tree->getNodeForPath($path); |
|
| 80 | + if ($sourceNode instanceof FutureFile) { |
|
| 81 | + $sourceNode->delete(); |
|
| 82 | + } |
|
| 83 | + throw $e; |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - // trigger all default events (copied from CorePlugin::move) |
|
| 87 | - $this->server->emit('afterMove', [$path, $destination]); |
|
| 88 | - $this->server->emit('afterUnbind', [$path]); |
|
| 89 | - $this->server->emit('afterBind', [$destination]); |
|
| 86 | + // trigger all default events (copied from CorePlugin::move) |
|
| 87 | + $this->server->emit('afterMove', [$path, $destination]); |
|
| 88 | + $this->server->emit('afterUnbind', [$path]); |
|
| 89 | + $this->server->emit('afterBind', [$destination]); |
|
| 90 | 90 | |
| 91 | - $response = $this->server->httpResponse; |
|
| 92 | - $response->setHeader('Content-Length', '0'); |
|
| 93 | - $response->setStatus($fileExists ? Http::STATUS_NO_CONTENT : Http::STATUS_CREATED); |
|
| 91 | + $response = $this->server->httpResponse; |
|
| 92 | + $response->setHeader('Content-Length', '0'); |
|
| 93 | + $response->setStatus($fileExists ? Http::STATUS_NO_CONTENT : Http::STATUS_CREATED); |
|
| 94 | 94 | |
| 95 | - return false; |
|
| 96 | - } |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * @throws BadRequest |
|
| 100 | - */ |
|
| 101 | - private function verifySize() { |
|
| 102 | - $expectedSize = $this->server->httpRequest->getHeader('OC-Total-Length'); |
|
| 103 | - if ($expectedSize === null) { |
|
| 104 | - return; |
|
| 105 | - } |
|
| 106 | - $actualSize = $this->sourceNode->getSize(); |
|
| 98 | + /** |
|
| 99 | + * @throws BadRequest |
|
| 100 | + */ |
|
| 101 | + private function verifySize() { |
|
| 102 | + $expectedSize = $this->server->httpRequest->getHeader('OC-Total-Length'); |
|
| 103 | + if ($expectedSize === null) { |
|
| 104 | + return; |
|
| 105 | + } |
|
| 106 | + $actualSize = $this->sourceNode->getSize(); |
|
| 107 | 107 | |
| 108 | - // casted to string because cast to float cause equality for non equal numbers |
|
| 109 | - // and integer has the problem of limited size on 32 bit systems |
|
| 110 | - if ((string)$expectedSize !== (string)$actualSize) { |
|
| 111 | - throw new BadRequest("Chunks on server do not sum up to $expectedSize but to $actualSize bytes"); |
|
| 112 | - } |
|
| 113 | - } |
|
| 108 | + // casted to string because cast to float cause equality for non equal numbers |
|
| 109 | + // and integer has the problem of limited size on 32 bit systems |
|
| 110 | + if ((string)$expectedSize !== (string)$actualSize) { |
|
| 111 | + throw new BadRequest("Chunks on server do not sum up to $expectedSize but to $actualSize bytes"); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | 114 | } |
@@ -30,30 +30,30 @@ |
||
| 30 | 30 | * Delete the admin|personal sections and settings tables |
| 31 | 31 | */ |
| 32 | 32 | class Version14000Date20180129121024 extends SimpleMigrationStep { |
| 33 | - public function name(): string { |
|
| 34 | - return 'Drop obsolete settings tables'; |
|
| 35 | - } |
|
| 33 | + public function name(): string { |
|
| 34 | + return 'Drop obsolete settings tables'; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - public function description(): string { |
|
| 38 | - return 'Drops the following obsolete tables: "admin_sections", "admin_settings", "personal_sections" and "personal_settings"'; |
|
| 39 | - } |
|
| 37 | + public function description(): string { |
|
| 38 | + return 'Drops the following obsolete tables: "admin_sections", "admin_settings", "personal_sections" and "personal_settings"'; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * @param IOutput $output |
|
| 43 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 44 | - * @param array $options |
|
| 45 | - * @return null|ISchemaWrapper |
|
| 46 | - * @since 13.0.0 |
|
| 47 | - */ |
|
| 48 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 49 | - /** @var ISchemaWrapper $schema */ |
|
| 50 | - $schema = $schemaClosure(); |
|
| 41 | + /** |
|
| 42 | + * @param IOutput $output |
|
| 43 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
| 44 | + * @param array $options |
|
| 45 | + * @return null|ISchemaWrapper |
|
| 46 | + * @since 13.0.0 |
|
| 47 | + */ |
|
| 48 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 49 | + /** @var ISchemaWrapper $schema */ |
|
| 50 | + $schema = $schemaClosure(); |
|
| 51 | 51 | |
| 52 | - $schema->dropTable('admin_sections'); |
|
| 53 | - $schema->dropTable('admin_settings'); |
|
| 54 | - $schema->dropTable('personal_sections'); |
|
| 55 | - $schema->dropTable('personal_settings'); |
|
| 52 | + $schema->dropTable('admin_sections'); |
|
| 53 | + $schema->dropTable('admin_settings'); |
|
| 54 | + $schema->dropTable('personal_sections'); |
|
| 55 | + $schema->dropTable('personal_settings'); |
|
| 56 | 56 | |
| 57 | - return $schema; |
|
| 58 | - } |
|
| 57 | + return $schema; |
|
| 58 | + } |
|
| 59 | 59 | } |
@@ -62,7 +62,7 @@ |
||
| 62 | 62 | */ |
| 63 | 63 | public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
| 64 | 64 | if (isset($this->collections[$name])) { |
| 65 | - throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
| 65 | + throw new \OutOfBoundsException('Duplicate entity name "'.$name.'"'); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | $this->collections[$name] = $entityExistsFunction; |
@@ -17,45 +17,45 @@ |
||
| 17 | 17 | * @since 28.0.0 Dispatched as a typed event |
| 18 | 18 | */ |
| 19 | 19 | class SystemTagsEntityEvent extends Event { |
| 20 | - /** |
|
| 21 | - * @since 9.1.0 |
|
| 22 | - * @deprecated 22.0.0 Listen to the typed event instead |
|
| 23 | - */ |
|
| 24 | - public const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity'; |
|
| 25 | - |
|
| 26 | - /** @var \Closure[] */ |
|
| 27 | - protected $collections; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * @since 9.1.0 |
|
| 31 | - */ |
|
| 32 | - public function __construct() { |
|
| 33 | - parent::__construct(); |
|
| 34 | - $this->collections = []; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param string $name |
|
| 39 | - * @param \Closure $entityExistsFunction The closure should take one |
|
| 40 | - * argument, which is the id of the entity, that tags |
|
| 41 | - * should be handled for. The return should then be bool, |
|
| 42 | - * depending on whether tags are allowed (true) or not. |
|
| 43 | - * @throws \OutOfBoundsException when the entity name is already taken |
|
| 44 | - * @since 9.1.0 |
|
| 45 | - */ |
|
| 46 | - public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
|
| 47 | - if (isset($this->collections[$name])) { |
|
| 48 | - throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - $this->collections[$name] = $entityExistsFunction; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return \Closure[] |
|
| 56 | - * @since 9.1.0 |
|
| 57 | - */ |
|
| 58 | - public function getEntityCollections(): array { |
|
| 59 | - return $this->collections; |
|
| 60 | - } |
|
| 20 | + /** |
|
| 21 | + * @since 9.1.0 |
|
| 22 | + * @deprecated 22.0.0 Listen to the typed event instead |
|
| 23 | + */ |
|
| 24 | + public const EVENT_ENTITY = 'OCP\SystemTag\ISystemTagManager::registerEntity'; |
|
| 25 | + |
|
| 26 | + /** @var \Closure[] */ |
|
| 27 | + protected $collections; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * @since 9.1.0 |
|
| 31 | + */ |
|
| 32 | + public function __construct() { |
|
| 33 | + parent::__construct(); |
|
| 34 | + $this->collections = []; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param string $name |
|
| 39 | + * @param \Closure $entityExistsFunction The closure should take one |
|
| 40 | + * argument, which is the id of the entity, that tags |
|
| 41 | + * should be handled for. The return should then be bool, |
|
| 42 | + * depending on whether tags are allowed (true) or not. |
|
| 43 | + * @throws \OutOfBoundsException when the entity name is already taken |
|
| 44 | + * @since 9.1.0 |
|
| 45 | + */ |
|
| 46 | + public function addEntityCollection(string $name, \Closure $entityExistsFunction) { |
|
| 47 | + if (isset($this->collections[$name])) { |
|
| 48 | + throw new \OutOfBoundsException('Duplicate entity name "' . $name . '"'); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + $this->collections[$name] = $entityExistsFunction; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return \Closure[] |
|
| 56 | + * @since 9.1.0 |
|
| 57 | + */ |
|
| 58 | + public function getEntityCollections(): array { |
|
| 59 | + return $this->collections; |
|
| 60 | + } |
|
| 61 | 61 | } |
@@ -67,6 +67,6 @@ |
||
| 67 | 67 | $implements |= Backend::COUNT_USERS; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - return (bool)($actions & $implements); |
|
| 70 | + return (bool) ($actions & $implements); |
|
| 71 | 71 | } |
| 72 | 72 | } |
@@ -34,41 +34,41 @@ |
||
| 34 | 34 | * @since 14.0.0 |
| 35 | 35 | */ |
| 36 | 36 | abstract class ABackend implements IUserBackend, UserInterface { |
| 37 | - /** |
|
| 38 | - * @deprecated 14.0.0 |
|
| 39 | - * @since 14.0.0 |
|
| 40 | - * |
|
| 41 | - * @param int $actions The action to check for |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - public function implementsActions($actions): bool { |
|
| 45 | - $implements = 0; |
|
| 37 | + /** |
|
| 38 | + * @deprecated 14.0.0 |
|
| 39 | + * @since 14.0.0 |
|
| 40 | + * |
|
| 41 | + * @param int $actions The action to check for |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + public function implementsActions($actions): bool { |
|
| 45 | + $implements = 0; |
|
| 46 | 46 | |
| 47 | - if ($this instanceof ICreateUserBackend) { |
|
| 48 | - $implements |= Backend::CREATE_USER; |
|
| 49 | - } |
|
| 50 | - if ($this instanceof ISetPasswordBackend) { |
|
| 51 | - $implements |= Backend::SET_PASSWORD; |
|
| 52 | - } |
|
| 53 | - if ($this instanceof ICheckPasswordBackend) { |
|
| 54 | - $implements |= Backend::CHECK_PASSWORD; |
|
| 55 | - } |
|
| 56 | - if ($this instanceof IGetHomeBackend) { |
|
| 57 | - $implements |= Backend::GET_HOME; |
|
| 58 | - } |
|
| 59 | - if ($this instanceof IGetDisplayNameBackend) { |
|
| 60 | - $implements |= Backend::GET_DISPLAYNAME; |
|
| 61 | - } |
|
| 62 | - if ($this instanceof ISetDisplayNameBackend) { |
|
| 63 | - $implements |= Backend::SET_DISPLAYNAME; |
|
| 64 | - } |
|
| 65 | - if ($this instanceof IProvideAvatarBackend) { |
|
| 66 | - $implements |= Backend::PROVIDE_AVATAR; |
|
| 67 | - } |
|
| 68 | - if ($this instanceof ICountUsersBackend) { |
|
| 69 | - $implements |= Backend::COUNT_USERS; |
|
| 70 | - } |
|
| 47 | + if ($this instanceof ICreateUserBackend) { |
|
| 48 | + $implements |= Backend::CREATE_USER; |
|
| 49 | + } |
|
| 50 | + if ($this instanceof ISetPasswordBackend) { |
|
| 51 | + $implements |= Backend::SET_PASSWORD; |
|
| 52 | + } |
|
| 53 | + if ($this instanceof ICheckPasswordBackend) { |
|
| 54 | + $implements |= Backend::CHECK_PASSWORD; |
|
| 55 | + } |
|
| 56 | + if ($this instanceof IGetHomeBackend) { |
|
| 57 | + $implements |= Backend::GET_HOME; |
|
| 58 | + } |
|
| 59 | + if ($this instanceof IGetDisplayNameBackend) { |
|
| 60 | + $implements |= Backend::GET_DISPLAYNAME; |
|
| 61 | + } |
|
| 62 | + if ($this instanceof ISetDisplayNameBackend) { |
|
| 63 | + $implements |= Backend::SET_DISPLAYNAME; |
|
| 64 | + } |
|
| 65 | + if ($this instanceof IProvideAvatarBackend) { |
|
| 66 | + $implements |= Backend::PROVIDE_AVATAR; |
|
| 67 | + } |
|
| 68 | + if ($this instanceof ICountUsersBackend) { |
|
| 69 | + $implements |= Backend::COUNT_USERS; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return (bool)($actions & $implements); |
|
| 73 | - } |
|
| 72 | + return (bool)($actions & $implements); |
|
| 73 | + } |
|
| 74 | 74 | } |
@@ -25,10 +25,10 @@ |
||
| 25 | 25 | use OCP\IL10N; |
| 26 | 26 | |
| 27 | 27 | class KerberosAuth extends AuthMechanism { |
| 28 | - public function __construct(IL10N $l) { |
|
| 29 | - $this |
|
| 30 | - ->setIdentifier('smb::kerberos') |
|
| 31 | - ->setScheme(self::SCHEME_SMB) |
|
| 32 | - ->setText($l->t('Kerberos ticket')); |
|
| 33 | - } |
|
| 28 | + public function __construct(IL10N $l) { |
|
| 29 | + $this |
|
| 30 | + ->setIdentifier('smb::kerberos') |
|
| 31 | + ->setScheme(self::SCHEME_SMB) |
|
| 32 | + ->setText($l->t('Kerberos ticket')); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -129,7 +129,7 @@ |
||
| 129 | 129 | $data = $result->fetchAll(); |
| 130 | 130 | $result->closeCursor(); |
| 131 | 131 | |
| 132 | - $entities = array_map(function ($row) { |
|
| 132 | + $entities = array_map(function($row) { |
|
| 133 | 133 | return PublicKeyToken::fromRow($row); |
| 134 | 134 | }, $data); |
| 135 | 135 | |
@@ -18,235 +18,235 @@ |
||
| 18 | 18 | * @template-extends QBMapper<PublicKeyToken> |
| 19 | 19 | */ |
| 20 | 20 | class PublicKeyTokenMapper extends QBMapper { |
| 21 | - public function __construct(IDBConnection $db) { |
|
| 22 | - parent::__construct($db, 'authtoken'); |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Invalidate (delete) a given token |
|
| 27 | - */ |
|
| 28 | - public function invalidate(string $token) { |
|
| 29 | - /* @var $qb IQueryBuilder */ |
|
| 30 | - $qb = $this->db->getQueryBuilder(); |
|
| 31 | - $qb->delete($this->tableName) |
|
| 32 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 33 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 34 | - ->executeStatement(); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param int $olderThan |
|
| 39 | - * @param int $type |
|
| 40 | - * @param int|null $remember |
|
| 41 | - */ |
|
| 42 | - public function invalidateOld(int $olderThan, int $type = IToken::TEMPORARY_TOKEN, ?int $remember = null) { |
|
| 43 | - /* @var $qb IQueryBuilder */ |
|
| 44 | - $qb = $this->db->getQueryBuilder(); |
|
| 45 | - $delete = $qb->delete($this->tableName) |
|
| 46 | - ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
| 47 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
| 48 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 49 | - if ($remember !== null) { |
|
| 50 | - $delete->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))); |
|
| 51 | - } |
|
| 52 | - $delete->executeStatement(); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function invalidateLastUsedBefore(string $uid, int $before): int { |
|
| 56 | - $qb = $this->db->getQueryBuilder(); |
|
| 57 | - $qb->delete($this->tableName) |
|
| 58 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 59 | - ->andWhere($qb->expr()->lt('last_activity', $qb->createNamedParameter($before, IQueryBuilder::PARAM_INT))) |
|
| 60 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 61 | - return $qb->executeStatement(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Get the user UID for the given token |
|
| 66 | - * |
|
| 67 | - * @throws DoesNotExistException |
|
| 68 | - */ |
|
| 69 | - public function getToken(string $token): PublicKeyToken { |
|
| 70 | - /* @var $qb IQueryBuilder */ |
|
| 71 | - $qb = $this->db->getQueryBuilder(); |
|
| 72 | - $result = $qb->select('*') |
|
| 73 | - ->from($this->tableName) |
|
| 74 | - ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 75 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 76 | - ->executeQuery(); |
|
| 77 | - |
|
| 78 | - $data = $result->fetch(); |
|
| 79 | - $result->closeCursor(); |
|
| 80 | - if ($data === false) { |
|
| 81 | - throw new DoesNotExistException('token does not exist'); |
|
| 82 | - } |
|
| 83 | - return PublicKeyToken::fromRow($data); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the token for $id |
|
| 88 | - * |
|
| 89 | - * @throws DoesNotExistException |
|
| 90 | - */ |
|
| 91 | - public function getTokenById(int $id): PublicKeyToken { |
|
| 92 | - /* @var $qb IQueryBuilder */ |
|
| 93 | - $qb = $this->db->getQueryBuilder(); |
|
| 94 | - $result = $qb->select('*') |
|
| 95 | - ->from($this->tableName) |
|
| 96 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 97 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 98 | - ->executeQuery(); |
|
| 99 | - |
|
| 100 | - $data = $result->fetch(); |
|
| 101 | - $result->closeCursor(); |
|
| 102 | - if ($data === false) { |
|
| 103 | - throw new DoesNotExistException('token does not exist'); |
|
| 104 | - } |
|
| 105 | - return PublicKeyToken::fromRow($data); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get all tokens of a user |
|
| 110 | - * |
|
| 111 | - * The provider may limit the number of result rows in case of an abuse |
|
| 112 | - * where a high number of (session) tokens is generated |
|
| 113 | - * |
|
| 114 | - * @param string $uid |
|
| 115 | - * @return PublicKeyToken[] |
|
| 116 | - */ |
|
| 117 | - public function getTokenByUser(string $uid): array { |
|
| 118 | - /* @var $qb IQueryBuilder */ |
|
| 119 | - $qb = $this->db->getQueryBuilder(); |
|
| 120 | - $qb->select('*') |
|
| 121 | - ->from($this->tableName) |
|
| 122 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 123 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 124 | - ->setMaxResults(1000); |
|
| 125 | - $result = $qb->executeQuery(); |
|
| 126 | - $data = $result->fetchAll(); |
|
| 127 | - $result->closeCursor(); |
|
| 128 | - |
|
| 129 | - $entities = array_map(function ($row) { |
|
| 130 | - return PublicKeyToken::fromRow($row); |
|
| 131 | - }, $data); |
|
| 132 | - |
|
| 133 | - return $entities; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - public function getTokenByUserAndId(string $uid, int $id): ?string { |
|
| 137 | - /* @var $qb IQueryBuilder */ |
|
| 138 | - $qb = $this->db->getQueryBuilder(); |
|
| 139 | - $qb->select('token') |
|
| 140 | - ->from($this->tableName) |
|
| 141 | - ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 142 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 143 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 144 | - return $qb->executeQuery()->fetchOne() ?: null; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * delete all auth token which belong to a specific client if the client was deleted |
|
| 149 | - * |
|
| 150 | - * @param string $name |
|
| 151 | - */ |
|
| 152 | - public function deleteByName(string $name) { |
|
| 153 | - $qb = $this->db->getQueryBuilder(); |
|
| 154 | - $qb->delete($this->tableName) |
|
| 155 | - ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
|
| 156 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 157 | - $qb->executeStatement(); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - public function deleteTempToken(PublicKeyToken $except) { |
|
| 161 | - $qb = $this->db->getQueryBuilder(); |
|
| 162 | - |
|
| 163 | - $qb->delete($this->tableName) |
|
| 164 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($except->getUID()))) |
|
| 165 | - ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN))) |
|
| 166 | - ->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($except->getId()))) |
|
| 167 | - ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 168 | - |
|
| 169 | - $qb->executeStatement(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - public function hasExpiredTokens(string $uid): bool { |
|
| 173 | - $qb = $this->db->getQueryBuilder(); |
|
| 174 | - $qb->select('*') |
|
| 175 | - ->from($this->tableName) |
|
| 176 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 177 | - ->andWhere($qb->expr()->eq('password_invalid', $qb->createNamedParameter(true), IQueryBuilder::PARAM_BOOL)) |
|
| 178 | - ->setMaxResults(1); |
|
| 179 | - |
|
| 180 | - $cursor = $qb->executeQuery(); |
|
| 181 | - $data = $cursor->fetchAll(); |
|
| 182 | - $cursor->closeCursor(); |
|
| 183 | - |
|
| 184 | - return count($data) === 1; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Update the last activity timestamp |
|
| 189 | - * |
|
| 190 | - * In highly concurrent setups it can happen that two parallel processes |
|
| 191 | - * trigger the update at (nearly) the same time. In that special case it's |
|
| 192 | - * not necessary to hit the database with two actual updates. Therefore the |
|
| 193 | - * target last activity is included in the WHERE clause with a few seconds |
|
| 194 | - * of tolerance. |
|
| 195 | - * |
|
| 196 | - * Example: |
|
| 197 | - * - process 1 (P1) reads the token at timestamp 1500 |
|
| 198 | - * - process 1 (P2) reads the token at timestamp 1501 |
|
| 199 | - * - activity update interval is 100 |
|
| 200 | - * |
|
| 201 | - * This means |
|
| 202 | - * |
|
| 203 | - * - P1 will see a last_activity smaller than the current time and update |
|
| 204 | - * the token row |
|
| 205 | - * - If P2 reads after P1 had written, it will see 1600 as last activity |
|
| 206 | - * and the comparison on last_activity won't be truthy. This means no rows |
|
| 207 | - * need to be updated a second time |
|
| 208 | - * - If P2 reads before P1 had written, it will see 1501 as last activity, |
|
| 209 | - * but the comparison on last_activity will still not be truthy and the |
|
| 210 | - * token row is not updated a second time |
|
| 211 | - * |
|
| 212 | - * @param IToken $token |
|
| 213 | - * @param int $now |
|
| 214 | - */ |
|
| 215 | - public function updateActivity(IToken $token, int $now): void { |
|
| 216 | - $qb = $this->db->getQueryBuilder(); |
|
| 217 | - $update = $qb->update($this->getTableName()) |
|
| 218 | - ->set('last_activity', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)) |
|
| 219 | - ->where( |
|
| 220 | - $qb->expr()->eq('id', $qb->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 221 | - $qb->expr()->lt('last_activity', $qb->createNamedParameter($now - 15, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT) |
|
| 222 | - ); |
|
| 223 | - $update->executeStatement(); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - public function updateHashesForUser(string $userId, string $passwordHash): void { |
|
| 227 | - $qb = $this->db->getQueryBuilder(); |
|
| 228 | - $update = $qb->update($this->getTableName()) |
|
| 229 | - ->set('password_hash', $qb->createNamedParameter($passwordHash)) |
|
| 230 | - ->where( |
|
| 231 | - $qb->expr()->eq('uid', $qb->createNamedParameter($userId)) |
|
| 232 | - ); |
|
| 233 | - $update->executeStatement(); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - public function getFirstTokenForUser(string $userId): ?PublicKeyToken { |
|
| 237 | - $qb = $this->db->getQueryBuilder(); |
|
| 238 | - $qb->select('*') |
|
| 239 | - ->from($this->getTableName()) |
|
| 240 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($userId))) |
|
| 241 | - ->setMaxResults(1) |
|
| 242 | - ->orderBy('id'); |
|
| 243 | - $result = $qb->executeQuery(); |
|
| 244 | - |
|
| 245 | - $data = $result->fetch(); |
|
| 246 | - $result->closeCursor(); |
|
| 247 | - if ($data === false) { |
|
| 248 | - return null; |
|
| 249 | - } |
|
| 250 | - return PublicKeyToken::fromRow($data); |
|
| 251 | - } |
|
| 21 | + public function __construct(IDBConnection $db) { |
|
| 22 | + parent::__construct($db, 'authtoken'); |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Invalidate (delete) a given token |
|
| 27 | + */ |
|
| 28 | + public function invalidate(string $token) { |
|
| 29 | + /* @var $qb IQueryBuilder */ |
|
| 30 | + $qb = $this->db->getQueryBuilder(); |
|
| 31 | + $qb->delete($this->tableName) |
|
| 32 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 33 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 34 | + ->executeStatement(); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param int $olderThan |
|
| 39 | + * @param int $type |
|
| 40 | + * @param int|null $remember |
|
| 41 | + */ |
|
| 42 | + public function invalidateOld(int $olderThan, int $type = IToken::TEMPORARY_TOKEN, ?int $remember = null) { |
|
| 43 | + /* @var $qb IQueryBuilder */ |
|
| 44 | + $qb = $this->db->getQueryBuilder(); |
|
| 45 | + $delete = $qb->delete($this->tableName) |
|
| 46 | + ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
| 47 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter($type, IQueryBuilder::PARAM_INT))) |
|
| 48 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 49 | + if ($remember !== null) { |
|
| 50 | + $delete->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT))); |
|
| 51 | + } |
|
| 52 | + $delete->executeStatement(); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function invalidateLastUsedBefore(string $uid, int $before): int { |
|
| 56 | + $qb = $this->db->getQueryBuilder(); |
|
| 57 | + $qb->delete($this->tableName) |
|
| 58 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 59 | + ->andWhere($qb->expr()->lt('last_activity', $qb->createNamedParameter($before, IQueryBuilder::PARAM_INT))) |
|
| 60 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 61 | + return $qb->executeStatement(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Get the user UID for the given token |
|
| 66 | + * |
|
| 67 | + * @throws DoesNotExistException |
|
| 68 | + */ |
|
| 69 | + public function getToken(string $token): PublicKeyToken { |
|
| 70 | + /* @var $qb IQueryBuilder */ |
|
| 71 | + $qb = $this->db->getQueryBuilder(); |
|
| 72 | + $result = $qb->select('*') |
|
| 73 | + ->from($this->tableName) |
|
| 74 | + ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))) |
|
| 75 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 76 | + ->executeQuery(); |
|
| 77 | + |
|
| 78 | + $data = $result->fetch(); |
|
| 79 | + $result->closeCursor(); |
|
| 80 | + if ($data === false) { |
|
| 81 | + throw new DoesNotExistException('token does not exist'); |
|
| 82 | + } |
|
| 83 | + return PublicKeyToken::fromRow($data); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the token for $id |
|
| 88 | + * |
|
| 89 | + * @throws DoesNotExistException |
|
| 90 | + */ |
|
| 91 | + public function getTokenById(int $id): PublicKeyToken { |
|
| 92 | + /* @var $qb IQueryBuilder */ |
|
| 93 | + $qb = $this->db->getQueryBuilder(); |
|
| 94 | + $result = $qb->select('*') |
|
| 95 | + ->from($this->tableName) |
|
| 96 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 97 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 98 | + ->executeQuery(); |
|
| 99 | + |
|
| 100 | + $data = $result->fetch(); |
|
| 101 | + $result->closeCursor(); |
|
| 102 | + if ($data === false) { |
|
| 103 | + throw new DoesNotExistException('token does not exist'); |
|
| 104 | + } |
|
| 105 | + return PublicKeyToken::fromRow($data); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get all tokens of a user |
|
| 110 | + * |
|
| 111 | + * The provider may limit the number of result rows in case of an abuse |
|
| 112 | + * where a high number of (session) tokens is generated |
|
| 113 | + * |
|
| 114 | + * @param string $uid |
|
| 115 | + * @return PublicKeyToken[] |
|
| 116 | + */ |
|
| 117 | + public function getTokenByUser(string $uid): array { |
|
| 118 | + /* @var $qb IQueryBuilder */ |
|
| 119 | + $qb = $this->db->getQueryBuilder(); |
|
| 120 | + $qb->select('*') |
|
| 121 | + ->from($this->tableName) |
|
| 122 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 123 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))) |
|
| 124 | + ->setMaxResults(1000); |
|
| 125 | + $result = $qb->executeQuery(); |
|
| 126 | + $data = $result->fetchAll(); |
|
| 127 | + $result->closeCursor(); |
|
| 128 | + |
|
| 129 | + $entities = array_map(function ($row) { |
|
| 130 | + return PublicKeyToken::fromRow($row); |
|
| 131 | + }, $data); |
|
| 132 | + |
|
| 133 | + return $entities; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + public function getTokenByUserAndId(string $uid, int $id): ?string { |
|
| 137 | + /* @var $qb IQueryBuilder */ |
|
| 138 | + $qb = $this->db->getQueryBuilder(); |
|
| 139 | + $qb->select('token') |
|
| 140 | + ->from($this->tableName) |
|
| 141 | + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))) |
|
| 142 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 143 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 144 | + return $qb->executeQuery()->fetchOne() ?: null; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * delete all auth token which belong to a specific client if the client was deleted |
|
| 149 | + * |
|
| 150 | + * @param string $name |
|
| 151 | + */ |
|
| 152 | + public function deleteByName(string $name) { |
|
| 153 | + $qb = $this->db->getQueryBuilder(); |
|
| 154 | + $qb->delete($this->tableName) |
|
| 155 | + ->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR)) |
|
| 156 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 157 | + $qb->executeStatement(); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + public function deleteTempToken(PublicKeyToken $except) { |
|
| 161 | + $qb = $this->db->getQueryBuilder(); |
|
| 162 | + |
|
| 163 | + $qb->delete($this->tableName) |
|
| 164 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($except->getUID()))) |
|
| 165 | + ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN))) |
|
| 166 | + ->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($except->getId()))) |
|
| 167 | + ->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT))); |
|
| 168 | + |
|
| 169 | + $qb->executeStatement(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + public function hasExpiredTokens(string $uid): bool { |
|
| 173 | + $qb = $this->db->getQueryBuilder(); |
|
| 174 | + $qb->select('*') |
|
| 175 | + ->from($this->tableName) |
|
| 176 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 177 | + ->andWhere($qb->expr()->eq('password_invalid', $qb->createNamedParameter(true), IQueryBuilder::PARAM_BOOL)) |
|
| 178 | + ->setMaxResults(1); |
|
| 179 | + |
|
| 180 | + $cursor = $qb->executeQuery(); |
|
| 181 | + $data = $cursor->fetchAll(); |
|
| 182 | + $cursor->closeCursor(); |
|
| 183 | + |
|
| 184 | + return count($data) === 1; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Update the last activity timestamp |
|
| 189 | + * |
|
| 190 | + * In highly concurrent setups it can happen that two parallel processes |
|
| 191 | + * trigger the update at (nearly) the same time. In that special case it's |
|
| 192 | + * not necessary to hit the database with two actual updates. Therefore the |
|
| 193 | + * target last activity is included in the WHERE clause with a few seconds |
|
| 194 | + * of tolerance. |
|
| 195 | + * |
|
| 196 | + * Example: |
|
| 197 | + * - process 1 (P1) reads the token at timestamp 1500 |
|
| 198 | + * - process 1 (P2) reads the token at timestamp 1501 |
|
| 199 | + * - activity update interval is 100 |
|
| 200 | + * |
|
| 201 | + * This means |
|
| 202 | + * |
|
| 203 | + * - P1 will see a last_activity smaller than the current time and update |
|
| 204 | + * the token row |
|
| 205 | + * - If P2 reads after P1 had written, it will see 1600 as last activity |
|
| 206 | + * and the comparison on last_activity won't be truthy. This means no rows |
|
| 207 | + * need to be updated a second time |
|
| 208 | + * - If P2 reads before P1 had written, it will see 1501 as last activity, |
|
| 209 | + * but the comparison on last_activity will still not be truthy and the |
|
| 210 | + * token row is not updated a second time |
|
| 211 | + * |
|
| 212 | + * @param IToken $token |
|
| 213 | + * @param int $now |
|
| 214 | + */ |
|
| 215 | + public function updateActivity(IToken $token, int $now): void { |
|
| 216 | + $qb = $this->db->getQueryBuilder(); |
|
| 217 | + $update = $qb->update($this->getTableName()) |
|
| 218 | + ->set('last_activity', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)) |
|
| 219 | + ->where( |
|
| 220 | + $qb->expr()->eq('id', $qb->createNamedParameter($token->getId(), IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), |
|
| 221 | + $qb->expr()->lt('last_activity', $qb->createNamedParameter($now - 15, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT) |
|
| 222 | + ); |
|
| 223 | + $update->executeStatement(); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + public function updateHashesForUser(string $userId, string $passwordHash): void { |
|
| 227 | + $qb = $this->db->getQueryBuilder(); |
|
| 228 | + $update = $qb->update($this->getTableName()) |
|
| 229 | + ->set('password_hash', $qb->createNamedParameter($passwordHash)) |
|
| 230 | + ->where( |
|
| 231 | + $qb->expr()->eq('uid', $qb->createNamedParameter($userId)) |
|
| 232 | + ); |
|
| 233 | + $update->executeStatement(); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + public function getFirstTokenForUser(string $userId): ?PublicKeyToken { |
|
| 237 | + $qb = $this->db->getQueryBuilder(); |
|
| 238 | + $qb->select('*') |
|
| 239 | + ->from($this->getTableName()) |
|
| 240 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($userId))) |
|
| 241 | + ->setMaxResults(1) |
|
| 242 | + ->orderBy('id'); |
|
| 243 | + $result = $qb->executeQuery(); |
|
| 244 | + |
|
| 245 | + $data = $result->fetch(); |
|
| 246 | + $result->closeCursor(); |
|
| 247 | + if ($data === false) { |
|
| 248 | + return null; |
|
| 249 | + } |
|
| 250 | + return PublicKeyToken::fromRow($data); |
|
| 251 | + } |
|
| 252 | 252 | } |
@@ -107,7 +107,7 @@ |
||
| 107 | 107 | * @return string language |
| 108 | 108 | * @since 7.0.0 |
| 109 | 109 | */ |
| 110 | - public function getLanguageCode(): string ; |
|
| 110 | + public function getLanguageCode(): string; |
|
| 111 | 111 | |
| 112 | 112 | /** |
| 113 | 113 | * * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object |
@@ -17,77 +17,77 @@ |
||
| 17 | 17 | * @since 6.0.0 |
| 18 | 18 | */ |
| 19 | 19 | interface IL10N { |
| 20 | - /** |
|
| 21 | - * Translating |
|
| 22 | - * @param string $text The text we need a translation for |
|
| 23 | - * @param array|string $parameters default:array() Parameters for sprintf |
|
| 24 | - * @return string Translation or the same text |
|
| 25 | - * |
|
| 26 | - * Returns the translation. If no translation is found, $text will be |
|
| 27 | - * returned. |
|
| 28 | - * @since 6.0.0 |
|
| 29 | - */ |
|
| 30 | - public function t(string $text, $parameters = []): string; |
|
| 20 | + /** |
|
| 21 | + * Translating |
|
| 22 | + * @param string $text The text we need a translation for |
|
| 23 | + * @param array|string $parameters default:array() Parameters for sprintf |
|
| 24 | + * @return string Translation or the same text |
|
| 25 | + * |
|
| 26 | + * Returns the translation. If no translation is found, $text will be |
|
| 27 | + * returned. |
|
| 28 | + * @since 6.0.0 |
|
| 29 | + */ |
|
| 30 | + public function t(string $text, $parameters = []): string; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Translating |
|
| 34 | - * @param string $text_singular the string to translate for exactly one object |
|
| 35 | - * @param string $text_plural the string to translate for n objects |
|
| 36 | - * @param integer $count Number of objects |
|
| 37 | - * @param array $parameters default:array() Parameters for sprintf |
|
| 38 | - * @return string Translation or the same text |
|
| 39 | - * |
|
| 40 | - * Returns the translation. If no translation is found, $text will be |
|
| 41 | - * returned. %n will be replaced with the number of objects. |
|
| 42 | - * |
|
| 43 | - * The correct plural is determined by the plural_forms-function |
|
| 44 | - * provided by the po file. |
|
| 45 | - * @since 6.0.0 |
|
| 46 | - * |
|
| 47 | - */ |
|
| 48 | - public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string; |
|
| 32 | + /** |
|
| 33 | + * Translating |
|
| 34 | + * @param string $text_singular the string to translate for exactly one object |
|
| 35 | + * @param string $text_plural the string to translate for n objects |
|
| 36 | + * @param integer $count Number of objects |
|
| 37 | + * @param array $parameters default:array() Parameters for sprintf |
|
| 38 | + * @return string Translation or the same text |
|
| 39 | + * |
|
| 40 | + * Returns the translation. If no translation is found, $text will be |
|
| 41 | + * returned. %n will be replaced with the number of objects. |
|
| 42 | + * |
|
| 43 | + * The correct plural is determined by the plural_forms-function |
|
| 44 | + * provided by the po file. |
|
| 45 | + * @since 6.0.0 |
|
| 46 | + * |
|
| 47 | + */ |
|
| 48 | + public function n(string $text_singular, string $text_plural, int $count, array $parameters = []): string; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Localization |
|
| 52 | - * @param string $type Type of localization |
|
| 53 | - * @param \DateTime|int|string $data parameters for this localization |
|
| 54 | - * @param array $options currently supports following options: |
|
| 55 | - * - 'width': handed into \Punic\Calendar::formatDate as second parameter |
|
| 56 | - * @return string|int|false |
|
| 57 | - * |
|
| 58 | - * Returns the localized data. |
|
| 59 | - * |
|
| 60 | - * Implemented types: |
|
| 61 | - * - date |
|
| 62 | - * - Creates a date |
|
| 63 | - * - l10n-field: date |
|
| 64 | - * - params: timestamp (int/string) |
|
| 65 | - * - datetime |
|
| 66 | - * - Creates date and time |
|
| 67 | - * - l10n-field: datetime |
|
| 68 | - * - params: timestamp (int/string) |
|
| 69 | - * - time |
|
| 70 | - * - Creates a time |
|
| 71 | - * - l10n-field: time |
|
| 72 | - * - params: timestamp (int/string) |
|
| 73 | - * @since 6.0.0 - parameter $options was added in 8.0.0 |
|
| 74 | - */ |
|
| 75 | - public function l(string $type, $data, array $options = []); |
|
| 50 | + /** |
|
| 51 | + * Localization |
|
| 52 | + * @param string $type Type of localization |
|
| 53 | + * @param \DateTime|int|string $data parameters for this localization |
|
| 54 | + * @param array $options currently supports following options: |
|
| 55 | + * - 'width': handed into \Punic\Calendar::formatDate as second parameter |
|
| 56 | + * @return string|int|false |
|
| 57 | + * |
|
| 58 | + * Returns the localized data. |
|
| 59 | + * |
|
| 60 | + * Implemented types: |
|
| 61 | + * - date |
|
| 62 | + * - Creates a date |
|
| 63 | + * - l10n-field: date |
|
| 64 | + * - params: timestamp (int/string) |
|
| 65 | + * - datetime |
|
| 66 | + * - Creates date and time |
|
| 67 | + * - l10n-field: datetime |
|
| 68 | + * - params: timestamp (int/string) |
|
| 69 | + * - time |
|
| 70 | + * - Creates a time |
|
| 71 | + * - l10n-field: time |
|
| 72 | + * - params: timestamp (int/string) |
|
| 73 | + * @since 6.0.0 - parameter $options was added in 8.0.0 |
|
| 74 | + */ |
|
| 75 | + public function l(string $type, $data, array $options = []); |
|
| 76 | 76 | |
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * The code (en, de, ...) of the language that is used for this IL10N object |
|
| 80 | - * |
|
| 81 | - * @return string language |
|
| 82 | - * @since 7.0.0 |
|
| 83 | - */ |
|
| 84 | - public function getLanguageCode(): string ; |
|
| 78 | + /** |
|
| 79 | + * The code (en, de, ...) of the language that is used for this IL10N object |
|
| 80 | + * |
|
| 81 | + * @return string language |
|
| 82 | + * @since 7.0.0 |
|
| 83 | + */ |
|
| 84 | + public function getLanguageCode(): string ; |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object |
|
| 88 | - * |
|
| 89 | - * @return string locale |
|
| 90 | - * @since 14.0.0 |
|
| 91 | - */ |
|
| 92 | - public function getLocaleCode(): string; |
|
| 86 | + /** |
|
| 87 | + * * The code (en_US, fr_CA, ...) of the locale that is used for this IL10N object |
|
| 88 | + * |
|
| 89 | + * @return string locale |
|
| 90 | + * @since 14.0.0 |
|
| 91 | + */ |
|
| 92 | + public function getLocaleCode(): string; |
|
| 93 | 93 | } |