updates for active directory
tests / ci (push) Canceled after 0s

This commit is contained in:
Mark Gregory
2026-11-11 16:47:59 +00:00
parent 2dcdb3790e
commit 4ebcd12565
3 changed files with 101 additions and 32 deletions
+34 -12
View File
@@ -10,7 +10,12 @@ class SyncDirectoryUsers
{
public function __construct(private ActiveDirectory $directory) {}
/** @return array{created: int, updated: int, unchanged: int, deactivated: int, reactivated: int} */
/**
* @return array{
* counts: array{created: int, updated: int, unchanged: int, deactivated: int, reactivated: int},
* 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): array
{
$entries = $this->snapshot();
@@ -38,22 +43,28 @@ class SyncDirectoryUsers
}
if (! $user->exists) {
$counts['created']++;
$action = 'created';
$user->ad_disabled_at = $entry['enabled'] ? null : now();
} elseif (! $entry['enabled'] && $user->ad_disabled_at === null) {
$counts['deactivated']++;
$action = 'deactivated';
$this->disable($user);
} elseif ($entry['enabled'] && $user->ad_disabled_at !== null) {
$counts['reactivated']++;
$action = 'reactivated';
$user->ad_disabled_at = null;
} elseif ($user->isDirty()) {
$counts['updated']++;
$action = 'updated';
} else {
$counts['unchanged']++;
$action = 'unchanged';
}
if ($user->isDirty()) {
$changes[] = $user;
$counts[$action]++;
if ($action !== 'unchanged') {
$reason = match (true) {
! $entry['enabled'] && in_array($action, ['created', 'deactivated'], true) => 'directory_disabled',
$action === 'reactivated' => 'directory_enabled',
default => null,
};
$changes[] = [$user, $action, $reason];
}
}
@@ -64,7 +75,7 @@ class SyncDirectoryUsers
if (! isset($entries[$user->ad_guid])) {
$counts['deactivated']++;
$this->disable($user);
$changes[] = $user;
$changes[] = [$user, 'deactivated', 'missing_from_snapshot'];
}
}
}
@@ -73,16 +84,27 @@ class SyncDirectoryUsers
throw new DirectorySyncRejected('Deactivation limit exceeded.');
}
if (! $dryRun) {
foreach ($changes as $user) {
$audit = [];
foreach ($changes as [$user, $action, $reason]) {
$fields = array_values(array_intersect(['name', 'email', 'email_verified_at', 'ad_disabled_at'], array_keys($user->getDirty())));
if (! $dryRun) {
if (! $user->exists) {
$user->password = Str::random(64);
}
$user->save();
}
$audit[] = [
'action' => $action,
'user_id' => $user->exists ? $user->id : null,
'ad_guid' => $user->ad_guid,
'disabled' => $user->ad_disabled_at !== null,
'changed_fields' => $fields,
'reason' => $reason,
];
}
return $counts;
return ['counts' => $counts, 'changes' => $audit];
}, attempts: 3);
}