From 94c3557bdf0fb74e8f24dbc0029bdca402dc3e0d Mon Sep 17 00:00:00 2001 From: Florian RUEN Date: Tue, 9 Jul 2024 15:44:34 +0200 Subject: [PATCH] feat: add config to external storage to auto create user folder --- lib/Event/Listener/UserCreatedListener.php | 41 ++++++++++------------ lib/Service/BackendService.php | 4 +++ 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/lib/Event/Listener/UserCreatedListener.php b/lib/Event/Listener/UserCreatedListener.php index 000dfa5..ca88cae 100644 --- a/lib/Event/Listener/UserCreatedListener.php +++ b/lib/Event/Listener/UserCreatedListener.php @@ -48,36 +48,33 @@ public function handle(Event $event): void return; } - // get "Public Filecoin" external storage from config ID - // do this only if public gateway enabled - if ($this->config->getSystemValue('cidgravity_gateway')['is_public_gateway_enabled']) { - $externalStorageID = $this->config->getSystemValue('cidgravity_gateway')['public_external_storage_id']; - $externalStorage = $this->globalStoragesService->getStorage($externalStorageID); + // iterate over all external storages + // if config for external storage autoCreateUserFolder is true, create user folder on it + $externalStorages = $this->globalStoragesService->getStorages(); + foreach ($externalStorages as $externalStorage) { if ($externalStorage->getBackend()->getIdentifier() != "cidgravity") { $this->logger->error("CIDgravity: unable to create user folder on provided external storage: not an cidgravity external storage"); return; } - // get associated user and create the folder - $userUID = $event->getUser()->getUID(); - $externalStorageMountpoint = $externalStorage->getMountPoint(); - $userFolder = $this->rootFolder->getUserFolder($userUID); - $webDavPath = "/{$externalStorageMountpoint}/{$userUID}"; + if ($externalStorage->getBackendOption('auto_create_user_folder')) { + $userUID = $event->getUser()->getUID(); + $externalStorageMountpoint = $externalStorage->getMountPoint(); + $userFolder = $this->rootFolder->getUserFolder($userUID); + $webDavPath = "/{$externalStorageMountpoint}/{$userUID}"; - try { - if (!$userFolder->nodeExists($webDavPath)) { - $userFolder->newFolder($webDavPath); - $this->logger->debug("CIDgravity: User folder successfully created on provided external storage"); - } else { - $this->logger->debug("CIDgravity: unable to create user folder on provided external storage: already exists"); - } - } catch (\Exception $e) { - $this->logger->error("CIDgravity: unable to create user folder on provided external storage: " . $e->getMessage()); + try { + if (!$userFolder->nodeExists($webDavPath)) { + $userFolder->newFolder($webDavPath); + $this->logger->debug("CIDgravity: User folder successfully created on provided external storage"); + } else { + $this->logger->debug("CIDgravity: unable to create user folder on provided external storage: already exists"); + } + } catch (\Exception $e) { + $this->logger->error("CIDgravity: unable to create user folder on provided external storage: " . $e->getMessage()); + } } - - } else { - $this->logger->debug("CIDgravity: public gateway not enabled, will not try to create user folder on external storage"); } } } \ No newline at end of file diff --git a/lib/Service/BackendService.php b/lib/Service/BackendService.php index b479103..603eebc 100644 --- a/lib/Service/BackendService.php +++ b/lib/Service/BackendService.php @@ -48,6 +48,10 @@ public function __construct(IL10N $l, Password $legacyAuth) { ->setType(DefinitionParameter::VALUE_TEXT) ->setDefaultValue('https://ipfs.io/ipfs') ->setTooltip('You can also use your custom gateway or public gateway such as https://dweb.link'), + (new DefinitionParameter('auto_create_user_folder', $l->t('Auto create user folder'))) + ->setType(DefinitionParameter::VALUE_BOOLEAN) + ->setDefaultValue(false) + ->setTooltip('Auto create on folder on this external storage when a new user is created'), ]) ->addAuthScheme(AuthMechanism::SCHEME_PASSWORD) ->setLegacyAuthMechanism($legacyAuth);