diff --git a/src/app/Console/Commands/HKCCPImportLdap.php b/src/app/Console/Commands/HKCCPImportLdap.php new file mode 100644 index 00000000..4672ce61 --- /dev/null +++ b/src/app/Console/Commands/HKCCPImportLdap.php @@ -0,0 +1,115 @@ +syncDomains(); + $this->syncUsers(); + + LDAP::disconnect(); + } + + private function syncDomains() + { + $domains = \App\Domain::withTrashed()->get(); + + $bar = $this->createProgressBar(count($domains), "Syncing Domains"); + + foreach ($domains as $domain) { + $bar->advance(); + + $result = null; + + try { + $result = LDAP::updateDomain($domain); + } catch (\Exception $e) { + \Log::error("Domain {$domain->namespace} failed (exception)"); + continue; + } + + if ($result) { + \Log::info("Domain {$domain->namespace} updated"); + } else { + \Log::error("Domain {$domain->namespace} failed"); + } + } + + $bar->finish(); + + $this->info("DONE"); + } + + private function syncUsers() + { + $users = \App\User::withTrashed()->get(); + + $bar = $this->createProgressBar(count($users), "Syncing Users"); + + foreach ($users as $user) { + $bar->advance(); + + $result = null; + + try { + $result = LDAP::updateUser($user); + } catch (\Exception $e) { + \Log::error("User {$user->email} failed (exception)"); + continue; + } + + if ($result) { + \Log::info("User {$user->email} updated"); + } else { + \Log::error("User {$user->email} failed"); + } + } + + $bar->finish(); + + $this->info("DONE"); + } + + private function createProgressBar($count, $message = null) + { + $bar = $this->output->createProgressBar($count); + $bar->setFormat( + '%current:7s%/%max:7s% [%bar%] %percent:3s%% %elapsed:7s%/%estimated:-7s% %message% ' + ); + + if ($message) { + $bar->setMessage($message . " ..."); + } + + $bar->start(); + + return $bar; + } +}