1

使用 adldap-laravel 和 Laravel 5.8。

我正在获得基于 LDAP 组的权限。我可以使用以下方法检查用户是否属于组:($user->ldap->inGroup('Accounts');返回布尔值)

但是,该方法也接受array,但似乎是“AND”搜索,而不是“ANY”。

所以我写了这个:

/**
     * 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']);

但是需要很长时间才能检查。

有谁知道解决我的问题的改进方法?

4

1 回答 1

1

是的,可以通过 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. 算法/过程是相同的。

于 2019-07-02T14:53:18.323 回答