在 AD LDAP 中查找用户
Find user in AD LDAP
我遇到了为 LDAP 构建过滤器的问题。
实际问题前的一些注释:
Фамилия = Family Name (will be presented as FamilyName)
Имя = Name (will be presented as Name)
Отчество = Patronymic (will be presented as Patronymic)
我有一个用户在 AD 中有以下信息:
CN=Фамилия Имя Отчество
sn=FamilyName
givenname=Name
我想提供以下功能:
- 用户可以在网站的字段中输入
Name FamilyName
,这将导致 Name FamilyName
和 FamilyName Name
' 的 2 个过滤器数组
- 用户可以输入
Имя Фамилия
,搜索应该从 (&(sn=)(givenname=)) 切换到 (cn=)
目前我有以下代码来完成第一个选项:
/**
* Generate search terms
* @param string $query
* @return LDAPSearcher
*/
protected function generateSearchTerms(string $query) : self {
$this->searchTerms = [];
$explode = explode(' ', $query);
$combinations = [];
array_combinations($explode, $combinations);
foreach($combinations as $index => $combination) {
if (false !== strpos($combination, ' ')) {
[$firstName, $lastName] = explode(' ', $combination);
$this->searchTerms[] = [
'sn' => $lastName,
'cn' => $combination,
'givenname' => $firstName,
'filter' => '(&(sn=' . $firstName . ')(givenname=' . $lastName . '))'
];
}
}
return $this;
}
当用户使用 his/her 名字和姓氏的拉丁表示时它工作得很好,但是当我想切换到使用 CN 时,我不知道该怎么做。我已经为过滤器尝试了以下代码,但它显示过滤器不正确:
((&(sn=' . $firstName . ')(givenname=' . $lastName . '))|(cn=' . $combination . '*))
P.S. 我将哪个变量分配给 SN 或 GivenName 并不重要,因为无论如何组合都会匹配正确的用户,我 运行 每个用户最多搜索 3 次以确保找到正确的(只是为了消除为变量分配正确值的答案的可能性)
P.P.S.组合是使用下面的一段代码生成的
if (! function_exists('array_combinations')) {
function array_combinations(array $source, array &$target, ?string $tempString = null) {
if ($tempString !== null) {
$target[] = $tempString;
}
$size = \count($source);
for ($i = 0; $i < $size; $i++) {
$copy = $source;
$element = array_splice($copy, $i, 1);
$tmp = null;
if ($tempString !== null) {
$tmp = $tempString . ' ' . $element[0];
} else {
$tmp = $element[0];
}
if (\count($copy) > 0) {
array_combinations($copy, $target, $tmp);
} else {
$target[] = $tmp;
}
}
}
}
您的查询确实无效。
在 LDAP 查询语法中,"this OR that" 条件写为 (|(this)(that))
。但是你把|
放在你的条件之间了。它需要在前面。它应该看起来像这样:
(|(&(sn=' . $firstName . ')(givenname=' . $lastName . '))(cn=' . $combination . '*))
我遇到了为 LDAP 构建过滤器的问题。
实际问题前的一些注释:
Фамилия = Family Name (will be presented as FamilyName)
Имя = Name (will be presented as Name)
Отчество = Patronymic (will be presented as Patronymic)
我有一个用户在 AD 中有以下信息:
CN=Фамилия Имя Отчество
sn=FamilyName
givenname=Name
我想提供以下功能:
- 用户可以在网站的字段中输入
Name FamilyName
,这将导致Name FamilyName
和FamilyName Name
' 的 2 个过滤器数组
- 用户可以输入
Имя Фамилия
,搜索应该从 (&(sn=)(givenname=)) 切换到 (cn=)
目前我有以下代码来完成第一个选项:
/**
* Generate search terms
* @param string $query
* @return LDAPSearcher
*/
protected function generateSearchTerms(string $query) : self {
$this->searchTerms = [];
$explode = explode(' ', $query);
$combinations = [];
array_combinations($explode, $combinations);
foreach($combinations as $index => $combination) {
if (false !== strpos($combination, ' ')) {
[$firstName, $lastName] = explode(' ', $combination);
$this->searchTerms[] = [
'sn' => $lastName,
'cn' => $combination,
'givenname' => $firstName,
'filter' => '(&(sn=' . $firstName . ')(givenname=' . $lastName . '))'
];
}
}
return $this;
}
当用户使用 his/her 名字和姓氏的拉丁表示时它工作得很好,但是当我想切换到使用 CN 时,我不知道该怎么做。我已经为过滤器尝试了以下代码,但它显示过滤器不正确:
((&(sn=' . $firstName . ')(givenname=' . $lastName . '))|(cn=' . $combination . '*))
P.S. 我将哪个变量分配给 SN 或 GivenName 并不重要,因为无论如何组合都会匹配正确的用户,我 运行 每个用户最多搜索 3 次以确保找到正确的(只是为了消除为变量分配正确值的答案的可能性)
P.P.S.组合是使用下面的一段代码生成的
if (! function_exists('array_combinations')) {
function array_combinations(array $source, array &$target, ?string $tempString = null) {
if ($tempString !== null) {
$target[] = $tempString;
}
$size = \count($source);
for ($i = 0; $i < $size; $i++) {
$copy = $source;
$element = array_splice($copy, $i, 1);
$tmp = null;
if ($tempString !== null) {
$tmp = $tempString . ' ' . $element[0];
} else {
$tmp = $element[0];
}
if (\count($copy) > 0) {
array_combinations($copy, $target, $tmp);
} else {
$target[] = $tmp;
}
}
}
}
您的查询确实无效。
在 LDAP 查询语法中,"this OR that" 条件写为 (|(this)(that))
。但是你把|
放在你的条件之间了。它需要在前面。它应该看起来像这样:
(|(&(sn=' . $firstName . ')(givenname=' . $lastName . '))(cn=' . $combination . '*))