*/ public function users(bool $includeProperties = false): array { $baseDn = self::baseDn(); if ($baseDn === '') { throw new RuntimeException('An LDAP search base is required.'); } return User::resolveConnection(config('ldap.default'))->run(function (LdapInterface $ldap) use ($baseDn, $includeProperties): array { $attributes = $includeProperties ? [...self::ATTRIBUTES, ...self::DIAGNOSTIC_ATTRIBUTES] : self::ATTRIBUTES; $entries = []; $cookie = ''; do { $result = $ldap->search($baseDn, '(&(objectCategory=person)(objectClass=user)(mail=*))', $attributes, controls: [[ 'oid' => LDAP_CONTROL_PAGEDRESULTS, 'iscritical' => true, 'value' => ['size' => 500, 'cookie' => $cookie], ]]); if ($result === false) { throw new RuntimeException(sprintf( 'Active Directory LDAP paged search failed (LDAP error %s: %s).', (string) $ldap->errNo(), ldap_err2str((int) $ldap->errNo()) )); } try { $response = $ldap->parseResult($result); $next = $response ? ($response->controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? null) : null; if (! $response || $response->failed() || $response->referrals || ! is_string($next)) { throw new RuntimeException('LDAP returned an incomplete page.'); } $page = $ldap->getEntries($result); $count = $page['count'] ?? null; if (! is_int($count) || $count !== count($page) - 1 || ($count === 0 && $next !== '')) { throw new RuntimeException('LDAP returned invalid page entries.'); } for ($index = 0; $index < $count; $index++) { $user = (new User)->setRawAttributes($page[$index]); $entry = $this->entry($user); if ($includeProperties) { $entry['ad_properties'] = ['objectguid' => $entry['guid']]; foreach (array_diff($attributes, ['objectguid']) as $attribute) { $values = $user->getAttribute($attribute); if ($values !== null) { $entry['ad_properties'][$attribute] = $values; } } } $entries[] = $entry; } $cookie = $next; } finally { $ldap->freeResult($result); } } while ($cookie !== ''); return $entries; }); } /** @return array{guid: ?string, email: string, name: string, enabled: bool} */ private function entry(User $user): array { $flags = filter_var($user->getFirstAttribute('useraccountcontrol'), FILTER_VALIDATE_INT, [ 'options' => ['min_range' => 0], ]); if ($flags === false) { throw new RuntimeException('Missing or invalid userAccountControl.'); } return [ 'guid' => $user->getConvertedGuid(), 'email' => (string) $user->getFirstAttribute('mail'), 'name' => (string) ($user->getFirstAttribute('displayname') ?: $user->getFirstAttribute('cn')), 'enabled' => ($flags & AccountControl::ACCOUNTDISABLE) === 0, 'account_control' => $flags, 'account_flags' => AccountControl::flags($flags), ]; } }