]> source.dussan.org Git - nextcloud-server.git/commitdiff
Transfer to settings app and register
authorChristopher Ng <chrng8@gmail.com>
Fri, 4 Mar 2022 06:36:49 +0000 (06:36 +0000)
committerChristopher Ng <chrng8@gmail.com>
Fri, 18 Mar 2022 04:51:26 +0000 (04:51 +0000)
Signed-off-by: Christopher Ng <chrng8@gmail.com>
apps/settings/composer/composer/autoload_classmap.php
apps/settings/composer/composer/autoload_static.php
apps/settings/lib/AppInfo/Application.php
apps/settings/lib/UserMigration/AccountMigrator.php [new file with mode: 0644]
apps/settings/lib/UserMigration/AccountMigratorException.php [new file with mode: 0644]
lib/private/Accounts/AccountMigrator.php [deleted file]
lib/private/Accounts/AccountMigratorException.php [deleted file]

index 3d3729a66e5aa904e39cf3ad4ce5163a74ed45d3..bd092f3b4018249a351d06d0b1d71fd216988acf 100644 (file)
@@ -74,5 +74,7 @@ return array(
     'OCA\\Settings\\SetupChecks\\PhpDefaultCharset' => $baseDir . '/../lib/SetupChecks/PhpDefaultCharset.php',
     'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => $baseDir . '/../lib/SetupChecks/PhpOutputBuffering.php',
     'OCA\\Settings\\SetupChecks\\SupportedDatabase' => $baseDir . '/../lib/SetupChecks/SupportedDatabase.php',
+    'OCA\\Settings\\UserMigration\\AccountMigrator' => $baseDir . '/../lib/UserMigration/AccountMigrator.php',
+    'OCA\\Settings\\UserMigration\\AccountMigratorException' => $baseDir . '/../lib/UserMigration/AccountMigratorException.php',
     'OCA\\Settings\\WellKnown\\SecurityTxtHandler' => $baseDir . '/../lib/WellKnown/SecurityTxtHandler.php',
 );
index 7d00184dc7f5f1d32b0e50fe3616ef47b54c262c..b8aec66c25bc348b74bd2a1693d307da5c0f807f 100644 (file)
@@ -89,6 +89,8 @@ class ComposerStaticInitSettings
         'OCA\\Settings\\SetupChecks\\PhpDefaultCharset' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpDefaultCharset.php',
         'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpOutputBuffering.php',
         'OCA\\Settings\\SetupChecks\\SupportedDatabase' => __DIR__ . '/..' . '/../lib/SetupChecks/SupportedDatabase.php',
+        'OCA\\Settings\\UserMigration\\AccountMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigrator.php',
+        'OCA\\Settings\\UserMigration\\AccountMigratorException' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigratorException.php',
         'OCA\\Settings\\WellKnown\\SecurityTxtHandler' => __DIR__ . '/..' . '/../lib/WellKnown/SecurityTxtHandler.php',
     );
 
index 4810e78db2b7ec2f21ee955b62a82ef6233676ed..89a988621f21ed62845b52ef819a36677e9de03d 100644 (file)
@@ -46,6 +46,7 @@ use OCA\Settings\Mailer\NewUserMailHelper;
 use OCA\Settings\Middleware\SubadminMiddleware;
 use OCA\Settings\Search\AppSearch;
 use OCA\Settings\Search\SectionSearch;
+use OCA\Settings\UserMigration\AccountMigrator;
 use OCA\Settings\WellKnown\SecurityTxtHandler;
 use OCP\AppFramework\App;
 use OCP\AppFramework\Bootstrap\IBootContext;
@@ -131,6 +132,8 @@ class Application extends App implements IBootstrap {
                                Util::getDefaultEmailAddress('no-reply')
                        );
                });
