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

This commit is contained in:
Mark Gregory
2026-11-11 17:15:42 +00:00
parent 90f8655595
commit 9c3c82e7c6
9 changed files with 121 additions and 41 deletions
+16 -3
View File
@@ -2,6 +2,7 @@
namespace App\Console\Commands;
use App\Services\DirectorySyncHistory;
use App\Services\DirectorySyncRejected;
use App\Services\SyncDirectoryUsers;
use Illuminate\Console\Command;
@@ -13,11 +14,12 @@ use Throwable;
class SyncActiveDirectory extends Command
{
protected $signature = 'ad:sync {--dry-run : Preview changes without saving users}
{--deactivate-missing : Disable managed users absent from the snapshot}';
{--deactivate-missing : Disable managed users absent from the snapshot}
{--reviewed-missing= : Apply only the missing users from the reviewed preview}';
protected $description = 'Synchronize local user records from Active Directory';
public function handle(SyncDirectoryUsers $sync): int
public function handle(SyncDirectoryUsers $sync, DirectorySyncHistory $history): int
{
$started = hrtime(true);
$context = [
@@ -27,20 +29,29 @@ class SyncActiveDirectory extends Command
];
$duration = fn (): int => (int) ((hrtime(true) - $started) / 1_000_000);
$review = $this->option('reviewed-missing');
$record = fn (string $status, ?array $counts = null, ?string $failure = null) => $history->record(
$context['run_id'], $status, $context['dry_run'], $context['deactivate_missing'], $duration(), $counts, $failure,
);
Log::info('Active Directory sync started.', $context);
try {
if ($review !== null && (! $context['deactivate_missing'] || $context['dry_run'])) {
throw new DirectorySyncRejected('A reviewed apply requires a live missing-user run.');
}
$lock = Cache::lock('ad:sync', 3600);
if (! $lock->get()) {
throw new DirectorySyncRejected('A directory sync is already running.');
}
try {
$result = $sync->run($context['dry_run'], $context['deactivate_missing']);
$result = $sync->run($context['dry_run'], $context['deactivate_missing'], $review);
} finally {
$lock->release();
}
} catch (DirectorySyncRejected $exception) {
Log::warning('Active Directory sync rejected.', $context + ['reason' => $exception->getMessage(), 'duration_ms' => $duration()]);
$record('rejected', failure: $exception->getMessage());
$this->error($exception->getMessage());
return self::FAILURE;
@@ -50,6 +61,7 @@ class SyncActiveDirectory extends Command
'exception_code' => $exception->getCode(),
'duration_ms' => $duration(),
]);
$record('failed', failure: 'Directory or database error');
$this->error('Sync stopped due to a directory or database error.');
return self::FAILURE;
@@ -61,6 +73,7 @@ class SyncActiveDirectory extends Command
$counts = $result['counts'];
Log::info('Active Directory sync completed.', $context + $counts + ['duration_ms' => $duration()]);
$record($review === null ? 'completed' : 'review_applied', $counts);
$summary = [];
foreach ($counts as $key => $value) {
$summary[] = "$key=$value";