Skip to content

A free open source IT asset/license management system

Use mapping array

snipe69e589438020
diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.php
index 12cf3340e5..7a97d3a047 100644
--- a/app/Console/Commands/LdapSync.php
+++ b/app/Console/Commands/LdapSync.php
@@ -57,7 +57,7 @@ class LdapSync extends Command
// Single source of truth for internal-key => LDAP-attribute-name
// lives on the Ldap model so parseAndMapLdapAttributes and this
// command can't drift. Used here for the LDAP query attribute
- // list plus a handful of specific-lookup gates (active_flag,
+ // list plus a handful of specific-lookup gates (activated,
// manager, location, username) that only LdapSync needs.
$ldap_map = Ldap::attributeMap();
@@ -117,7 +117,7 @@ class LdapSync extends Command
*/
$attributes = array_values(array_filter($ldap_map));
- if (Setting::getSettings()->is_ad === 1 && is_null($ldap_map['active_flag'])) {
+ if (Setting::getSettings()->is_ad === 1 && is_null($ldap_map['activated'])) {
$attributes[] = 'useraccountcontrol';
}
@@ -260,7 +260,7 @@ class LdapSync extends Command
// Handles every mapped scalar field, plus Department and
// Location firstOrCreate for the LDAP-derived values. The
// three LdapSync-only concerns (manager LDAP re-query,
- // active_flag / UAC, OU location override) are handled
+ // activated / UAC, OU location override) are handled
// inline below because they don't apply to the first-login
// path.
Ldap::applyLdapAttributesToUser($user, $item);
@@ -315,10 +315,10 @@ class LdapSync extends Command
}
// Sync activated state for Active Directory.
- if (! empty($ldap_map['active_flag'])) { // IF we have an 'active' flag set....
+ if (! empty($ldap_map['activated'])) { // IF we have an 'active' flag set....
// ....then *most* things that are truthy will activate the user. Anything falsey will deactivate them.
// (Specifically, we don't handle a value of '0.0' correctly)
- $raw_value = @$results[$i][$ldap_map['active_flag']][0];
+ $raw_value = @$results[$i][$ldap_map['activated']][0];
$filter_var = filter_var($raw_value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
$boolean_cast = (bool) $raw_value;
diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php
index 297e3fbc5c..cef0e53037 100644
--- a/app/Http/Controllers/Api/SettingsController.php
+++ b/app/Http/Controllers/Api/SettingsController.php
@@ -43,31 +43,30 @@ class SettingsController extends Controller
'message' => 'Successfully connected to LDAP server.',
];
+ // Shape is driven by Ldap::parseAndMapLdapAttributes so
+ // this endpoint stays in sync with the sync command and
+ // the first-login create path. Blank fields collapse to
+ // null in the JSON so the JS side's `?? NULL` fallback
+ // renders "NULL" for missing values.
$users = collect(Ldap::findLdapUsers(null, 10))->filter(function ($value, $key) {
return is_int($key);
- })->slice(0, 10)->map(function ($item) use ($settings) {
- return (object) [
- 'username' => $item[$settings['ldap_username_field']][0] ?? null,
- 'display_name' => $item[$settings['ldap_display_name']][0] ?? null,
- 'employee_number' => $item[$settings['ldap_emp_num']][0] ?? null,
- 'lastname' => $item[$settings['ldap_lname_field']][0] ?? null,
- 'firstname' => $item[$settings['ldap_fname_field']][0] ?? null,
- 'email' => $item[$settings['ldap_email']][0] ?? null,
- 'phone' => $item[$settings['ldap_phone_field']][0] ?? null,
- 'mobile' => $item[$settings['ldap_mobile']][0] ?? null,
- 'jobtitle' => $item[$settings['ldap_jobtitle']][0] ?? null,
- 'department' => $item[$settings['ldap_department']][0] ?? null,
- 'manager' => $item[$settings['ldap_manager']][0] ?? null,
- 'address' => $item[$settings['ldap_address']][0] ?? null,
- 'city' => $item[$settings['ldap_city']][0] ?? null,
- 'state' => $item[$settings['ldap_state']][0] ?? null,
- 'zip' => $item[$settings['ldap_zip']][0] ?? null,
- 'country' => $item[$settings['ldap_country']][0] ?? null,
- 'location' => $item[$settings['ldap_location']][0] ?? null,
- ];
+ })->slice(0, 10)->map(function ($item) {
+ $mapped = Ldap::parseAndMapLdapAttributes($item);
+
+ return (object) array_map(fn ($value) => $value === '' ? null : $value, $mapped);
});
if ($users->count() > 0) {
+ // `fields` is the ordered internal_key => translated
+ // label map the JS iterates to build the results
+ // table's header + per-row cells. Same shape drives
+ // the LDAP wizard's step-3 preview, so both stay
+ // consistent as attributeMap() grows.
+ $labels = Ldap::attributeLabels();
+ $fields = collect(array_keys(Ldap::parseAndMapLdapAttributes([])))
+ ->mapWithKeys(fn ($key) => [$key => $labels[$key] ?? $key])
+ ->all();
$message['user_sync'] = [
+ 'fields' => $fields,
'users' => $users,
];
} else {