diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 7b718d8bc3a49aa0b025db22302cb25e8b6dcc4a..844582461ea35a1204d800b62f3e8215d86d8dde 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; +use OCP\Group\Events\UserAddedEvent; class Application extends App implements IBootstrap { public const APP_ID = 'ecloud-accounts'; @@ -46,6 +48,7 @@ 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 0000000000000000000000000000000000000000..9801632d3a6566cbbb3052dd6ad3602689e33953 --- /dev/null +++ b/lib/Listeners/UserAddedToBetaGroupListener.php @@ -0,0 +1,65 @@ +config = $config; + $this->logger = $logger; + } + + 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->getGID() !== $betaGroup) { + return; + } + 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(); + $userDir = substr($username, 0, 2); + $email = $user->getEMailAddress(); + $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($snappyDir)) { + $this->logger->debug("$snappyDir already exists"); + return; + } + 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; + } +}