Clean up how we use the '$location' in LDAP sync command

This commit is contained in:
Brady Wetherington 2024-10-15 17:26:31 +01:00
parent 16c8264e76
commit e4e1d0d50a

View file

@ -137,23 +137,24 @@ class LdapSync extends Command
} }
/* Determine which location to assign users to by default. */ /* Determine which location to assign users to by default. */
$location = null; // TODO - this would be better called "$default_location", which is more explicit about its purpose $default_location = null;
if ($this->option('location') != '') { if ($this->option('location') != '') {
if ($location = Location::where('name', '=', $this->option('location'))->first()) { if ($default_location = Location::where('name', '=', $this->option('location'))->first()) {
Log::debug('Location name ' . $this->option('location') . ' passed'); Log::debug('Location name ' . $this->option('location') . ' passed');
Log::debug('Importing to ' . $location->name . ' (' . $location->id . ')'); Log::debug('Importing to '.$default_location->name.' ('.$default_location->id.')');
} }
} elseif ($this->option('location_id')) { } elseif ($this->option('location_id')) {
//TODO - figure out how or why this is an array?
foreach($this->option('location_id') as $location_id) { foreach($this->option('location_id') as $location_id) {
if ($location = Location::where('id', '=', $location_id)->first()) { if ($default_location = Location::where('id', '=', $location_id)->first()) {
Log::debug('Location ID ' . $location_id . ' passed'); Log::debug('Location ID ' . $location_id . ' passed');
Log::debug('Importing to ' . $location->name . ' (' . $location->id . ')'); Log::debug('Importing to '.$default_location->name.' ('.$default_location->id.')');
} }
} }
} }
if (! isset($location)) { if (!isset($default_location)) {
Log::debug('That location is invalid or a location was not provided, so no location will be assigned by default.'); Log::debug('That location is invalid or a location was not provided, so no location will be assigned by default.');
} }
@ -243,6 +244,7 @@ class LdapSync extends Command
$item['department'] = $results[$i][$ldap_map["dept"]][0] ?? ''; $item['department'] = $results[$i][$ldap_map["dept"]][0] ?? '';
$item['manager'] = $results[$i][$ldap_map["manager"]][0] ?? ''; $item['manager'] = $results[$i][$ldap_map["manager"]][0] ?? '';
$item['location'] = $results[$i][$ldap_map["location"]][0] ?? ''; $item['location'] = $results[$i][$ldap_map["location"]][0] ?? '';
$location = $default_location; //initially, set '$location' to the default_location (which may just be `null`)
// ONLY if you are using the "ldap_location" option *AND* you have an actual result // ONLY if you are using the "ldap_location" option *AND* you have an actual result
if ($ldap_map["location"] && $item['location']) { if ($ldap_map["location"] && $item['location']) {
@ -296,7 +298,7 @@ class LdapSync extends Command
$user->department_id = $department->id; $user->department_id = $department->id;
} }
if($ldap_map["location"] != null){ if($ldap_map["location"] != null){
$user->location_id = $location ? $location->id : null; $user->location_id = $location?->id;
} }
if($ldap_map["manager"] != null){ if($ldap_map["manager"] != null){
@ -399,9 +401,12 @@ class LdapSync extends Command
if ((is_array($location)) && (array_key_exists('id', $location))) { if ((is_array($location)) && (array_key_exists('id', $location))) {
$user->location_id = $location['id']; $user->location_id = $location['id'];
} elseif (is_object($location)) { } elseif (is_object($location)) {
$user->location_id = $location->id; $user->location_id = $location->id; //THIS is the magic line, this should do it.
} }
} }
// TODO - should we be NULLING locations if $location is really `null`, and that's what we came up with?
// will that conflict with any overriding setting that the user set? Like, if they moved someone from
// the 'null' location to somewhere, we wouldn't want to try to override that, right?
$location = null; $location = null;
$user->ldap_import = 1; $user->ldap_import = 1;