From a182d8c9242aad393b718b523d2c4f3b8dba9762 Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 31 Oct 2016 20:59:46 -0700 Subject: [PATCH] Fixes #1348 - LDAP sync in artisan command TODO: Make the LDAP sync page work using this command to avoid code duplication --- app/Console/Commands/LdapSync.php | 164 ++++++++++++++++++++++++++++++ app/Console/Kernel.php | 1 + 2 files changed, 165 insertions(+) create mode 100644 app/Console/Commands/LdapSync.php diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.php new file mode 100644 index 0000000000..e7f2e4f7f7 --- /dev/null +++ b/app/Console/Commands/LdapSync.php @@ -0,0 +1,164 @@ +ldap_username_field; + $ldap_result_last_name = Setting::getSettings()->ldap_lname_field; + $ldap_result_first_name = Setting::getSettings()->ldap_fname_field; + + $ldap_result_active_flag = Setting::getSettings()->ldap_active_flag_field; + $ldap_result_emp_num = Setting::getSettings()->ldap_emp_num; + $ldap_result_email = Setting::getSettings()->ldap_email; + + try { + $ldapconn = Ldap::connectToLdap(); + } catch (\Exception $e) { + LOG::error($e); + } + + try { + Ldap::bindAdminToLdap($ldapconn); + } catch (\Exception $e) { + LOG::error($e); + } + + $summary = array(); + + $results = Ldap::findLdapUsers(); + + if ($this->option('location')!='') { + $location = Location::where('name','=',$this->option('location'))->first(); + LOG::debug('Location name '.$this->option('location').' passed'); + LOG::debug('Importing to '.$location->name.' ('.$location->id.')'); + } elseif ($this->option('location_id')!='') { + $location = Location::where('id','=',$this->option('location_id'))->first(); + LOG::debug('Location ID '.$this->option('location_id').' passed'); + LOG::debug('Importing to '.$location->name.' ('.$location->id.')'); + } else { + $location = new Location; + } + + if (!isset($location)) { + LOG::debug('That location is invalid, so no location will be assigned.'); + } + + + $tmp_pass = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 20); + $pass = bcrypt($tmp_pass); + + + for ($i = 0; $i < $results["count"]; $i++) { + if (empty($ldap_result_active_flag) || $results[$i][$ldap_result_active_flag][0] == "TRUE") { + + $item = array(); + $item["username"] = isset($results[$i][$ldap_result_username][0]) ? $results[$i][$ldap_result_username][0] : ""; + $item["employee_number"] = isset($results[$i][$ldap_result_emp_num][0]) ? $results[$i][$ldap_result_emp_num][0] : ""; + $item["lastname"] = isset($results[$i][$ldap_result_last_name][0]) ? $results[$i][$ldap_result_last_name][0] : ""; + $item["firstname"] = isset($results[$i][$ldap_result_first_name][0]) ? $results[$i][$ldap_result_first_name][0] : ""; + $item["email"] = isset($results[$i][$ldap_result_email][0]) ? $results[$i][$ldap_result_email][0] : "" ; + + // User exists + $item["createorupdate"] = 'updated'; + if (!$user = User::where('username', $item["username"])->first()) { + $user = new User; + $user->password = $pass; + $item["createorupdate"] = 'created'; + } + + // Create the user if they don't exist. + + + $user->first_name = e($item["firstname"]); + $user->last_name = e($item["lastname"]); + $user->username = e($item["username"]); + $user->email = e($item["email"]); + $user->employee_num = e($item["employee_number"]); + $user->activated = 1; + + if ($location) { + $user->location_id = e($location->id); + } + + $user->notes = 'Imported from LDAP'; + $user->ldap_import = 1; + + $errors = ''; + + if ($user->save()) { + $item["note"] = $item["createorupdate"]; + $item["status"]='success'; + } else { + foreach ($user->getErrors()->getMessages() as $key => $err) { + $errors .= $err[0]; + } + $item["note"] = $errors; + $item["status"]='error'; + } + + array_push($summary, $item); + } + + } + + if ($this->option('summary')) { + for ($x = 0; $x < count($summary); $x++) { + if ($summary[$x]['status']=='error') { + $this->error('ERROR: '.$summary[$x]['firstname'].' '.$summary[$x]['lastname'].' (username: '.$summary[$x]['username'].' was not imported: '.$summary[$x]['note'] ); + } else { + $this->info('User '.$summary[$x]['firstname'].' '.$summary[$x]['lastname'].' (username: '.$summary[$x]['username'].' was '.strtoupper($summary[$x]['createorupdate']).'.'); + } + + } + } else { + return $summary; + } + + + + } + + +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 68bb7ecf6d..3e04cb550b 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -23,6 +23,7 @@ class Kernel extends ConsoleKernel Commands\SystemBackup::class, Commands\DisableLDAP::class, Commands\Purge::class, + Commands\LdapSync::class, ]; /**