@@ -70,7 +70,7 @@ class LdapAuth extends BaseObject
70
70
private $ _username ;
71
71
private $ _curDn ;
72
72
private $ _curDomainHostname ;
73
-
73
+ private $ _curDomainKey ;
74
74
private $ _singleValuedAttrs ;
75
75
76
76
public function init ()
@@ -188,8 +188,6 @@ public function autoDetect($overrideIp = false)
188
188
public function login ($ username , $ password , $ domainKey = false , $ fetchUserDN = false )
189
189
{
190
190
191
- Yii::debug ('Hello! :) Trying to log you in via LDAP! ' , __METHOD__ );
192
-
193
191
if ($ fetchUserDN ) {
194
192
Yii::debug ("We have to determine the user DN first! " , __METHOD__ );
195
193
$ userDNSearch = $ this ->searchUser ($ username , ['dn ' ], null , $ domainKey , true );
@@ -211,6 +209,11 @@ public function login($username, $password, $domainKey = false, $fetchUserDN = f
211
209
}
212
210
}
213
211
212
+ if ($ this ->_l && $ domainKey && $ domainKey === $ this ->_curDomainKey ) {
213
+ Yii::debug ("Reusing current LDAP link identifier " , __METHOD__ );
214
+ return true ;
215
+ }
216
+
214
217
if ($ domainKey === false ) {
215
218
Yii::debug ("Using all domains " , __METHOD__ );
216
219
$ domains = $ this ->domains ;
@@ -310,6 +313,7 @@ public function login($username, $password, $domainKey = false, $fetchUserDN = f
310
313
$ this ->_ldapBaseDn = $ domainData ['baseDn ' ];
311
314
$ this ->_username = $ username ;
312
315
$ this ->_curDomainHostname = $ domainData ['hostname ' ];
316
+ $ this ->_curDomainKey = $ domainKey ;
313
317
314
318
return true ;
315
319
}
@@ -357,7 +361,7 @@ public function fetchUserData($attributes = "")
357
361
}
358
362
$ sid = self ::SIDtoString ($ entries [0 ]['objectsid ' ])[0 ];
359
363
$ sidHistory = isset ($ entries [0 ]['sidhistory ' ]) ? self ::SIDtoString ($ entries [0 ]['sidhistory ' ]) : null ;
360
- return array_merge (['sid ' => $ sid , 'sidhistory ' => $ sidHistory ], $ this ->handleEntry ($ entries [0 ], $ dom ));
364
+ return array_merge (['sid ' => $ sid , 'sidhistory ' => $ sidHistory ], $ this ->handleEntry ($ entries [0 ]));
361
365
} else {
362
366
Yii::error ('[FetchUserData]: Search failed: ' . ldap_error ($ this ->_l ), __METHOD__ );
363
367
return false ;
@@ -457,12 +461,10 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
457
461
$ searchFilter = str_replace (["%searchFor% " , "%onlyActive% " ], [addslashes ($ searchFor ), $ onlyActive ], $ searchFilter );
458
462
$ baseDN = $ baseDN ?: $ this ->_ldapBaseDn ;
459
463
460
- Yii::debug ('Search-Filter: ' . $ searchFilter , __METHOD__ );
464
+ Yii::debug ('Search-Filter: ' . $ searchFilter . " | BaseDN: " . $ baseDN , __METHOD__ );
461
465
462
466
$ result = ldap_read ($ this ->_l , '' , '(objectClass=*) ' , ['supportedControl ' ]);
463
467
$ supControls = ldap_get_entries ($ this ->_l , $ result );
464
- Yii::debug ("Supported Controls here: " , __METHOD__ );
465
- Yii::debug ($ supControls , __METHOD__ );
466
468
467
469
if (empty ($ this ->_singleValuedAttrs ) || !isset ($ this ->_singleValuedAttrs [$ domain ['hostname ' ]])) {
468
470
$ this ->_singleValuedAttrs [$ domain ['hostname ' ]] = [];
@@ -522,7 +524,7 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
522
524
} else {
523
525
Yii::error ('ldap_search_error: ' . ldap_error ($ this ->_l ), __METHOD__ );
524
526
}
525
- Yii:: error ( " Search query: " . $ searchFilter , __METHOD__ ) ;
527
+ $ this -> _l = null ;
526
528
break ;
527
529
}
528
530
ldap_parse_result ($ this ->_l , $ result , $ errcode , $ matcheddn , $ errmsg , $ referrals , $ controls );
@@ -585,13 +587,11 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
585
587
// Empty cookie means last page
586
588
} while (!empty ($ cookie ));
587
589
588
- // Reset LDAP Link
589
- ldap_close ($ this ->_l );
590
- $ this ->_l = null ;
591
- }
592
590
593
- Yii::debug ("Result: " , __METHOD__ );
594
- Yii::debug ($ return , __METHOD__ );
591
+ if ($ result ) {
592
+ @ldap_free_result ($ result );
593
+ }
594
+ }
595
595
596
596
if ($ this ->enableCache ) {
597
597
Yii::debug ("Adding cache entry " , __METHOD__ );
@@ -730,7 +730,7 @@ public static function SIDtoString($ADsid)
730
730
continue ;
731
731
}
732
732
}
733
- Yii::debug ('Converted SID to: ' . $ sid , __METHOD__ );
733
+ // Yii::debug('Converted SID to: ' . $sid, __METHOD__);
734
734
array_push ($ results , $ sid );
735
735
}
736
736
return $ results ;
@@ -740,14 +740,14 @@ private function handleEntry($entry)
740
740
{
741
741
$ newEntry = [];
742
742
foreach ($ entry as $ attr => $ value ) {
743
- Yii::debug ('Processing attribute ' . $ attr , __FUNCTION__ );
743
+ // Yii::debug('Processing attribute ' . $attr, __FUNCTION__);
744
744
745
745
if (is_int ($ attr ) || $ attr == 'objectsid ' || $ attr == 'sidhistory ' || !isset ($ value ['count ' ])) {
746
- Yii::debug ('Skipping... ' , __FUNCTION__ );
746
+ // Yii::debug('Skipping...', __FUNCTION__);
747
747
continue ;
748
748
}
749
749
$ count = $ value ['count ' ];
750
- Yii::debug ('Count: ' . $ count , __FUNCTION__ );
750
+ // Yii::debug('Count: ' . $count, __FUNCTION__);
751
751
752
752
if ($ count > 1 || !in_array ($ attr , $ this ->_singleValuedAttrs [$ this ->_curDomainHostname ] ?? [])) {
753
753
unset($ value ['count ' ]);
@@ -764,4 +764,12 @@ public function getLastError()
764
764
return ldap_error ($ this ->_l );
765
765
}
766
766
767
+ public function __destruct ()
768
+ {
769
+ if ($ this ->_l ) {
770
+ @ldap_close ($ this ->_l );
771
+ $ this ->_l = null ;
772
+ }
773
+ }
774
+
767
775
}
0 commit comments