updates for active directory now with docs
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\ActiveDirectory\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\ActiveDirectory\Services\DirectorySyncHistory;
|
||||
use Modules\ActiveDirectory\Services\DirectorySyncRejected;
|
||||
use Modules\ActiveDirectory\Services\SyncDirectoryUsers;
|
||||
use Throwable;
|
||||
|
||||
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}
|
||||
{--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, DirectorySyncHistory $history): int
|
||||
{
|
||||
$started = hrtime(true);
|
||||
$context = [
|
||||
'run_id' => (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);
|
||||
|
||||
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']);
|
||||
} 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.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
foreach ($result['changes'] as $change) {
|
||||
Log::info('Active Directory sync user change.', $context + $change);
|
||||
}
|
||||
|
||||
$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";
|
||||
}
|
||||
$this->info(($context['dry_run'] ? 'Dry run: ' : '').implode(' ', $summary));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user