Skip to content

A free open source IT asset/license management system

Fix LDAP location sync issue in #3993 (#4181)

Richard Hofmanadca7cb0c500
diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.php
index 827de4e55e..09e71a3b2e 100755
--- a/app/Console/Commands/LdapSync.php
+++ b/app/Console/Commands/LdapSync.php
@@ -69,7 +69,15 @@ class LdapSync extends Command
$results = Ldap::findLdapUsers();
- $ldap_ou_locations = Location::whereNotNull('ldap_ou')->get();
+ // Retrieve locations with a mapped OU, and sort them from the shallowest to deepest OU (see #3993)
+ $ldap_ou_locations = Location::whereNotNull('ldap_ou')->get()->toArray();
+ $ldap_ou_lengths = array();
+
+ foreach ($ldap_ou_locations as $location) {
+ $ldap_ou_lengths[] = strlen($location["ldap_ou"]);
+ }
+
+ array_multisort($ldap_ou_lengths, SORT_ASC, $ldap_ou_locations);
if (sizeof($ldap_ou_locations) > 0) {
LOG::debug('Some locations have special OUs set. Locations will be automatically set for users in those OUs.');
@@ -99,11 +107,11 @@ 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);
+ $location_users = Ldap::findLdapUsers($ldap_loc["ldap_ou"]);
$usernames = array();
for ($i = 0; $i < $location_users["count"]; $i++) {
$location_users[$i]["ldap_location_override"] = true;
- $location_users[$i]["location_id"] = $ldap_loc->id;
+ $location_users[$i]["location_id"] = $ldap_loc["id"];
$usernames[] = $location_users[$i][$ldap_result_username][0];
}
@@ -194,7 +202,6 @@ class LdapSync extends Command
} else {
$this->info('User '.$summary[$x]['firstname'].' '.$summary[$x]['lastname'].' (username: '.$summary[$x]['username'].' was '.strtoupper($summary[$x]['createorupdate']).'.');
}
-
}
} else if ($this->option('json_summary')) {
$json_summary = [ "error" => false, "error_message" => "", "summary" => $summary ];
diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php
index e542a8eb6c..4abdfa65e1 100755
--- a/app/Http/Controllers/LocationsController.php
+++ b/app/Http/Controllers/LocationsController.php
@@ -89,6 +89,7 @@ class LocationsController extends Controller
$location->state = Input::get('state');
$location->country = Input::get('country');
$location->zip = Input::get('zip');
+ $location->ldap_ou = Input::get('ldap_ou');
$location->manager_id = Input::get('manager_id');
$location->user_id = Auth::id();