使用adldap-laravel和laravel 5.8。
我根据LDAP组获得了权限。我可以检查用户是否是组的一部分: $user->ldap->inGroup('Accounts');
(返回bool(
然而,该方法也接受array
,但似乎是"one_answers"搜索",而不是"任何"。
所以我已经写了这篇文章:
/**
* LDAP helper, to see if the user is in an AD group.
* Iterate through and break when a match is found.
*
* @param mixed $input
* @return bool
*/
public function isInGroup($input)
{
if (is_string($input)) {
$input[] = $input;
}
foreach ($input as $group)
if ($this->ldap->inGroup($group)) {
return true;
}
return false;
}
这样实施: $user->isInGroup(['Accounts', 'Sales', 'Marketing']);
但是,检查需要很长时间。
有人知道解决我的问题的改进方法吗?
是的,可以通过adldap的查询构建器进行。
/**
* LDAP helper, to see if the user is in an AD group.
* Iterate through and break when a match is found.
*
* @param mixed $input
* @return bool
*/
public function isInGroup($input){
if (is_string($input)) {
$input[] = $input;
}
$counter = 0;
$groupQuery = $this->ldap->search()->groups();
foreach ($input as $group) {
if ($counter == 0) {
$groupQuery->where('samaccountname', '=', $group);
} else {
$groupQuery->orWhere('samaccountname', '=', $group);
}
$counter++;
}
return $groupQuery->get()->count();
}
此 SAMACCOUNTNAME 可能是您的LDAP提供商的字段名称。如果有任何语法或方法名称错误,我将无法对其进行测试。无论如何,您会从Adldap Provider class
中找到相同的方法。算法/过程相同。