104 lines
4.3 KiB
Plaintext
104 lines
4.3 KiB
Plaintext
<?php
|
|
|
|
namespace Modules\ActiveDirectory\Services;
|
|
|
|
use LdapRecord\LdapInterface;
|
|
use LdapRecord\Models\ActiveDirectory\User;
|
|
use Modules\ActiveDirectory\Constants\AccountControl;
|
|
use RuntimeException;
|
|
|
|
class ActiveDirectory
|
|
{
|
|
private const ATTRIBUTES = ['objectguid', 'mail', 'displayname', 'cn', 'useraccountcontrol'];
|
|
|
|
private const DIAGNOSTIC_ATTRIBUTES = [
|
|
'distinguishedname', 'samaccountname', 'userprincipalname', 'givenname', 'sn',
|
|
'objectclass', 'memberof', 'whencreated', 'whenchanged', 'pwdlastset', 'lastlogontimestamp',
|
|
];
|
|
|
|
public static function baseDn(): string
|
|
{
|
|
$connection = config('ldap.default');
|
|
|
|
return trim((string) (config('ad-sync.base-dn') ?: config("ldap.connections.{$connection}.base_dn")));
|
|
}
|
|
|
|
/** @return list<array{guid: ?string, email: string, name: string, enabled: bool}> */
|
|
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))', $attributes, controls: [[
|
|
'oid' => LDAP_CONTROL_PAGEDRESULTS,
|
|
'iscritical' => true,
|
|
'value' => ['size' => 500, 'cookie' => $cookie],
|
|
]]);
|
|
|
|
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),
|
|
];
|
|
}
|
|
}
|