Skip to content

Commit 08927a1

Browse files
author
robin.kluth
committed
1.1.16
* New option to let `searchUser` filter output by checking `sidHistory`
1 parent 238a8a5 commit 08927a1

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This extensions adds a simple LDAP-Auth mechanism for your yii2 application
1212
* User login via LDAP
1313
* Read self defined LDAP attributes
1414
* Domain autodetection based on IPFilter.
15+
* Filter out results by checking every results `sidHistory`
1516

1617
## Installation
1718

@@ -30,6 +31,7 @@ Either you use it as standalone or add this as component:
3031
'components' => [
3132
'ldap' => [
3233
'class' => 'commifreak\yii2\LdapAuth',
34+
'filterBySidhistory' => false, // Filter by checking sidHistory?
3335
'domains' => [
3436
['name' => 'Domain1', 'hostname' => 'domain1.tld', 'autodetectIps' => ['172.31.0.0/16', '192.168.178.0/24', '127.0.0.1'], 'baseDn' => 'DC=Domain1,DC=tld', 'publicSearchUser' => 'example@domain', 'publicSearchUserPassword' => 'secret'],
3537
['name' => 'Domain2', 'hostname' => '192.168.178.14', 'autodetectIps' => ['192.168.178.55'], 'baseDn' => 'DC=Domain2,DC=tld', 'publicSearchUser' => 'example@domain', 'publicSearchUserPassword' => 'secret'],
@@ -40,7 +42,7 @@ Either you use it as standalone or add this as component:
4042
]
4143
```
4244

43-
You can omit `autodetectIps` if you dont want Ips for a specific domain.
45+
You can omit `autodetectIps` if you don't want Ips for a specific domain.
4446

4547
__Attention!__ You need to define `baseDn`. This defines the baseDN in where the function will search for the user data!
4648

src/LdapAuth.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ class LdapAuth
3232
],
3333
];
3434

35+
/**
36+
* If false (default) any user search would return the whole result.
37+
* If true, the script checks every users sidHistory and only return results which are newer (migrated).
38+
* A use case for `true`: You have two domains and user "Foo" was copied from Domain 1 to Domain 2 without deleting it from Domain 1 - now you have 2 results for a search "Foo", but the entry in Domain 2 has a set "sidHistory" with its sid from Domain 1.
39+
* Setting this tp true will filter out the "Foo" from Domain 1, since its sid is listed in the Domain 2 entry of it.
40+
*
41+
* @see https://docs.microsoft.com/en-us/windows/win32/adschema/a-sidhistory
42+
* @see https://ldapwiki.com/wiki/SIDHistory
43+
* @var bool
44+
*/
45+
public $filterBySidhistory = false;
46+
3547
private $_ldapBaseDn;
3648
private $_l;
3749
private $_username;
@@ -268,12 +280,34 @@ public function searchUser($searchFor, $attributes = "", $searchFilter = "", $au
268280
}
269281
$sid = self::SIDtoString($entry['objectsid'])[0];
270282
$sidHistory = isset($entry['sidhistory']) ? self::SIDtoString($entry['sidhistory']) : null;
283+
284+
285+
if ($this->filterBySidhistory) {
286+
// Check if this user is maybe already listed in the results - ifo so, determine which one is newer
287+
foreach ($return as $_sid => $_data) {
288+
if (!empty($_data['sidhistory']) && in_array($sid, $_data['sidhistory'])) {
289+
Yii::debug('This user is listed in another users history - skipping');
290+
continue 2;
291+
}
292+
}
293+
294+
if ($sidHistory) {
295+
foreach ($sidHistory as $item) {
296+
if (array_key_exists($item, $return)) {
297+
Yii::debug('User already exists with its sidhistory in results! Unsetting the old entry...');
298+
unset($return[$item]);
299+
}
300+
}
301+
}
302+
}
303+
304+
271305
$additionalData = ['sid' => $sid, 'sidhistory' => $sidHistory, 'dn' => $entry['dn'], 'domainKey' => $i];
272306
if (count($this->domains) > 1) {
273307
// Enable domainName output if more than one domains configured
274308
$additionalData['domainName'] = $this->domains[$i]['name'];
275309
}
276-
array_push($return, array_merge($additionalData, self::handleEntry($entry)));
310+
$return[$sid] = array_merge($additionalData, self::handleEntry($entry));
277311
}
278312
}
279313
$i++;

0 commit comments

Comments
 (0)