updates for active directory now with docs

This commit is contained in:
Mark Gregory
2026-11-12 08:28:46 +00:00
parent 7166dd33f9
commit 1c57e92780
6 changed files with 225 additions and 9 deletions
@@ -6,9 +6,11 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Modules\ActiveDirectory\Services\ActiveDirectory;
use Modules\ActiveDirectory\Services\DirectorySyncHistory;
use Modules\ActiveDirectory\Services\DirectorySyncRejected;
use Modules\ActiveDirectory\Services\SyncDirectoryUsers;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class SyncActiveDirectory extends Command
@@ -16,6 +18,8 @@ class SyncActiveDirectory extends Command
protected $signature = 'ad:sync {--email : Match directory accounts against existing Laravel user emails}
{--create-missing : Explicitly create users absent from Laravel}
{--dry-run : Preview changes without saving users}
{--debug : Print the LDAP account attributes returned by the sync search}
{--d|dump-ad : Print AD account properties returned by LDAP (-dd)}
{--deactivate-missing : Disable managed users absent from the snapshot}
{--reviewed-missing= : Apply only the missing users from the reviewed preview}';
@@ -38,6 +42,7 @@ class SyncActiveDirectory extends Command
$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'])) {
@@ -49,7 +54,15 @@ class SyncActiveDirectory extends Command
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']);
$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();
}
@@ -67,17 +80,31 @@ class SyncActiveDirectory extends Command
]);
$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";
@@ -86,4 +113,29 @@ class SyncActiveDirectory extends Command
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);
}
}
}