Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions lib/BackgroundJob/UserImportJob.php
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);
}
}
79 changes: 79 additions & 0 deletions lib/Db/UserImport.php
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() {
Copy link
Member

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 ?

Copy link
Member Author

@Pytal Pytal Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The oc_user_import_jobs table adds the target_user and path columns which oc_user_export_jobs doesn'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 a oc_user_migration_jobs table and drop the other two

$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));
}
}
74 changes: 74 additions & 0 deletions lib/Db/UserImportMapper.php
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);
}
}
78 changes: 78 additions & 0 deletions lib/Migration/Version00001Date20220412116131.php
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;
}
}
Loading