Skip to content

Commit c58a53b

Browse files
author
Robin Kluth
committed
fix(performance): Reuse Link, if valid
1 parent fe827be commit c58a53b

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/LdapAuth.php

+26-18
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class LdapAuth extends BaseObject
7070
private $_username;
7171
private $_curDn;
7272
private $_curDomainHostname;
73-
73+
private $_curDomainKey;
7474
private $_singleValuedAttrs;
7575

7676
public function init()
@@ -188,8 +188,6 @@ public function autoDetect($overrideIp = false)
188188
public function login($username, $password, $domainKey = false, $fetchUserDN = false)
189189
{
190190

191-
Yii::debug('Hello! :) Trying to log you in via LDAP!', __METHOD__);
192-
193191
if ($fetchUserDN) {
194192
Yii::debug("We have to determine the user DN first!", __METHOD__);
195193
$userDNSearch = $this->searchUser($username, ['dn'], null, $domainKey, true);
@@ -211,6 +209,11 @@ public function login($username, $password, $domainKey = false, $fetchUserDN = f
211209
}
212210
}
213211

212+
if ($this->_l && $domainKey && $domainKey === $this->_curDomainKey) {
213+
Yii::debug("Reusing current LDAP link identifier", __METHOD__);
214+
return true;
215+
}
216+
214217
if ($domainKey === false) {
215218
Yii::debug("Using all domains", __METHOD__);
216219
$domains = $this->domains;
@@ -310,6 +313,7 @@ public function login($username, $password, $domainKey = false, $fetchUserDN = f
310313
$this->_ldapBaseDn = $domainData['baseDn'];
311314
$this->_username = $username;
312315
$this->_curDomainHostname = $domainData['hostname'];
316+
$this->_curDomainKey = $domainKey;
313317

314318
return true;
315319
}
@@ -357,7 +361,7 @@ public function fetchUserData($attributes = "")
357361
}
358362
$sid = self::SIDtoString($entries[0]['objectsid'])[0];
359363
$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]));
361365
} else {
362366
Yii::error('[FetchUserData]: Search failed: ' . ldap_error($this->_l), __METHOD__);
363367
return false;
@@ -457,12 +461,10 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
457461
$searchFilter = str_replace(["%searchFor%", "%onlyActive%"], [addslashes($searchFor), $onlyActive], $searchFilter);
458462
$baseDN = $baseDN ?: $this->_ldapBaseDn;
459463

460-
Yii::debug('Search-Filter: ' . $searchFilter, __METHOD__);
464+
Yii::debug('Search-Filter: ' . $searchFilter . " | BaseDN: " . $baseDN, __METHOD__);
461465

462466
$result = ldap_read($this->_l, '', '(objectClass=*)', ['supportedControl']);
463467
$supControls = ldap_get_entries($this->_l, $result);
464-
Yii::debug("Supported Controls here:", __METHOD__);
465-
Yii::debug($supControls, __METHOD__);
466468

467469
if (empty($this->_singleValuedAttrs) || !isset($this->_singleValuedAttrs[$domain['hostname']])) {
468470
$this->_singleValuedAttrs[$domain['hostname']] = [];
@@ -522,7 +524,7 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
522524
} else {
523525
Yii::error('ldap_search_error: ' . ldap_error($this->_l), __METHOD__);
524526
}
525-
Yii::error("Search query: " . $searchFilter, __METHOD__);
527+
$this->_l = null;
526528
break;
527529
}
528530
ldap_parse_result($this->_l, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
@@ -585,13 +587,11 @@ public function searchUser(?string $searchFor, ?array $attributes = [], ?string
585587
// Empty cookie means last page
586588
} while (!empty($cookie));
587589

588-
// Reset LDAP Link
589-
ldap_close($this->_l);
590-
$this->_l = null;
591-
}
592590

593-
Yii::debug("Result:", __METHOD__);
594-
Yii::debug($return, __METHOD__);
591+
if ($result) {
592+
@ldap_free_result($result);
593+
}
594+
}
595595

596596
if ($this->enableCache) {
597597
Yii::debug("Adding cache entry", __METHOD__);
@@ -730,7 +730,7 @@ public static function SIDtoString($ADsid)
730730
continue;
731731
}
732732
}
733-
Yii::debug('Converted SID to: ' . $sid, __METHOD__);
733+
// Yii::debug('Converted SID to: ' . $sid, __METHOD__);
734734
array_push($results, $sid);
735735
}
736736
return $results;
@@ -740,14 +740,14 @@ private function handleEntry($entry)
740740
{
741741
$newEntry = [];
742742
foreach ($entry as $attr => $value) {
743-
Yii::debug('Processing attribute ' . $attr, __FUNCTION__);
743+
// Yii::debug('Processing attribute ' . $attr, __FUNCTION__);
744744

745745
if (is_int($attr) || $attr == 'objectsid' || $attr == 'sidhistory' || !isset($value['count'])) {
746-
Yii::debug('Skipping...', __FUNCTION__);
746+
// Yii::debug('Skipping...', __FUNCTION__);
747747
continue;
748748
}
749749
$count = $value['count'];
750-
Yii::debug('Count: ' . $count, __FUNCTION__);
750+
// Yii::debug('Count: ' . $count, __FUNCTION__);
751751

752752
if ($count > 1 || !in_array($attr, $this->_singleValuedAttrs[$this->_curDomainHostname] ?? [])) {
753753
unset($value['count']);
@@ -764,4 +764,12 @@ public function getLastError()
764764
return ldap_error($this->_l);
765765
}
766766

767+
public function __destruct()
768+
{
769+
if ($this->_l) {
770+
@ldap_close($this->_l);
771+
$this->_l = null;
772+
}
773+
}
774+
767775
}

0 commit comments

Comments
 (0)