+
+               $context->registerUserMigrator(AccountMigrator::class);
        }
 
        public function boot(IBootContext $context): void {
diff --git a/apps/settings/lib/UserMigration/AccountMigrator.php b/apps/settings/lib/UserMigration/AccountMigrator.php
new file mode 100644 (file)
index 0000000..5800324
--- /dev/null
@@ -0,0 +1,152 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2022 Christopher Ng <chrng8@gmail.com>
+ *
+ * @author Christopher Ng <chrng8@gmail.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Settings\UserMigration;
+
+use InvalidArgumentException;
+use OC\Accounts\AccountProperty;
+use OC\Accounts\AccountPropertyCollection;
+use OC\Accounts\TAccountsHelper;
+use OC\NotSquareException;
+use OCP\Accounts\IAccountManager;
+use OCP\IAvatarManager;
+use OCP\IUser;
+use OCP\UserMigration\IExportDestination;
+use OCP\UserMigration\IImportSource;
+use OCP\UserMigration\IMigrator;
+use OCP\UserMigration\TMigratorBasicVersionHandling;
+use Symfony\Component\Console\Output\OutputInterface;
+use Throwable;
+
+class AccountMigrator implements IMigrator {
+       use TMigratorBasicVersionHandling;
+
+       use TAccountsHelper;
+
+       private IAccountManager $accountManager;
+
+       private IAvatarManager $avatarManager;
+
+       private const EXPORT_ACCOUNT_FILE = 'account.json';
+
+       private const EXPORT_AVATAR_BASENAME = 'avatar';
+
+       public function __construct(
+               IAccountManager $accountManager,
+               IAvatarManager $avatarManager
+       ) {
+               $this->accountManager = $accountManager;
+               $this->avatarManager = $avatarManager;
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
+               $output->writeln('Exporting account information in ' . AccountMigrator::EXPORT_ACCOUNT_FILE . '…');
+
+               if ($exportDestination->addFileContents(AccountMigrator::EXPORT_ACCOUNT_FILE, json_encode($this->accountManager->getAccount($user))) === false) {
+                       throw new AccountMigratorException('Could not export account information');
+               }
+
+               $avatar = $this->avatarManager->getAvatar($user->getUID());
+               if ($avatar->isCustomAvatar()) {
+                       $avatarFile = $avatar->getFile(-1);
+                       $exportFilename = AccountMigrator::EXPORT_AVATAR_BASENAME . '.' . $avatarFile->getExtension();
+
+                       $output->writeln('Exporting avatar to ' . $exportFilename . '…');
+                       if ($exportDestination->addFileContents($exportFilename, $avatarFile->getContent()) === false) {
+                               throw new AccountMigratorException('Could not export avatar');
+                       }
+               }
+       }
+
+       /**
+        * {@inheritDoc}
+        */
+       public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
+               if ($importSource->getMigratorVersion(static::class) === null) {
+                       $output->writeln('No version for ' . static::class . ', skipping import…');
+                       return;
+               }
+
+               $output->writeln('Importing account information from ' . AccountMigrator::EXPORT_ACCOUNT_FILE . '…');
+
+               $account = $this->accountManager->getAccount($user);
+
+               /** @var array<string, array> $data */
+               $data = json_decode($importSource->getFileContents(AccountMigrator::EXPORT_ACCOUNT_FILE), true, 512, JSON_THROW_ON_ERROR);
+
+               foreach ($data as $propertyName => $propertyData) {
+                       if ($this->isCollection($propertyName)) {
+                               $collection = new AccountPropertyCollection($propertyName);
+                               /** @var array<int, array{name: string, value: string, scope: string, verified: string, verificationData: string}> $collectionData */
+                               $collectionData = $propertyData[$propertyName];
+                               foreach ($collectionData as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) {
+                                       $collection->addProperty(new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData));
+                               }
+                               $account->setPropertyCollection($collection);
+                       } else {
+                               /** @var array{name: string, value: string, scope: string, verified: string, verificationData: string} $propertyData */
+                               ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyData;
+                               $account->setProperty($propertyName, $value, $scope, $verified, $verificationData);
+                       }
+               }
+
+               try {
+                       $this->accountManager->updateAccount($account);
+               } catch (InvalidArgumentException $e) {
+                       throw new AccountMigratorException('Failed to import account information');
+               }
+
+               $avatarFiles = array_filter(
+                       $importSource->getFolderListing(''),
+                       fn (string $filename) => pathinfo($filename, PATHINFO_FILENAME) === AccountMigrator::EXPORT_AVATAR_BASENAME,
+               );
+
+               if (!empty($avatarFiles)) {
+                       if (count($avatarFiles) >= 2) {
+                               $output->writeln('Expected single avatar image file, using first file found');
+                       }
+
+                       $importFilename = reset($avatarFiles);
+
+                       $output->writeln('Importing avatar from ' . $importFilename . '…');
+                       $data = $importSource->getFileContents($importFilename);
+                       $image = new \OC_Image();
+                       $image->loadFromData($data);
+
+                       try {
+                               $avatar = $this->avatarManager->getAvatar($user->getUID());
+                               $avatar->set($image);
+                       } catch (NotSquareException $e) {
+                               throw new AccountMigratorException('Avatar image must be square');
+                       } catch (Throwable $e) {
+                               throw new AccountMigratorException('Failed to import avatar', 0, $e);
+                       }
+               }
+       }
+}
diff --git a/apps/settings/lib/UserMigration/AccountMigratorException.php b/apps/settings/lib/UserMigration/AccountMigratorException.php
new file mode 100644 (file)
index 0000000..91c63ba
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright 2022 Christopher Ng <chrng8@gmail.com>
+ *
+ * @author Christopher Ng <chrng8@gmail.com>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Settings\UserMigration;
+
+use OCP\UserMigration\UserMigrationException;
+
+class AccountMigratorException extends UserMigrationException {
+}
diff --git a/lib/private/Accounts/AccountMigrator.php b/lib/private/Accounts/AccountMigrator.php
deleted file mode 100644 (file)
index 9edda8f..0000000
+++ /dev/null
@@ -1,149 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/**
- * @copyright 2022 Christopher Ng <chrng8@gmail.com>
- *
- * @author Christopher Ng <chrng8@gmail.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Accounts;
-
-use InvalidArgumentException;
-use OC\NotSquareException;
-use OCP\Accounts\IAccountManager;
-use OCP\IAvatarManager;
-use OCP\IUser;
-use OCP\UserMigration\IExportDestination;
-use OCP\UserMigration\IImportSource;
-use OCP\UserMigration\IMigrator;
-use OCP\UserMigration\TMigratorBasicVersionHandling;
-use Symfony\Component\Console\Output\OutputInterface;
-use Throwable;
-
-class AccountMigrator implements IMigrator {
-       use TMigratorBasicVersionHandling;
-
-       use TAccountsHelper;
-
-       private IAccountManager $accountManager;
-
-       private IAvatarManager $avatarManager;
-
-       private const EXPORT_ACCOUNT_FILE = 'account.json';
-
-       private const EXPORT_AVATAR_BASENAME = 'avatar';
-
-       public function __construct(
-               IAccountManager $accountManager,
-               IAvatarManager $avatarManager
-       ) {
-               $this->accountManager = $accountManager;
-               $this->avatarManager = $avatarManager;
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void {
-               $output->writeln('Exporting account information in ' . AccountMigrator::EXPORT_ACCOUNT_FILE . '…');
-
-               if ($exportDestination->addFileContents(AccountMigrator::EXPORT_ACCOUNT_FILE, json_encode($this->accountManager->getAccount($user))) === false) {
-                       throw new AccountMigratorException('Could not export account information');
-               }
-
-               $avatar = $this->avatarManager->getAvatar($user->getUID());
-               if ($avatar->isCustomAvatar()) {
-                       $avatarFile = $avatar->getFile(-1);
-                       $exportFilename = AccountMigrator::EXPORT_AVATAR_BASENAME . '.' . $avatarFile->getExtension();
-
-                       $output->writeln('Exporting avatar to ' . $exportFilename . '…');
-                       if ($exportDestination->addFileContents($exportFilename, $avatarFile->getContent()) === false) {
-                               throw new AccountMigratorException('Could not export avatar');
-                       }
-               }
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void {
-               if ($importSource->getMigratorVersion(static::class) === null) {
-                       $output->writeln('No version for ' . static::class . ', skipping import…');
-                       return;
-               }
-
-               $output->writeln('Importing account information from ' . AccountMigrator::EXPORT_ACCOUNT_FILE . '…');
-
-               $account = $this->accountManager->getAccount($user);
-
-               /** @var array<string, array> $data */
-               $data = json_decode($importSource->getFileContents(AccountMigrator::EXPORT_ACCOUNT_FILE), true, 512, JSON_THROW_ON_ERROR);
-
-               foreach ($data as $propertyName => $propertyData) {
-                       if ($this->isCollection($propertyName)) {
-                               $collection = new AccountPropertyCollection($propertyName);
-                               /** @var array<int, array{name: string, value: string, scope: string, verified: string, verificationData: string}> $collectionData */
-                               $collectionData = $propertyData[$propertyName];
-                               foreach ($collectionData as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) {
-                                       $collection->addProperty(new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData));
-                               }
-                               $account->setPropertyCollection($collection);
-                       } else {
-                               /** @var array{name: string, value: string, scope: string, verified: string, verificationData: string} $propertyData */
-                               ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyData;
-                               $account->setProperty($propertyName, $value, $scope, $verified, $verificationData);
-                       }
-               }
-
-               try {
-                       $this->accountManager->updateAccount($account);
-               } catch (InvalidArgumentException $e) {
-                       throw new AccountMigratorException('Failed to import account information');
-               }
-
-               $avatarFiles = array_filter(
-                       $importSource->getFolderListing(''),
-                       fn (string $filename) => pathinfo($filename, PATHINFO_FILENAME) === AccountMigrator::EXPORT_AVATAR_BASENAME,
-               );
-
-               if (!empty($avatarFiles)) {
-                       if (count($avatarFiles) >= 2) {
-                               $output->writeln('Expected single avatar image file, using first file found');
-                       }
-
-                       $importFilename = reset($avatarFiles);
-
-                       $output->writeln('Importing avatar from ' . $importFilename . '…');
-                       $data = $importSource->getFileContents($importFilename);
-                       $image = new \OC_Image();
-                       $image->loadFromData($data);
-
-                       try {
-                               $avatar = $this->avatarManager->getAvatar($user->getUID());
-                               $avatar->set($image);
-                       } catch (NotSquareException $e) {
-                               throw new AccountMigratorException('Avatar image must be square');
-                       } catch (Throwable $e) {
-                               throw new AccountMigratorException('Failed to import avatar', 0, $e);
-                       }
-               }
-       }
-}
diff --git a/lib/private/Accounts/AccountMigratorException.php b/lib/private/Accounts/AccountMigratorException.php
deleted file mode 100644 (file)
index 328eceb..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-/**
- * @copyright 2022 Christopher Ng <chrng8@gmail.com>
- *
- * @author Christopher Ng <chrng8@gmail.com>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-namespace OC\Accounts;
-
-use Exception;
-
-class AccountMigratorException extends Exception {
-}