A free open source IT asset/license management system
LDAP Wizard: Fixed #19789 - check for empty() to handle differences in old LDAP and wizard
snipe1180e83e3cf3
diff --git a/app/Console/Commands/LdapSync.php b/app/Console/Commands/LdapSync.phpindex c72498fe2d..9e719b4a68 100644--- a/app/Console/Commands/LdapSync.php+++ b/app/Console/Commands/LdapSync.php@@ -127,7 +127,11 @@ class LdapSync extends Command*/$attributes = array_values(array_filter($ldap_map));- if (Setting::getSettings()->is_ad === 1 && is_null($ldap_map['activated'])) {+ // empty() rather than is_null() because the LDAP wizard+ // (App\Livewire\LdapSettings) persists an unset mapping as ''+ // whereas the legacy form path routes through+ // ConvertEmptyStringsToNull and persists it as null.+ if (Setting::getSettings()->is_ad === 1 && empty($ldap_map['activated'])) {$attributes[] = 'useraccountcontrol';}diff --git a/app/Livewire/LdapSettings.php b/app/Livewire/LdapSettings.phpindex 46bcf3a117..7246a047d2 100644--- a/app/Livewire/LdapSettings.php+++ b/app/Livewire/LdapSettings.php@@ -885,25 +885,33 @@ class LdapSettings extends Component// that's an optional sanity check the user runs on demand to make sure// their syncs will look right and map all of the right fields, not// a network gate we run for them. Save + advance to step 5.+ // Blank optional mapping fields persist as NULL, not ''.+ // The legacy LDAP form (ConvertEmptyStringsToNull on the HTTP+ // middleware stack) already stored blanks as null, so this+ // matches legacy behavior and prevents wizard-stored blanks from+ // tripping downstream `is_null()` / `=== null` gates like the+ // AD useraccountcontrol fetch in LdapSync (issue #19789).+ // ldap_username_field + ldap_fname_field are required by+ // step3SyntaxRules() so they always carry a value.$setting = Setting::getSettings();$setting->ldap_username_field = $this->ldap_username_field;$setting->ldap_fname_field = $this->ldap_fname_field;- $setting->ldap_lname_field = $this->ldap_lname_field;- $setting->ldap_display_name = $this->ldap_display_name;- $setting->ldap_email = $this->ldap_email;- $setting->ldap_emp_num = $this->ldap_emp_num;- $setting->ldap_phone_field = $this->ldap_phone_field;- $setting->ldap_mobile = $this->ldap_mobile;- $setting->ldap_jobtitle = $this->ldap_jobtitle;- $setting->ldap_manager = $this->ldap_manager;- $setting->ldap_dept = $this->ldap_dept;- $setting->ldap_address = $this->ldap_address;- $setting->ldap_city = $this->ldap_city;- $setting->ldap_state = $this->ldap_state;- $setting->ldap_zip = $this->ldap_zip;- $setting->ldap_country = $this->ldap_country;- $setting->ldap_location = $this->ldap_location;- $setting->ldap_active_flag = $this->ldap_active_flag;+ $setting->ldap_lname_field = trim($this->ldap_lname_field) ?: null;+ $setting->ldap_display_name = trim($this->ldap_display_name) ?: null;+ $setting->ldap_email = trim($this->ldap_email) ?: null;+ $setting->ldap_emp_num = trim($this->ldap_emp_num) ?: null;+ $setting->ldap_phone_field = trim($this->ldap_phone_field) ?: null;+ $setting->ldap_mobile = trim($this->ldap_mobile) ?: null;+ $setting->ldap_jobtitle = trim($this->ldap_jobtitle) ?: null;+ $setting->ldap_manager = trim($this->ldap_manager) ?: null;+ $setting->ldap_dept = trim($this->ldap_dept) ?: null;+ $setting->ldap_address = trim($this->ldap_address) ?: null;+ $setting->ldap_city = trim($this->ldap_city) ?: null;+ $setting->ldap_state = trim($this->ldap_state) ?: null;+ $setting->ldap_zip = trim($this->ldap_zip) ?: null;+ $setting->ldap_country = trim($this->ldap_country) ?: null;+ $setting->ldap_location = trim($this->ldap_location) ?: null;+ $setting->ldap_active_flag = trim($this->ldap_active_flag) ?: null;$setting->ldap_invert_active_flag = $this->ldap_invert_active_flag ? '1' : '0';$this->persistAndAdvance($setting);@@ -1333,7 +1341,7 @@ class LdapSettings extends Component$settings = Setting::getSettings();if (Ldap::shouldUseSaslExternal($settings)) {- if (!@ldap_sasl_bind($conn, null, null, 'EXTERNAL')) {+ if (! @ldap_sasl_bind($conn, null, null, 'EXTERNAL')) {$ldapError = Ldap::bindError($conn);@ldap_unbind($conn);$this->recordTestResult(diff --git a/tests/Feature/Settings/LdapWizardTest.php b/tests/Feature/Settings/LdapWizardTest.phpindex e915df57ff..f36b08d5aa 100644--- a/tests/Feature/Settings/LdapWizardTest.php+++ b/tests/Feature/Settings/LdapWizardTest.php@@ -546,6 +546,107 @@ class LdapWizardTest extends TestCase->assertHasErrors(['ldap_username_field']);}+ public function test_step3_persists_blank_optional_mappings_as_null_not_empty_string(): void+ {++ $this->actAsSuperuser();+ $this->ensureSetting([+ 'ldap_active_flag' => 'preexisting',+ 'ldap_dept' => 'preexisting',+ 'ldap_email' => 'preexisting',+ ]);++ Livewire::test(LdapSettings::class)+ ->set('highestStepReached', 3)+ ->set('currentStep', 3)+ ->set('ldap_username_field', 'uid')+ ->set('ldap_fname_field', 'givenname')+ ->set('ldap_lname_field', '')+ ->set('ldap_display_name', '')+ ->set('ldap_email', '')+ ->set('ldap_emp_num', '')+ ->set('ldap_phone_field', '')+ ->set('ldap_mobile', '')+ ->set('ldap_jobtitle', '')+ ->set('ldap_manager', '')+ ->set('ldap_dept', '')+ ->set('ldap_address', '')+ ->set('ldap_city', '')+ ->set('ldap_state', '')+ ->set('ldap_zip', '')+ ->set('ldap_country', '')+ ->set('ldap_location', '')+ ->set('ldap_active_flag', '')+ ->call('saveAndAdvance');++ Setting::$_cache = null;+ $saved = Setting::getSettings();++ $optionalMappings = [+ 'ldap_lname_field',+ 'ldap_display_name',+ 'ldap_email',+ 'ldap_emp_num',+ 'ldap_phone_field',+ 'ldap_mobile',+ 'ldap_jobtitle',+ 'ldap_manager',+ 'ldap_dept',+ 'ldap_address',+ 'ldap_city',+ 'ldap_state',+ 'ldap_zip',+ 'ldap_country',+ 'ldap_location',+ 'ldap_active_flag',+ ];+ foreach ($optionalMappings as $field) {+ $this->assertNull($saved->{$field}, "Expected blank {$field} to persist as null, got " . var_export($saved->{$field}, true));+ }+ }++ public function test_step3_whitespace_only_optional_mapping_persists_as_null(): void+ {+ // Defense-in-depth: a user who pastes a stray space / tab into+ // an optional mapping field should get the same null-stored+ // result as a user who left it empty.+ $this->actAsSuperuser();+ $this->ensureSetting();++ Livewire::test(LdapSettings::class)+ ->set('highestStepReached', 3)+ ->set('currentStep', 3)+ ->set('ldap_username_field', 'uid')+ ->set('ldap_fname_field', 'givenname')+ ->set('ldap_active_flag', " \t ")+ ->call('saveAndAdvance');++ Setting::$_cache = null;+ $this->assertNull(Setting::getSettings()->ldap_active_flag);+ }++ public function test_step3_populated_optional_mapping_is_preserved(): void+ {+ // Baseline for the null-coerce path: a non-blank value trimmed+ // of surrounding whitespace must survive the save.+ $this->actAsSuperuser();+ $this->ensureSetting();++ Livewire::test(LdapSettings::class)+ ->set('highestStepReached', 3)+ ->set('currentStep', 3)+ ->set('ldap_username_field', 'uid')+ ->set('ldap_fname_field', 'givenname')+ ->set('ldap_active_flag', 'useraccountcontrol')+ ->set('ldap_dept', 'department')+ ->call('saveAndAdvance');++ Setting::$_cache = null;+ $saved = Setting::getSettings();+ $this->assertSame('useraccountcontrol', $saved->ldap_active_flag);+ $this->assertSame('department', $saved->ldap_dept);+ }+// === Step 4 business logic =============================================public function test_step4_forces_ldap_enabled_true_on_save(): void