-
Notifications
You must be signed in to change notification settings - Fork 8
Add import background job #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright 2022 Christopher Ng <[email protected]> | ||
| * | ||
| * @author Christopher Ng <[email protected]> | ||
| * | ||
| * @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\UserMigration\BackgroundJob; | ||
|
|
||
| use OCA\UserMigration\AppInfo\Application; | ||
| use OCA\UserMigration\Db\UserImport; | ||
| use OCA\UserMigration\Db\UserImportMapper; | ||
| use OCA\UserMigration\Service\UserMigrationService; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use OCP\IUser; | ||
| use OCP\IUserManager; | ||
| use OCP\Notification\IManager as NotificationManager; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| class UserImportJob extends QueuedJob { | ||
| private IUserManager $userManager; | ||
| private UserMigrationService $migrationService; | ||
| private LoggerInterface $logger; | ||
| private NotificationManager $notificationManager; | ||
| private UserImportMapper $mapper; | ||
|
|
||
| public function __construct( | ||
| ITimeFactory $timeFactory, | ||
| IUserManager $userManager, | ||
| UserMigrationService $migrationService, | ||
| LoggerInterface $logger, | ||
| NotificationManager $notificationManager, | ||
| UserImportMapper $mapper | ||
| ) { | ||
| parent::__construct($timeFactory); | ||
|
|
||
| $this->userManager = $userManager; | ||
| $this->migrationService = $migrationService; | ||
| $this->logger = $logger; | ||
| $this->notificationManager = $notificationManager; | ||
| $this->mapper = $mapper; | ||
| } | ||
|
|
||
| public function run($argument): void { | ||
| $id = $argument['id']; | ||
|
|
||
| $import = $this->mapper->getById($id); | ||
| $user = $import->getSourceUser(); | ||
| $path = $import->getPath(); | ||
|
|
||
| $userObject = $this->userManager->get($user); | ||
|
|
||
| if (!$userObject instanceof IUser) { | ||
| $this->logger->error('Could not import: Unknown user ' . $user); | ||
| $this->failedNotication($import); | ||
| $this->mapper->delete($import); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $import->setStatus(UserImport::STATUS_STARTED); | ||
| $this->mapper->update($import); | ||
|
|
||
| $this->migrationService->import($path, $userObject); | ||
| $this->successNotification($import); | ||
| } catch (\Exception $e) { | ||
| $this->logger->error($e->getMessage(), ['exception' => $e]); | ||
| $this->failedNotication($import); | ||
| } finally { | ||
| $this->mapper->delete($import); | ||
| } | ||
| } | ||
|
|
||
| private function failedNotication(UserImport $import): void { | ||
| // Send notification to user | ||
| $notification = $this->notificationManager->createNotification(); | ||
| $notification->setUser($import->getSourceUser()) | ||
| ->setApp(Application::APP_ID) | ||
| ->setDateTime($this->time->getDateTime()) | ||
| ->setSubject('importFailed', [ | ||
| 'sourceUser' => $import->getSourceUser(), | ||
| ]) | ||
| ->setObject('import', (string)$import->getId()); | ||
| $this->notificationManager->notify($notification); | ||
| } | ||
|
|
||
| private function successNotification(UserImport $import): void { | ||
| // Send notification to user | ||
| $notification = $this->notificationManager->createNotification(); | ||
| $notification->setUser($import->getSourceUser()) | ||
| ->setApp(Application::APP_ID) | ||
| ->setDateTime($this->time->getDateTime()) | ||
| ->setSubject('importDone', [ | ||
| 'sourceUser' => $import->getSourceUser(), | ||
| ]) | ||
| ->setObject('import', (string)$import->getId()); | ||
| $this->notificationManager->notify($notification); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright 2022 Christopher Ng <[email protected]> | ||
| * | ||
| * @author Christopher Ng <[email protected]> | ||
| * | ||
| * @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\UserMigration\Db; | ||
|
|
||
| use OCP\AppFramework\Db\Entity; | ||
|
|
||
| /** | ||
| * @method void setSourceUser(string $uid) | ||
| * @method string getSourceUser() | ||
| * @method void setTargetUser(string $uid) | ||
| * @method string getTargetUser() | ||
| * @method void setPath(string $path) | ||
| * @method string getPath() | ||
| * @method void setMigrators(string $migrators) | ||
| * @method string getMigrators() | ||
| * @method void setStatus(int $status) | ||
| * @method int getStatus() | ||
| */ | ||
| class UserImport extends Entity { | ||
| public const STATUS_WAITING = 0; | ||
| public const STATUS_STARTED = 1; | ||
|
|
||
| /** @var string */ | ||
| protected $sourceUser; | ||
| /** @var string */ | ||
| protected $targetUser; | ||
| /** @var string */ | ||
| protected $path; | ||
| /** @var string JSON encoded array */ | ||
| protected $migrators; | ||
| /** @var int */ | ||
| protected $status; | ||
|
|
||
| public function __construct() { | ||
| $this->addType('sourceUser', 'string'); | ||
| $this->addType('targetUser', 'string'); | ||
| $this->addType('path', 'string'); | ||
| $this->addType('migrators', 'string'); | ||
| $this->addType('status', 'int'); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the migrators in an associative array | ||
| */ | ||
| public function getMigratorsArray(): ?array { | ||
| return json_decode($this->migrators, true); | ||
| } | ||
|
|
||
| /** | ||
| * Set the migrators | ||
| */ | ||
| public function setMigratorsArray(?array $migrators): void { | ||
| $this->setMigrators(json_encode($migrators)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright 2022 Christopher Ng <[email protected]> | ||
| * | ||
| * @author Christopher Ng <[email protected]> | ||
| * | ||
| * @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\UserMigration\Db; | ||
|
|
||
| use OCP\AppFramework\Db\QBMapper; | ||
| use OCP\IDBConnection; | ||
|
|
||
| class UserImportMapper extends QBMapper { | ||
| public const TABLE_NAME = 'user_import_jobs'; | ||
|
|
||
| public function __construct(IDBConnection $db) { | ||
| parent::__construct($db, static::TABLE_NAME, UserImport::class); | ||
| } | ||
|
|
||
| public function getById(int $id): UserImport { | ||
| $qb = $this->db->getQueryBuilder(); | ||
|
|
||
| $qb->select('*') | ||
| ->from($this->getTableName()) | ||
| ->where( | ||
| $qb->expr()->eq('id', $qb->createNamedParameter($id)) | ||
| ); | ||
|
|
||
| return $this->findEntity($qb); | ||
| } | ||
|
|
||
| public function getBySourceUser(string $userId): UserImport { | ||
| $qb = $this->db->getQueryBuilder(); | ||
|
|
||
| $qb->select('*') | ||
| ->from($this->getTableName()) | ||
| ->where( | ||
| $qb->expr()->eq('source_user', $qb->createNamedParameter($userId)) | ||
| ); | ||
|
|
||
| return $this->findEntity($qb); | ||
| } | ||
|
|
||
| public function getByTargetUser(string $userId): UserImport { | ||
| $qb = $this->db->getQueryBuilder(); | ||
|
|
||
| $qb->select('*') | ||
| ->from($this->getTableName()) | ||
| ->where( | ||
| $qb->expr()->eq('target_user', $qb->createNamedParameter($userId)) | ||
| ); | ||
|
|
||
| return $this->findEntity($qb); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright 2022 Christopher Ng <[email protected]> | ||
| * | ||
| * @author Christopher Ng <[email protected]> | ||
| * | ||
| * @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\UserMigration\Migration; | ||
|
|
||
| use Closure; | ||
| use OCA\UserMigration\Db\UserImportMapper; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
|
|
||
| class Version00001Date20220412116131 extends SimpleMigrationStep { | ||
|
|
||
| /** | ||
| * @param IOutput $output | ||
| * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
| * @param array $options | ||
| * @return null|ISchemaWrapper | ||
| */ | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| $table = $schema->createTable(UserImportMapper::TABLE_NAME); | ||
| $table->addColumn('id', Types::BIGINT, [ | ||
| 'autoincrement' => true, | ||
| 'notnull' => true, | ||
| 'length' => 20, | ||
| 'unsigned' => true, | ||
| ]); | ||
| $table->addColumn('source_user', Types::STRING, [ | ||
| 'notnull' => true, | ||
| 'length' => 64, | ||
| ]); | ||
| $table->addColumn('target_user', Types::STRING, [ | ||
| 'notnull' => true, | ||
| 'length' => 64, | ||
| ]); | ||
| $table->addColumn('path', Types::STRING, [ | ||
| 'notnull' => true, | ||
| 'length' => 4000, | ||
| ]); | ||
| $table->addColumn('migrators', Types::STRING, [ | ||
| 'notnull' => false, | ||
| 'length' => 4000, | ||
| ]); | ||
| $table->addColumn('status', Types::SMALLINT, [ | ||
| 'notnull' => true, | ||
| ]); | ||
| $table->setPrimaryKey(['id']); | ||
|
|
||
| return $schema; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we can just reuse the same table and add a flag to say whether it's an import or export ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
oc_user_import_jobstable adds thetarget_userandpathcolumns whichoc_user_export_jobsdoesn't have, so unless this massively impacts performance negatively let's keep it separate for now and add another migration later, if it's beneficial, to combine them into aoc_user_migration_jobstable and drop the other two