From 1074bc5fb947dba90ffbd5eabd1f8a916f3c1e6d Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Wed, 7 Dec 2022 01:34:02 +0530 Subject: [PATCH 1/9] on demand migration of rainloop data --- lib/AppInfo/Application.php | 4 ++ .../UserAddedToBetaGroupListener.php | 65 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 lib/Listeners/UserAddedToBetaGroupListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 7b718d8b..18f2a35b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -35,6 +35,8 @@ use OCA\EcloudAccounts\Service\LDAPConnectionService; use OCP\User\Events\BeforeUserDeletedEvent; use OCP\User\Events\UserChangedEvent; use OCA\EcloudAccounts\Listeners\UserChangedListener; +use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; + class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; @@ -46,6 +48,8 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class); + $context->registerEventListener(UserAddedEvent::class, UserAddedToBetaGroupListener::class); + } public function boot(IBootContext $context): void { diff --git a/lib/Listeners/UserAddedToBetaGroupListener.php b/lib/Listeners/UserAddedToBetaGroupListener.php new file mode 100644 index 00000000..6be67bc1 --- /dev/null +++ b/lib/Listeners/UserAddedToBetaGroupListener.php @@ -0,0 +1,65 @@ +config = $config; + } + + public function handle(Event $event): void { + if (!($event instanceof UserAddedEvent)) { + return; + } + + $user = $event->getUser(); + $group = $event->getGroup(); + + $betaGroup = $this->config->getSystemValue("beta_group_name"); + + if ($group->GID() !== $betaGroup) return; + + $this->migrateRainloopData($user); + + } + + private function migrateRainloopData(IUser $user): void { + + $dir = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/'); + $dir_snappy = $dir . '/appdata_snappymail/'; + $dir_rainloop = $dir . '/rainloop-storage/_data_/_default_/storage/cfg/'; + $iterator = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($dir_rainloop, \RecursiveDirectoryIterator::SKIP_DOTS), + \RecursiveIteratorIterator::SELF_FIRST + ); + foreach ($iterator as $item) { + $target = $dir_snappy . $iterator->getSubPathname(); + if (\preg_match('@/plugins/([^/])@', $target, $match)) { + $rainloop_plugins[$match[1]] = $match[1]; + } else if (!\strpos($target, '/cache/')) { + if ($item->isDir()) { + \is_dir($target) || \mkdir($target, 0755, true); + } else if (\file_exists($target)) { + $result[] = "skipped: {$target}"; + } else { + \copy($item, $target); + $result[] = "copied : {$target}"; + } + } + } + } +} -- GitLab From 18eb99a844b4893322b5ad94ceedad98b12c68af Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Wed, 7 Dec 2022 18:33:00 +0530 Subject: [PATCH 2/9] fix lint --- lib/AppInfo/Application.php | 2 -- .../UserAddedToBetaGroupListener.php | 30 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 18f2a35b..a9c6a293 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -37,7 +37,6 @@ use OCP\User\Events\UserChangedEvent; use OCA\EcloudAccounts\Listeners\UserChangedListener; use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; - class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; @@ -49,7 +48,6 @@ class Application extends App implements IBootstrap { $context->registerEventListener(BeforeUserDeletedEvent::class, BeforeUserDeletedListener::class); $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class); $context->registerEventListener(UserAddedEvent::class, UserAddedToBetaGroupListener::class); - } public function boot(IBootContext $context): void { diff --git a/lib/Listeners/UserAddedToBetaGroupListener.php b/lib/Listeners/UserAddedToBetaGroupListener.php index 6be67bc1..6e9b00f5 100644 --- a/lib/Listeners/UserAddedToBetaGroupListener.php +++ b/lib/Listeners/UserAddedToBetaGroupListener.php @@ -10,15 +10,13 @@ use OCP\Group\Events\UserAddedEvent; use OCP\IUser; use OCP\IConfig; - class UserAddedToBetaGroupListener implements IEventListener { - private $config; public function __construct( - IConfig $config + IConfig $config ) { - $this->config = $config; + $this->config = $config; } public function handle(Event $event): void { @@ -29,19 +27,21 @@ class UserAddedToBetaGroupListener implements IEventListener { $user = $event->getUser(); $group = $event->getGroup(); - $betaGroup = $this->config->getSystemValue("beta_group_name"); + $betaGroup = $this->config->getSystemValue("beta_group_name"); - if ($group->GID() !== $betaGroup) return; + if ($group->GID() !== $betaGroup) { + return; + } - $this->migrateRainloopData($user); - + $this->migrateRainloopData($user); } - private function migrateRainloopData(IUser $user): void { - - $dir = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/'); + private function migrateRainloopData(IUser $user): void { + $username = $user->getUID(); + $dir_data = substr($username, 0, 2); + $dir = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/'); $dir_snappy = $dir . '/appdata_snappymail/'; - $dir_rainloop = $dir . '/rainloop-storage/_data_/_default_/storage/cfg/'; + $dir_rainloop = $dir . '/rainloop-storage/_data_/_default_/storage/cfg/' . $dir_data; $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($dir_rainloop, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST @@ -50,10 +50,10 @@ class UserAddedToBetaGroupListener implements IEventListener { $target = $dir_snappy . $iterator->getSubPathname(); if (\preg_match('@/plugins/([^/])@', $target, $match)) { $rainloop_plugins[$match[1]] = $match[1]; - } else if (!\strpos($target, '/cache/')) { + } elseif (!\strpos($target, '/cache/')) { if ($item->isDir()) { \is_dir($target) || \mkdir($target, 0755, true); - } else if (\file_exists($target)) { + } elseif (\file_exists($target)) { $result[] = "skipped: {$target}"; } else { \copy($item, $target); @@ -61,5 +61,5 @@ class UserAddedToBetaGroupListener implements IEventListener { } } } - } + } } -- GitLab From f32ec52cc6a0151bfe0323ddc818532d1d3460e7 Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Fri, 9 Dec 2022 02:55:53 +0530 Subject: [PATCH 3/9] add UserAddedEvent class --- lib/AppInfo/Application.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index a9c6a293..1b36462c 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -36,6 +36,8 @@ use OCP\User\Events\BeforeUserDeletedEvent; use OCP\User\Events\UserChangedEvent; use OCA\EcloudAccounts\Listeners\UserChangedListener; use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; +use OCP\Group\Events\UserAddedEvent; + class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; -- GitLab From e15660ba8d79eb40969511bf4603651a795684cc Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Fri, 9 Dec 2022 09:12:59 +0000 Subject: [PATCH 4/9] Update composer.json --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 01f45f58..4783bee6 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,8 @@ { "require-dev": { "nextcloud/coding-standard": "^1.0", - "friendsofphp/php-cs-fixer": "^3.13" + "friendsofphp/php-cs-fixer": "^3.13", + "php": "^8.1" }, "scripts": { "cs:check": "php-cs-fixer fix --dry-run --diff", -- GitLab From 32c59c75a961c7d6a6ade14d76c5a27d991d1850 Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Fri, 9 Dec 2022 09:19:46 +0000 Subject: [PATCH 5/9] Revert "Update composer.json" This reverts commit e15660ba8d79eb40969511bf4603651a795684cc --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4783bee6..01f45f58 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,7 @@ { "require-dev": { "nextcloud/coding-standard": "^1.0", - "friendsofphp/php-cs-fixer": "^3.13", - "php": "^8.1" + "friendsofphp/php-cs-fixer": "^3.13" }, "scripts": { "cs:check": "php-cs-fixer fix --dry-run --diff", -- GitLab From c6ee3f5a9e3dfee45c3613e0aa6cf5799111c032 Mon Sep 17 00:00:00 2001 From: Nivesh Date: Fri, 9 Dec 2022 15:55:38 +0530 Subject: [PATCH 6/9] lint fix --- lib/AppInfo/Application.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 1b36462c..84458246 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -38,7 +38,6 @@ use OCA\EcloudAccounts\Listeners\UserChangedListener; use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; use OCP\Group\Events\UserAddedEvent; - class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; -- GitLab From 26593fe8cad5f81baa44c7ad34a0b894ee24050f Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Wed, 14 Dec 2022 00:08:42 +0530 Subject: [PATCH 7/9] copy using shell --- lib/AppInfo/Application.php | 1 - .../UserAddedToBetaGroupListener.php | 40 +++++++------------ 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 1b36462c..84458246 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -38,7 +38,6 @@ use OCA\EcloudAccounts\Listeners\UserChangedListener; use OCA\EcloudAccounts\Listeners\UserAddedToBetaGroupListener; use OCP\Group\Events\UserAddedEvent; - class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; diff --git a/lib/Listeners/UserAddedToBetaGroupListener.php b/lib/Listeners/UserAddedToBetaGroupListener.php index 6e9b00f5..e6da26e1 100644 --- a/lib/Listeners/UserAddedToBetaGroupListener.php +++ b/lib/Listeners/UserAddedToBetaGroupListener.php @@ -13,9 +13,7 @@ use OCP\IConfig; class UserAddedToBetaGroupListener implements IEventListener { private $config; - public function __construct( - IConfig $config - ) { + public function __construct(IConfig $config) { $this->config = $config; } @@ -29,37 +27,27 @@ class UserAddedToBetaGroupListener implements IEventListener { $betaGroup = $this->config->getSystemValue("beta_group_name"); - if ($group->GID() !== $betaGroup) { + if ($group->getGID() !== $betaGroup) { return; } - + $this->migrateRainloopData($user); } private function migrateRainloopData(IUser $user): void { $username = $user->getUID(); $dir_data = substr($username, 0, 2); - $dir = \rtrim(\trim(\OC::$server->getSystemConfig()->getValue('datadirectory', '')), '\\/'); - $dir_snappy = $dir . '/appdata_snappymail/'; - $dir_rainloop = $dir . '/rainloop-storage/_data_/_default_/storage/cfg/' . $dir_data; - $iterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($dir_rainloop, \RecursiveDirectoryIterator::SKIP_DOTS), - \RecursiveIteratorIterator::SELF_FIRST - ); - foreach ($iterator as $item) { - $target = $dir_snappy . $iterator->getSubPathname(); - if (\preg_match('@/plugins/([^/])@', $target, $match)) { - $rainloop_plugins[$match[1]] = $match[1]; - } elseif (!\strpos($target, '/cache/')) { - if ($item->isDir()) { - \is_dir($target) || \mkdir($target, 0755, true); - } elseif (\file_exists($target)) { - $result[] = "skipped: {$target}"; - } else { - \copy($item, $target); - $result[] = "copied : {$target}"; - } - } + $email = $user->getEMailAddress(); + $dir = rtrim(trim($this->config->getSystemValue('datadirectory', '')), '\\/'); + $dir_snappy = $dir . '/appdata_snappymail/_data_/_default_/storage/cfg/' . $dir_data . "/$email/"; + $dir_rainloop = $dir . '/rainloop-storage/_data_/_default_/storage/cfg/' . $dir_data . "/$email"; + + if (file_exists($dir_snappy)) { + \OC::$server->getLogger()->debug("$dir_snappy folder already exists"); + return; } + mkdir($dir_snappy, 0755, true); + shell_exec("cp -ar $dir_rainloop/* $dir_snappy"); + return; } } -- GitLab From 50025c83ef3a91194d39be9cc0471fcde36728e7 Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Wed, 14 Dec 2022 22:31:55 +0530 Subject: [PATCH 8/9] use coding standards --- .../UserAddedToBetaGroupListener.php | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/Listeners/UserAddedToBetaGroupListener.php b/lib/Listeners/UserAddedToBetaGroupListener.php index e6da26e1..cb5b09ce 100644 --- a/lib/Listeners/UserAddedToBetaGroupListener.php +++ b/lib/Listeners/UserAddedToBetaGroupListener.php @@ -9,12 +9,15 @@ use OCP\EventDispatcher\IEventListener; use OCP\Group\Events\UserAddedEvent; use OCP\IUser; use OCP\IConfig; +use OCP\ILogger; class UserAddedToBetaGroupListener implements IEventListener { private $config; + private $logger; - public function __construct(IConfig $config) { + public function __construct(IConfig $config, ILogger $logger) { $this->config = $config; + $this->logger = $logger; } public function handle(Event $event): void { @@ -30,24 +33,28 @@ class UserAddedToBetaGroupListener implements IEventListener { if ($group->getGID() !== $betaGroup) { return; } - - $this->migrateRainloopData($user); + try { + $this->migrateRainloopData($user); + } catch (Exception $e) { + $errorMessage = 'Error while migrating user rainloop data to snappymail'; + $this->logger->error($errorMessage . ': ' . $e->getMessage()); + } } private function migrateRainloopData(IUser $user): void { $username = $user->getUID(); - $dir_data = substr($username, 0, 2); + $userDir = substr($username, 0, 2); $email = $user->getEMailAddress(); - $dir = rtrim(trim($this->config->getSystemValue('datadirectory', '')), '\\/'); - $dir_snappy = $dir . '/appdata_snappymail/_data_/_default_/storage/cfg/' . $dir_data . "/$email/"; - $dir_rainloop = $dir . '/rainloop-storage/_data_/_default_/storage/cfg/' . $dir_data . "/$email"; + $dataDir = rtrim(trim($this->config->getSystemValue('datadirectory', '')), '\\/'); + $snappyDir = "$dataDir/appdata_snappymail/_data_/_default_/storage/cfg/$userDir/$email/"; + $rainloopDir = "$dataDir/rainloop-storage/_data_/_default_/storage/cfg/$userDir/$email"; - if (file_exists($dir_snappy)) { - \OC::$server->getLogger()->debug("$dir_snappy folder already exists"); + if (file_exists($snappyDir)) { + $this->logger->debug("$snappyDir already exists"); return; } - mkdir($dir_snappy, 0755, true); - shell_exec("cp -ar $dir_rainloop/* $dir_snappy"); + mkdir($snappyDir, 0755, true); + shell_exec("cp -ar $rainloopDir/* $snappyDir"); return; } } -- GitLab From a9f09dadcd7806b5010d97594ffa3ad2bc5b5d88 Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Thu, 15 Dec 2022 00:47:38 +0530 Subject: [PATCH 9/9] throw exception when cp or mkdir fails --- lib/Listeners/UserAddedToBetaGroupListener.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/Listeners/UserAddedToBetaGroupListener.php b/lib/Listeners/UserAddedToBetaGroupListener.php index cb5b09ce..9801632d 100644 --- a/lib/Listeners/UserAddedToBetaGroupListener.php +++ b/lib/Listeners/UserAddedToBetaGroupListener.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Listeners; +use Exception; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\Group\Events\UserAddedEvent; @@ -53,8 +54,12 @@ class UserAddedToBetaGroupListener implements IEventListener { $this->logger->debug("$snappyDir already exists"); return; } - mkdir($snappyDir, 0755, true); - shell_exec("cp -ar $rainloopDir/* $snappyDir"); + if (!mkdir($snappyDir, 0755, true)) { + throw new Exception("Unable to mkdir $snappyDir"); + } + if (!shell_exec("cp -avr $rainloopDir/* $snappyDir")) { + throw new Exception("Unable to copy files from $rainloopDir to $snappyDir"); + } return; } } -- GitLab