updates for active directory now with docs
This commit is contained in:
@@ -11,6 +11,11 @@ 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');
|
||||
@@ -19,19 +24,20 @@ class ActiveDirectory
|
||||
}
|
||||
|
||||
/** @return list<array{guid: ?string, email: string, name: string, enabled: bool}> */
|
||||
public function users(): array
|
||||
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): array {
|
||||
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))', self::ATTRIBUTES, controls: [[
|
||||
$result = $ldap->search($baseDn, '(&(objectCategory=person)(objectClass=user))', $attributes, controls: [[
|
||||
'oid' => LDAP_CONTROL_PAGEDRESULTS,
|
||||
'iscritical' => true,
|
||||
'value' => ['size' => 500, 'cookie' => $cookie],
|
||||
@@ -51,7 +57,18 @@ class ActiveDirectory
|
||||
}
|
||||
|
||||
for ($index = 0; $index < $count; $index++) {
|
||||
$entries[] = $this->entry((new User)->setRawAttributes($page[$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;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Modules\ActiveDirectory\Services;
|
||||
|
||||
use App\Models\User;
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
@@ -17,9 +18,11 @@ class SyncDirectoryUsers
|
||||
* changes: list<array{action: string, user_id: ?int, ad_guid: ?string, disabled: bool, changed_fields: list<string>, reason: ?string}>
|
||||
* }
|
||||
*/
|
||||
public function run(bool $dryRun, bool $deactivateMissing, ?string $reviewedMissing = null, bool $matchEmail = false, bool $createMissing = false): array
|
||||
public function run(bool $dryRun, bool $deactivateMissing, ?string $reviewedMissing = null, bool $matchEmail = false, bool $createMissing = false, ?Closure $inspectDirectory = null, bool $includeProperties = false): array
|
||||
{
|
||||
$entries = $this->snapshot();
|
||||
$directoryUsers = $includeProperties ? $this->directory->users(true) : $this->directory->users();
|
||||
$inspectDirectory?->__invoke($directoryUsers);
|
||||
$entries = $this->snapshot($directoryUsers);
|
||||
$source = $this->source();
|
||||
|
||||
return DB::transaction(function () use ($entries, $source, $dryRun, $deactivateMissing, $reviewedMissing, $matchEmail, $createMissing): array {
|
||||
@@ -161,11 +164,11 @@ class SyncDirectoryUsers
|
||||
}
|
||||
|
||||
/** @return array<string, array{email: string, name: string, enabled: bool}> */
|
||||
private function snapshot(): array
|
||||
private function snapshot(array $directoryUsers): array
|
||||
{
|
||||
$entries = [];
|
||||
$emails = [];
|
||||
foreach ($this->directory->users() as $entry) {
|
||||
foreach ($directoryUsers as $entry) {
|
||||
$guid = strtolower((string) $entry['guid']);
|
||||
$email = strtolower(trim($entry['email']));
|
||||
$name = trim($entry['name']);
|
||||
|
||||
Reference in New Issue
Block a user