This commit is contained in:
@@ -12,18 +12,20 @@ class SyncDirectoryUsers
|
||||
|
||||
/**
|
||||
* @return array{
|
||||
* missing_fingerprint: string,
|
||||
* 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
|
||||
public function run(bool $dryRun, bool $deactivateMissing, ?string $reviewedMissing = null): array
|
||||
{
|
||||
$entries = $this->snapshot();
|
||||
$source = $this->source();
|
||||
|
||||
return DB::transaction(function () use ($entries, $source, $dryRun, $deactivateMissing): array {
|
||||
return DB::transaction(function () use ($entries, $source, $dryRun, $deactivateMissing, $reviewedMissing): array {
|
||||
$counts = ['created' => 0, 'updated' => 0, 'unchanged' => 0, 'deactivated' => 0, 'reactivated' => 0];
|
||||
$changes = [];
|
||||
$missingGuids = [];
|
||||
|
||||
foreach ($entries as $guid => $entry) {
|
||||
$user = User::where('ad_guid', $guid)->lockForUpdate()->first();
|
||||
@@ -44,13 +46,18 @@ class SyncDirectoryUsers
|
||||
|
||||
if (! $user->exists) {
|
||||
$action = 'created';
|
||||
$user->ad_disabled_at = $entry['enabled'] ? null : now();
|
||||
if (! $entry['enabled']) {
|
||||
$this->disable($user);
|
||||
}
|
||||
} elseif (! $entry['enabled'] && $user->ad_disabled_at === null) {
|
||||
$action = 'deactivated';
|
||||
$this->disable($user);
|
||||
} elseif ($entry['enabled'] && $user->ad_disabled_at !== null) {
|
||||
$action = 'reactivated';
|
||||
$user->ad_disabled_at = null;
|
||||
$user->forceFill(['ad_disabled_at' => null, 'deleted_at' => null, 'ad_disabled_reason' => null]);
|
||||
} elseif (! $entry['enabled'] && $user->ad_disabled_reason !== 'directory_disabled') {
|
||||
$action = 'updated';
|
||||
$user->ad_disabled_reason = 'directory_disabled';
|
||||
} elseif ($user->isDirty()) {
|
||||
$action = 'updated';
|
||||
} else {
|
||||
@@ -74,7 +81,8 @@ class SyncDirectoryUsers
|
||||
foreach ($missing as $user) {
|
||||
if (! isset($entries[$user->ad_guid])) {
|
||||
$counts['deactivated']++;
|
||||
$this->disable($user);
|
||||
$this->disable($user, 'missing_from_snapshot');
|
||||
$missingGuids[] = $user->ad_guid;
|
||||
$changes[] = [$user, 'deactivated', 'missing_from_snapshot'];
|
||||
}
|
||||
}
|
||||
@@ -84,9 +92,21 @@ class SyncDirectoryUsers
|
||||
throw new DirectorySyncRejected('Deactivation limit exceeded.');
|
||||
}
|
||||
|
||||
sort($missingGuids);
|
||||
$fingerprint = hash('sha256', $source.':'.implode(',', $missingGuids));
|
||||
if ($reviewedMissing !== null) {
|
||||
if ($missingGuids === [] || ! hash_equals($fingerprint, $reviewedMissing)) {
|
||||
throw new DirectorySyncRejected('Missing users changed since the preview. Review the list again.');
|
||||
}
|
||||
|
||||
$changes = array_values(array_filter($changes, fn (array $change): bool => $change[2] === 'missing_from_snapshot'));
|
||||
$counts = array_fill_keys(array_keys($counts), 0);
|
||||
$counts['deactivated'] = count($changes);
|
||||
}
|
||||
|
||||
$audit = [];
|
||||
foreach ($changes as [$user, $action, $reason]) {
|
||||
$fields = array_values(array_intersect(['name', 'email', 'email_verified_at', 'ad_disabled_at'], array_keys($user->getDirty())));
|
||||
$fields = array_values(array_intersect(['name', 'email', 'email_verified_at', 'ad_disabled_at', 'deleted_at', 'ad_disabled_reason'], array_keys($user->getDirty())));
|
||||
if (! $dryRun) {
|
||||
if (! $user->exists) {
|
||||
$user->password = Str::random(64);
|
||||
@@ -104,13 +124,14 @@ class SyncDirectoryUsers
|
||||
];
|
||||
}
|
||||
|
||||
return ['counts' => $counts, 'changes' => $audit];
|
||||
return ['counts' => $counts, 'changes' => $audit, 'missing_fingerprint' => $fingerprint];
|
||||
}, attempts: 3);
|
||||
}
|
||||
|
||||
private function disable(User $user): void
|
||||
private function disable(User $user, string $reason = 'directory_disabled'): void
|
||||
{
|
||||
$user->forceFill(['ad_disabled_at' => now(), 'remember_token' => null]);
|
||||
$disabledAt = now();
|
||||
$user->forceFill(['ad_disabled_at' => $disabledAt, 'deleted_at' => $disabledAt, 'ad_disabled_reason' => $reason, 'remember_token' => null]);
|
||||
}
|
||||
|
||||
private function source(): string
|
||||
|
||||
Reference in New Issue
Block a user