(string) Str::uuid(), 'dry_run' => (bool) $this->option('dry-run'), 'deactivate_missing' => (bool) $this->option('deactivate-missing'), 'email' => (bool) $this->option('email'), 'create_missing' => (bool) $this->option('create-missing'), ]; $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); $this->writeDetails('Sync options', $context, OutputInterface::VERBOSITY_VERBOSE); 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'], $review, $context['email'], $context['create_missing'], ($this->option('debug') || $this->option('dump-ad')) ? $this->dumpDirectory(...) : null, includeProperties: (bool) $this->option('dump-ad'), ); } 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; } catch (Throwable $exception) { Log::error('Active Directory sync failed.', $context + [ 'exception_type' => $exception::class, '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.'); $this->writeDetails('Error', [ 'type' => $exception::class, 'code' => $exception->getCode(), ], OutputInterface::VERBOSITY_DEBUG); return self::FAILURE; } foreach ($result['changes'] as $change) { Log::info('Active Directory sync user change.', $context + $change); if ($this->output->isDebug()) { $this->writeDetails('User change', $change, OutputInterface::VERBOSITY_DEBUG); } else { $this->writeDetails('User change', [ 'action' => $change['action'], 'ad_guid' => $change['ad_guid'], 'changed_fields' => $change['changed_fields'], ], OutputInterface::VERBOSITY_VERY_VERBOSE); } } $counts = $result['counts']; Log::info('Active Directory sync completed.', $context + $counts + ['duration_ms' => $duration()]); $record($review === null ? 'completed' : 'review_applied', $counts); $this->writeDetails('Run duration', ['milliseconds' => $duration()], OutputInterface::VERBOSITY_VERBOSE); $summary = []; foreach ($counts as $key => $value) { $summary[] = "$key=$value"; } $this->info(($context['dry_run'] ? 'Dry run: ' : '').implode(' ', $summary)); return self::SUCCESS; } private function dumpDirectory(array $users): void { $debug = $this->option('debug'); if ($debug) { $this->writeDetails('AD snapshot', ['base_dn' => ActiveDirectory::baseDn(), 'accounts' => count($users)]); } foreach ($users as $user) { if ($this->option('dump-ad')) { $this->writeDetails('AD properties', $user['ad_properties']); } if ($debug) { unset($user['ad_properties']); $this->writeDetails('AD account', $user); } } } private function writeDetails(string $label, array $details, int $verbosity = OutputInterface::VERBOSITY_NORMAL): void { if ($this->output->getVerbosity() >= $verbosity) { $this->output->writeln($label.' '.json_encode($details, JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR), OutputInterface::OUTPUT_RAW); } } }