Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/Data/Import/LdifCommand.php
Show First 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | protected function loadFromFile(): void | ||||
$fh = fopen($file, 'r'); | $fh = fopen($file, 'r'); | ||||
$inserts = []; | $inserts = []; | ||||
$entry = []; | $entry = []; | ||||
$lastAttr = null; | $lastAttr = null; | ||||
$insertFunc = function ($limit = 0) use (&$entry, &$inserts) { | $insertFunc = function ($limit = 0) use (&$entry, &$inserts) { | ||||
// @phpstan-ignore-next-line | |||||
if (!empty($entry)) { | if (!empty($entry)) { | ||||
if ($entry = $this->parseLDAPEntry($entry)) { | if ($entry = $this->parseLDAPEntry($entry)) { | ||||
$inserts[] = $entry; | $inserts[] = $entry; | ||||
} | } | ||||
$entry = []; | $entry = []; | ||||
} | } | ||||
if (count($inserts) > $limit) { | if (count($inserts) > $limit) { | ||||
▲ Show 20 Lines • Show All 205 Lines • ▼ Show 20 Lines | protected function importResources(): void | ||||
// Make sure the domain exists | // Make sure the domain exists | ||||
if (!$this->domainExists($data->domain)) { | if (!$this->domainExists($data->domain)) { | ||||
$this->setImportWarning($_resource->id, "Domain not found"); | $this->setImportWarning($_resource->id, "Domain not found"); | ||||
continue; | continue; | ||||
} | } | ||||
$resource = new \App\Resource(); | $resource = new \App\Resource(); | ||||
$resource->name = $data->name; | $resource->name = $data->name; | ||||
$resource->domain = $data->domain; | $resource->domainName = $data->domain; | ||||
$resource->save(); | $resource->save(); | ||||
$resource->assignToWallet($this->wallet); | $resource->assignToWallet($this->wallet); | ||||
// Invitation policy | // Invitation policy | ||||
if (!empty($data->invitation_policy)) { | if (!empty($data->invitation_policy)) { | ||||
$resource->setSetting('invitation_policy', $data->invitation_policy); | $resource->setSetting('invitation_policy', $data->invitation_policy); | ||||
} | } | ||||
Show All 37 Lines | protected function importSharedFolders(): void | ||||
if (!$this->domainExists($data->domain)) { | if (!$this->domainExists($data->domain)) { | ||||
$this->setImportWarning($_folder->id, "Domain not found"); | $this->setImportWarning($_folder->id, "Domain not found"); | ||||
continue; | continue; | ||||
} | } | ||||
$folder = new \App\SharedFolder(); | $folder = new \App\SharedFolder(); | ||||
$folder->name = $data->name; | $folder->name = $data->name; | ||||
$folder->type = $data->type ?? 'mail'; | $folder->type = $data->type ?? 'mail'; | ||||
$folder->domain = $data->domain; | $folder->domainName = $data->domain; | ||||
$folder->save(); | $folder->save(); | ||||
$folder->assignToWallet($this->wallet); | $folder->assignToWallet($this->wallet); | ||||
// Invitation policy | // Invitation policy | ||||
if (!empty($data->acl)) { | if (!empty($data->acl)) { | ||||
$folder->setSetting('acl', json_encode($data->acl)); | $folder->setSetting('acl', json_encode($data->acl)); | ||||
} | } | ||||
// Target folder | // Target folder | ||||
if (!empty($data->folder)) { | if (!empty($data->folder)) { | ||||
$folder->setSetting('folder', $data->folder); | $folder->setSetting('folder', $data->folder); | ||||
} | } | ||||
// Import aliases | |||||
if (!empty($data->aliases)) { | |||||
$this->setObjectAliases($folder, $data->aliases); | |||||
} | |||||
} | } | ||||
$bar->finish(); | $bar->finish(); | ||||
$this->info("DONE"); | $this->info("DONE"); | ||||
} | } | ||||
/** | /** | ||||
* Import users from the temp table | * Import users from the temp table | ||||
*/ | */ | ||||
protected function importUsers(): void | protected function importUsers(): void | ||||
{ | { | ||||
$users = DB::table(self::$table)->where('type', 'user')->whereNull('error'); | $users = DB::table(self::$table)->where('type', 'user')->whereNull('error'); | ||||
// Skip the (already imported) account owner | // Skip the (already imported) account owner | ||||
if ($this->ownerDN) { | if ($this->ownerDN) { | ||||
$users->whereNotIn('dn', [$this->ownerDN]); | $users->whereNotIn('dn', [$this->ownerDN]); | ||||
} | } | ||||
// Import aliases of the owner, we got from importOwner() call | // Import aliases of the owner, we got from importOwner() call | ||||
if (!empty($this->aliases) && $this->wallet) { | if (!empty($this->aliases) && $this->wallet) { | ||||
$this->setUserAliases($this->wallet->owner, $this->aliases); | $this->setObjectAliases($this->wallet->owner, $this->aliases); | ||||
} | } | ||||
$bar = $this->createProgressBar($users->count(), "Importing users"); | $bar = $this->createProgressBar($users->count(), "Importing users"); | ||||
foreach ($users->cursor() as $_user) { | foreach ($users->cursor() as $_user) { | ||||
$bar->advance(); | $bar->advance(); | ||||
$this->importSingleUser($_user); | $this->importSingleUser($_user); | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | protected function importSingleUser($ldap_user) | ||||
// Import aliases | // Import aliases | ||||
if (!empty($data->aliases)) { | if (!empty($data->aliases)) { | ||||
if (!$this->wallet) { | if (!$this->wallet) { | ||||
// This is the account owner creation, at this point we likely do not have | // This is the account owner creation, at this point we likely do not have | ||||
// domain records yet, save the aliases to be inserted later (in importUsers()) | // domain records yet, save the aliases to be inserted later (in importUsers()) | ||||
$this->aliases = $data->aliases; | $this->aliases = $data->aliases; | ||||
} else { | } else { | ||||
$this->setUserAliases($user, $data->aliases); | $this->setObjectAliases($user, $data->aliases); | ||||
} | } | ||||
} | } | ||||
return $user; | return $user; | ||||
} | } | ||||
/** | /** | ||||
* Convert LDAP entry into an object supported by the migration tool | * Convert LDAP entry into an object supported by the migration tool | ||||
▲ Show 20 Lines • Show All 152 Lines • ▼ Show 20 Lines | protected function parseLDAPSharedFolder($entry) | ||||
if (!empty($entry['kolabtargetfolder'])) { | if (!empty($entry['kolabtargetfolder'])) { | ||||
$result['folder'] = $this->attrStringValue($entry, 'kolabtargetfolder'); | $result['folder'] = $this->attrStringValue($entry, 'kolabtargetfolder'); | ||||
} | } | ||||
if (!empty($entry['acl'])) { | if (!empty($entry['acl'])) { | ||||
$result['acl'] = $this->parseACL($this->attrArrayValue($entry, 'acl')); | $result['acl'] = $this->parseACL($this->attrArrayValue($entry, 'acl')); | ||||
} | } | ||||
if (!empty($entry['alias'])) { | |||||
$result['aliases'] = $this->attrArrayValue($entry, 'alias'); | |||||
} | |||||
} | } | ||||
return [$result, $error]; | return [$result, $error]; | ||||
} | } | ||||
/** | /** | ||||
* Convert LDAP user data into Kolab4 "format" | * Convert LDAP user data into Kolab4 "format" | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 235 Lines • ▼ Show 20 Lines | protected function preparePackagesAndSkus(): void | ||||
// Count storage skus | // Count storage skus | ||||
$sku = $this->packages['user']->skus()->where('title', 'storage')->first(); | $sku = $this->packages['user']->skus()->where('title', 'storage')->first(); | ||||
$this->packages['quota'] = $sku ? $sku->pivot->qty : 0; | $this->packages['quota'] = $sku ? $sku->pivot->qty : 0; | ||||
$this->packages['storage'] = \App\Sku::where('title', 'storage')->where('tenant_id', $tenant_id)->first(); | $this->packages['storage'] = \App\Sku::where('title', 'storage')->where('tenant_id', $tenant_id)->first(); | ||||
} | } | ||||
/** | /** | ||||
* Set aliases for the user | * Set aliases for for an object | ||||
*/ | */ | ||||
protected function setUserAliases(\App\User $user, array $aliases = []) | protected function setObjectAliases($object, array $aliases = []) | ||||
{ | { | ||||
if (!empty($aliases)) { | if (!empty($aliases)) { | ||||
// Some users might have alias entry with their main address, remove it | // Some users might have alias entry with their main address, remove it | ||||
$aliases = array_map('strtolower', $aliases); | $aliases = array_map('strtolower', $aliases); | ||||
$aliases = array_diff(array_unique($aliases), [$user->email]); | $aliases = array_diff(array_unique($aliases), [$object->email]); | ||||
// Remove aliases for domains that do not exist | // Remove aliases for domains that do not exist | ||||
if (!empty($aliases)) { | if (!empty($aliases)) { | ||||
$aliases = array_filter( | $aliases = array_filter( | ||||
$aliases, | $aliases, | ||||
function ($alias) { | function ($alias) { | ||||
return $this->domainExists(explode('@', $alias)[1]); | return $this->domainExists(explode('@', $alias)[1]); | ||||
} | } | ||||
); | ); | ||||
} | } | ||||
if (!empty($aliases)) { | if (!empty($aliases)) { | ||||
$user->setAliases($aliases); | $object->setAliases($aliases); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Set error message for specified import data record | * Set error message for specified import data record | ||||
*/ | */ | ||||
protected static function setImportError($id, $error): void | protected static function setImportError($id, $error): void | ||||
Show All 12 Lines |