handle deleted user during import from v1 (#569)
This commit is contained in:
@@ -127,6 +127,7 @@ class KimaiImporterCommand extends Command
|
||||
->addArgument('prefix', InputArgument::REQUIRED, 'The database prefix for the old Kimai v1 tables')
|
||||
->addArgument('password', InputArgument::REQUIRED, 'The new password for all imported user')
|
||||
->addArgument('country', InputArgument::OPTIONAL, 'The default country for customer (2-character uppercase)', 'DE')
|
||||
->addArgument('currency', InputArgument::OPTIONAL, 'The default currency for customer (code like EUR, CHF, GBP or USD)', 'EUR')
|
||||
;
|
||||
}
|
||||
|
||||
@@ -158,7 +159,14 @@ class KimaiImporterCommand extends Command
|
||||
|
||||
$country = $input->getArgument('country');
|
||||
if (2 != trim(strlen($country))) {
|
||||
$io->error('Country length needs to be exactly 2 character');
|
||||
$io->error('Country code needs to be exactly 2 character');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$currency = $input->getArgument('currency');
|
||||
if (3 != trim(strlen($currency))) {
|
||||
$io->error('Currency code needs to be exactly 3 character');
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -260,7 +268,7 @@ class KimaiImporterCommand extends Command
|
||||
}
|
||||
|
||||
try {
|
||||
$counter = $this->importCustomers($io, $customer, $country);
|
||||
$counter = $this->importCustomers($io, $customer, $country, $currency);
|
||||
$allImports += $counter;
|
||||
$io->success('Imported customers: ' . $counter);
|
||||
} catch (\Exception $ex) {
|
||||
@@ -576,10 +584,11 @@ class KimaiImporterCommand extends Command
|
||||
* @param SymfonyStyle $io
|
||||
* @param array $customers
|
||||
* @param string $country
|
||||
* @param string $currency
|
||||
* @return int
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function importCustomers(SymfonyStyle $io, $customers, $country)
|
||||
protected function importCustomers(SymfonyStyle $io, $customers, $country, $currency)
|
||||
{
|
||||
$counter = 0;
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
@@ -607,6 +616,7 @@ class KimaiImporterCommand extends Command
|
||||
->setTimezone($oldCustomer['timezone'])
|
||||
->setVisible($isActive)
|
||||
->setCountry(strtoupper($country))
|
||||
->setCurrency(strtoupper($currency))
|
||||
;
|
||||
|
||||
if (!$this->validateImport($io, $customer)) {
|
||||
@@ -910,6 +920,7 @@ class KimaiImporterCommand extends Command
|
||||
{
|
||||
$counter = 0;
|
||||
$activityCounter = 0;
|
||||
$userCounter = 0;
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$total = count($records);
|
||||
|
||||
@@ -949,11 +960,45 @@ class KimaiImporterCommand extends Command
|
||||
|
||||
$duration = $oldRecord['end'] - $oldRecord['start'];
|
||||
|
||||
// FIXME create user on the fly
|
||||
// ----------------------- unknown user, damned missing data integrity in Kimai v1 -----------------------
|
||||
if (!isset($this->users[$oldRecord['userID']])) {
|
||||
$io->error('Could not import timesheet record, unknown user: ' . $oldRecord['userID']);
|
||||
continue;
|
||||
|
||||
$tempUserName = uniqid();
|
||||
$tempPassword = uniqid() . uniqid();
|
||||
|
||||
$user = new User();
|
||||
$user->setUsername($tempUserName)
|
||||
->setAlias('Import: ' . $tempUserName)
|
||||
->setEmail($tempUserName . '@example.com')
|
||||
->setPlainPassword($tempPassword)
|
||||
->setEnabled(false)
|
||||
->setRoles([USER::ROLE_USER])
|
||||
;
|
||||
|
||||
$pwd = $this->encoder->encodePassword($user, $user->getPlainPassword());
|
||||
$user->setPassword($pwd);
|
||||
|
||||
if (!$this->validateImport($io, $user)) {
|
||||
$io->error('Found timesheet record for unknown user and failed to create user, skipping timesheet: ' . $oldRecord['timeEntryID']);
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
$entityManager->persist($user);
|
||||
$entityManager->flush();
|
||||
if ($this->debug) {
|
||||
$io->success('Created deactivated user: ' . $user->getUsername());
|
||||
}
|
||||
$userCounter++;
|
||||
} catch (\Exception $ex) {
|
||||
$io->error('Failed to create user: ' . $user->getUsername());
|
||||
$io->error('Reason: ' . $ex->getMessage());
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->users[$oldRecord['userID']] = $user;
|
||||
}
|
||||
// ----------------------- unknown user end -----------------------
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
|
||||
@@ -1028,6 +1073,9 @@ class KimaiImporterCommand extends Command
|
||||
}
|
||||
$io->writeln(' (' . $counter . '/' . $total . ')');
|
||||
|
||||
if ($userCounter > 0) {
|
||||
$io->success('Created new users during timesheet import: ' . $userCounter);
|
||||
}
|
||||
if ($activityCounter > 0) {
|
||||
$io->success('Created new activities during timesheet import: ' . $activityCounter);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user