@@ -7,6 +7,7 @@ use App\Services\SyncDirectoryUsers;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class SyncActiveDirectory extends Command
|
||||
@@ -18,34 +19,48 @@ class SyncActiveDirectory extends Command
|
||||
|
||||
public function handle(SyncDirectoryUsers $sync): int
|
||||
{
|
||||
$started = hrtime(true);
|
||||
$context = [
|
||||
'run_id' => (string) Str::uuid(),
|
||||
'dry_run' => (bool) $this->option('dry-run'),
|
||||
'deactivate_missing' => (bool) $this->option('deactivate-missing'),
|
||||
];
|
||||
|
||||
$duration = fn (): int => (int) ((hrtime(true) - $started) / 1_000_000);
|
||||
Log::info('Active Directory sync started.', $context);
|
||||
|
||||
try {
|
||||
$lock = Cache::lock('ad:sync', 3600);
|
||||
if (! $lock->get()) {
|
||||
throw new DirectorySyncRejected('A directory sync is already running.');
|
||||
}
|
||||
try {
|
||||
$counts = $sync->run($context['dry_run'], $context['deactivate_missing']);
|
||||
$result = $sync->run($context['dry_run'], $context['deactivate_missing']);
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
} catch (DirectorySyncRejected $exception) {
|
||||
Log::warning('Active Directory sync rejected.', $context + ['reason' => $exception->getMessage()]);
|
||||
Log::warning('Active Directory sync rejected.', $context + ['reason' => $exception->getMessage(), 'duration_ms' => $duration()]);
|
||||
$this->error($exception->getMessage());
|
||||
|
||||
return self::FAILURE;
|
||||
} catch (Throwable $exception) {
|
||||
Log::error('Active Directory sync failed.', $context + ['exception_type' => $exception::class]);
|
||||
Log::error('Active Directory sync failed.', $context + [
|
||||
'exception_type' => $exception::class,
|
||||
'exception_code' => $exception->getCode(),
|
||||
'duration_ms' => $duration(),
|
||||
]);
|
||||
$this->error('Sync stopped due to a directory or database error.');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
Log::info('Active Directory sync completed.', $context + $counts);
|
||||
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()]);
|
||||
$summary = [];
|
||||
foreach ($counts as $key => $value) {
|
||||
$summary[] = "$key=$value";
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user