diff --git a/developer_manual/digging_deeper/index.rst b/developer_manual/digging_deeper/index.rst index bd4d116f2..ef9feadd4 100644 --- a/developer_manual/digging_deeper/index.rst +++ b/developer_manual/digging_deeper/index.rst @@ -31,3 +31,4 @@ Digging deeper status security profile + user_migration diff --git a/developer_manual/digging_deeper/user_migration.rst b/developer_manual/digging_deeper/user_migration.rst new file mode 100644 index 000000000..7bd49db6b --- /dev/null +++ b/developer_manual/digging_deeper/user_migration.rst @@ -0,0 +1,154 @@ +============== +User Migration +============== + +The `User Migration app `_ may +be installed to allow migration of user data. + +App developers can integrate into User Migration and provide ways to export +and import the app data of a user. + +Register a migrator +------------------- + +A migrator is represented by a class implementing the +``OCP\\UserMigration\\IMigrator`` interface. This class is instantiated +whenever a user export or import begins. + +.. code-block:: php + + myAppManager = $myAppManager; + $this->l10n = $l10n; + } + + /** + * Export user data + * + * @throws UserMigrationException + * @since 24.0.0 + */ + public function export(IUser $user, IExportDestination $exportDestination, OutputInterface $output): void { + $output->writeln('Exporting myapp information in ' . MyAppMigrator::PATH_MYAPP_FILE . '…'); + + try { + $data = $this->myAppManager->getUserData($user); + $exportDestination->addFileContents(MyAppMigrator::PATH_MYAPP_FILE, json_encode($data)); + } catch (Throwable $e) { + throw new UserMigrationException('Could not export myapp information', 0, $e); + } + } + + /** + * Import user data + * + * @throws UserMigrationException + * @since 24.0.0 + */ + public function import(IUser $user, IImportSource $importSource, OutputInterface $output): void { + if ($importSource->getMigratorVersion($this->getId()) === null) { + $output->writeln('No version for ' . static::class . ', skipping import…'); + return; + } + + $output->writeln('Importing myapp information from ' . MyAppMigrator::PATH_MYAPP_FILE . '…'); + + $data = json_decode($importSource->getFileContents(MyAppMigrator::PATH_MYAPP_FILE), true, 512, JSON_THROW_ON_ERROR); + + try { + $this->myAppManager->setUserData($user, $data); + } catch (Throwable $e) { + throw new UserMigrationException('Could not import myapp information', 0, $e); + } + } + + /** + * Returns the unique ID + * + * @since 24.0.0 + */ + public function getId(): string { + return 'myapp'; + } + + /** + * Returns the display name + * + * @since 24.0.0 + */ + public function getDisplayName(): string { + return $this->l10n->t('My App'); + } + + /** + * Returns the description + * + * @since 24.0.0 + */ + public function getDescription(): string { + return $this->l10n->t('My App information'); + } + } + +The ``MyAppMigrator`` class needs to be registered during the :ref:`app bootstrap` + +.. code-block:: php + + registerUserMigrator(MyAppMigrator::class); + } + + public function boot(IBootContext $context): void { + } + }