From c62d43a77831dd798054b95e7ad9e72210f6accf Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Mon, 10 Aug 2020 17:04:17 -0700 Subject: [PATCH] Improve Exception management in Artisan LDAP Sync method. Still need to localize this better --- app/Console/Commands/LdapSync.php | 13 +++++++++++-- app/Models/Ldap.php | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.php index 1297c91213..b0897f2406 100755 --- a/app/Console/Commands/LdapSync.php +++ b/app/Console/Commands/LdapSync.php @@ -124,7 +124,16 @@ class LdapSync extends Command // Grab subsets based on location-specific DNs, and overwrite location for these users. foreach ($ldap_ou_locations as $ldap_loc) { - $location_users = Ldap::findLdapUsers($ldap_loc["ldap_ou"]); + try { + $location_users = Ldap::findLdapUsers($ldap_loc["ldap_ou"]); + } catch (\Exception $e) { // FIXME: this is stolen from line 77 or so above + if ($this->option('json_summary')) { + $json_summary = [ "error" => true, "error_message" => "Location ID: ".$ldap_loc['id']." (".$ldap_loc['name'].") cannot connect to \"".$ldap_loc["ldap_ou"]."\" - ".$e->getMessage(), "summary" => [] ]; + $this->info(json_encode($json_summary)); + } + LOG::info($e); + return []; + } $usernames = array(); for ($i = 0; $i < $location_users["count"]; $i++) { @@ -264,7 +273,7 @@ class LdapSync extends Command } } } else if ($this->option('json_summary')) { - $json_summary = [ "error" => false, "error_message" => "", "summary" => $summary ]; + $json_summary = [ "error" => false, "error_message" => "", "summary" => $summary ]; // hardcoding the error to false and the error_message to blank seems a bit weird $this->info(json_encode($json_summary)); } else { return $summary; diff --git a/app/Models/Ldap.php b/app/Models/Ldap.php index bf1b9fe8fb..e135714fdf 100644 --- a/app/Models/Ldap.php +++ b/app/Models/Ldap.php @@ -265,13 +265,13 @@ class Ldap extends Model $search_results = ldap_search($ldapconn, $base_dn, '('.$filter.')'); if (!$search_results) { - return redirect()->route('users.index')->with('error', trans('admin/users/message.error.ldap_could_not_search').ldap_error($ldapconn)); + return redirect()->route('users.index')->with('error', trans('admin/users/message.error.ldap_could_not_search').ldap_error($ldapconn)); // FIXME this is never called in any routed context - only from the Artisan command. So this redirect will never work. } // Get results from page $results = ldap_get_entries($ldapconn, $search_results); if (!$results) { - return redirect()->route('users.index')->with('error', trans('admin/users/message.error.ldap_could_not_get_entries').ldap_error($ldapconn)); + return redirect()->route('users.index')->with('error', trans('admin/users/message.error.ldap_could_not_get_entries').ldap_error($ldapconn)); // FIXME this is never called in any routed context - only from the Artisan command. So this redirect will never work. } // Add results to result set