updates for active directory now with docs
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Services\ActiveDirectory;
|
||||
use Illuminate\Console\Application as Artisan;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
@@ -12,6 +12,7 @@ use Laravel\Passkeys\Passkeys;
|
||||
use LdapRecord\Connection;
|
||||
use LdapRecord\LdapInterface;
|
||||
use LdapRecord\LdapResultResponse;
|
||||
use Modules\ActiveDirectory\Services\ActiveDirectory;
|
||||
|
||||
function directoryEntry(string $email = '[email protected]', string $guid = '00112233-4455-6677-8899-aabbccddeeff', bool $enabled = true): array
|
||||
{
|
||||
@@ -25,15 +26,15 @@ function directorySnapshot(array $entries): void
|
||||
|
||||
it('imports by canonical GUID and retains the account when its email changes', function () {
|
||||
directorySnapshot([directoryEntry(guid: strtoupper(directoryEntry()['guid']))]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
$user = User::sole();
|
||||
expect($user->ad_guid)->toBe(directoryEntry()['guid'])
|
||||
->and($user->email_verified_at)->toBeNull();
|
||||
|
||||
directorySnapshot([directoryEntry('[email protected]')]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
directorySnapshot([directoryEntry('[email protected]')]);
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('unchanged=1')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->expectsOutputToContain('unchanged=1')->assertSuccessful();
|
||||
|
||||
expect(User::sole()->id)->toBe($user->id)
|
||||
->and($user->refresh()->email)->toBe('[email protected]');
|
||||
@@ -42,14 +43,14 @@ it('imports by canonical GUID and retains the account when its email changes', f
|
||||
it('refuses to claim a local account by case-insensitive email and applies no partial changes', function () {
|
||||
$local = User::factory()->create(['email' => '[email protected]']);
|
||||
directorySnapshot([directoryEntry('[email protected]', '11111111-1111-1111-1111-111111111111'), directoryEntry()]);
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('email collision')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing')->expectsOutputToContain('email collision')->assertFailed();
|
||||
expect(User::sole()->id)->toBe($local->id)
|
||||
->and($local->refresh()->ad_guid)->toBeNull();
|
||||
});
|
||||
|
||||
it('rejects unsafe snapshots before writing', function (array $entries) {
|
||||
directorySnapshot($entries);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing --deactivate-missing')->assertFailed();
|
||||
expect(User::count())->toBe(0);
|
||||
})->with([
|
||||
'empty' => fn () => [[]],
|
||||
@@ -60,27 +61,27 @@ it('rejects unsafe snapshots before writing', function (array $entries) {
|
||||
|
||||
it('keeps dry runs read-only and blocks access for an explicitly disabled directory user', function () {
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync --dry-run')->expectsOutputToContain('created=1')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing --dry-run')->expectsOutputToContain('created=1')->assertSuccessful();
|
||||
expect(User::count())->toBe(0);
|
||||
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
$user = User::sole();
|
||||
$user->update(['password' => 'example-password']);
|
||||
directorySnapshot([directoryEntry(enabled: false)]);
|
||||
$this->artisan('ad:sync --dry-run')->expectsOutputToContain('deactivated=1')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing --dry-run')->expectsOutputToContain('deactivated=1')->assertSuccessful();
|
||||
expect($user->refresh()->ad_disabled_at)->toBeNull()
|
||||
->and($user->deleted_at)->toBeNull();
|
||||
|
||||
directorySnapshot([directoryEntry(enabled: false)]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
expect($user->refresh()->ad_disabled_at)->not->toBeNull()
|
||||
->and($user->deleted_at)->toEqual($user->ad_disabled_at);
|
||||
$this->post(route('login.store'), ['email' => $user->email, 'password' => 'example-password']);
|
||||
$this->assertGuest();
|
||||
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
expect($user->refresh()->ad_disabled_at)->toBeNull()
|
||||
->and($user->deleted_at)->toBeNull();
|
||||
});
|
||||
@@ -90,18 +91,18 @@ it('requires explicit bounded missing-user deactivation and preserves local acco
|
||||
$third = directoryEntry('[email protected]', '22222222-2222-2222-2222-222222222222');
|
||||
$local = User::factory()->create();
|
||||
directorySnapshot([directoryEntry(), $second, $third]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
expect(User::whereNotNull('ad_disabled_at')->count())->toBe(0);
|
||||
|
||||
config(['ad-sync.max-deactivations' => 1]);
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing --deactivate-missing')->assertFailed();
|
||||
expect(User::whereNotNull('ad_disabled_at')->count())->toBe(0);
|
||||
|
||||
directorySnapshot([directoryEntry(), $second]);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing --deactivate-missing')->assertSuccessful();
|
||||
expect(User::whereNotNull('ad_disabled_at')->sole()->email)->toBe($third['email'])
|
||||
->and(User::whereNotNull('deleted_at')->sole()->email)->toBe($third['email'])
|
||||
->and($local->refresh()->ad_disabled_at)->toBeNull()
|
||||
@@ -110,7 +111,7 @@ it('requires explicit bounded missing-user deactivation and preserves local acco
|
||||
|
||||
it('reports directory failures without changing users', function () {
|
||||
$this->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andThrow(new RuntimeException('sensitive bind detail'));
|
||||
$this->artisan('ad:sync --deactivate-missing')->expectsOutputToContain('directory or database error')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing --deactivate-missing')->expectsOutputToContain('directory or database error')->assertFailed();
|
||||
expect(User::count())->toBe(0);
|
||||
});
|
||||
|
||||
@@ -119,7 +120,7 @@ it('rejects overlapping runs before reading the directory', function () {
|
||||
expect($lock->get())->toBeTrue();
|
||||
try {
|
||||
$this->mock(ActiveDirectory::class)->shouldNotReceive('users');
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('already running')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing')->expectsOutputToContain('already running')->assertFailed();
|
||||
} finally {
|
||||
$lock->release();
|
||||
}
|
||||
@@ -147,7 +148,12 @@ it('converts binary AD GUIDs and reads the account-disabled flag across pages',
|
||||
$ldap->shouldReceive('freeResult')->twice()->andReturnTrue();
|
||||
useDirectoryConnection($ldap);
|
||||
|
||||
expect((new ActiveDirectory)->users())->toBe([directoryEntry(enabled: false)]);
|
||||
expect((new ActiveDirectory)->users())->toBe([
|
||||
directoryEntry(enabled: false) + [
|
||||
'account_control' => 514,
|
||||
'account_flags' => ['ACCOUNTDISABLE', 'NORMAL_ACCOUNT'],
|
||||
],
|
||||
]);
|
||||
});
|
||||
|
||||
it('discards the entire snapshot when the server returns a partial LDAP result', function () {
|
||||
@@ -158,7 +164,7 @@ it('discards the entire snapshot when the server returns a partial LDAP result',
|
||||
$ldap->shouldReceive('freeResult')->with('partial')->once()->andReturnTrue();
|
||||
useDirectoryConnection($ldap);
|
||||
|
||||
$this->artisan('ad:sync --deactivate-missing')->expectsOutputToContain('directory or database error')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing --deactivate-missing')->expectsOutputToContain('directory or database error')->assertFailed();
|
||||
expect(User::count())->toBe(0);
|
||||
});
|
||||
|
||||
@@ -176,13 +182,13 @@ function useDirectoryConnection(LdapInterface $ldap): void
|
||||
it('denies existing sessions and passkey logins while a managed user is disabled', function () {
|
||||
$this->withoutVite();
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
$user = User::sole();
|
||||
$key = auth()->guard()->getName();
|
||||
$this->withSession([$key => $user->id])->get(route('dashboard'))->assertOk();
|
||||
|
||||
directorySnapshot([directoryEntry(enabled: false)]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
Auth::forgetGuards();
|
||||
$this->withSession([$key => $user->id])->get(route('dashboard'))->assertRedirect(route('login'));
|
||||
|
||||
@@ -197,12 +203,14 @@ it('correlates run summaries and planned or committed changes without logging fi
|
||||
$events[$message] = $context;
|
||||
});
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan($dryRun ? 'ad:sync --dry-run' : 'ad:sync')->assertSuccessful();
|
||||
$this->artisan($dryRun ? 'ad:sync --create-missing --dry-run' : 'ad:sync --create-missing')->assertSuccessful();
|
||||
|
||||
$start = $events['Active Directory sync started.'];
|
||||
expect(Str::isUuid($start['run_id']))->toBeTrue()
|
||||
->and($start['dry_run'])->toBe($dryRun)
|
||||
->and($events['Active Directory sync user change.'])->toBe($start + [
|
||||
'account_control' => null,
|
||||
'account_flags' => [],
|
||||
'action' => 'created',
|
||||
'user_id' => $dryRun ? null : User::sole()->id,
|
||||
'ad_guid' => directoryEntry()['guid'],
|
||||
@@ -217,7 +225,7 @@ it('correlates run summaries and planned or committed changes without logging fi
|
||||
it('logs sanitized failures and emits no user changes for a rejected run', function () {
|
||||
$log = Log::spy();
|
||||
$this->mock(ActiveDirectory::class)->shouldReceive('users')->once()->andThrow(new RuntimeException('sensitive bind detail'));
|
||||
$this->artisan('ad:sync')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing')->assertFailed();
|
||||
$log->shouldHaveReceived('error')->once()->withArgs(fn ($message, $context) => $message === 'Active Directory sync failed.'
|
||||
&& $context['exception_type'] === RuntimeException::class
|
||||
&& $context['exception_code'] === 0
|
||||
@@ -225,7 +233,7 @@ it('logs sanitized failures and emits no user changes for a rejected run', funct
|
||||
);
|
||||
|
||||
directorySnapshot([]);
|
||||
$this->artisan('ad:sync')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing')->assertFailed();
|
||||
$log->shouldHaveReceived('warning')->once()->withArgs(fn ($message, $context) => $message === 'Active Directory sync rejected.'
|
||||
&& $context['reason'] === 'Directory snapshot is empty.'
|
||||
&& $context['duration_ms'] >= 0
|
||||
@@ -235,14 +243,14 @@ it('logs sanitized failures and emits no user changes for a rejected run', funct
|
||||
|
||||
it('will neither claim an existing GUID nor deactivate missing users from another directory scope', function () {
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing')->assertSuccessful();
|
||||
$existing = User::sole();
|
||||
config(['ad-sync.base-dn' => 'OU=Other,DC=ad,DC=test']);
|
||||
directorySnapshot([directoryEntry()]);
|
||||
$this->artisan('ad:sync')->expectsOutputToContain('another directory scope')->assertFailed();
|
||||
$this->artisan('ad:sync --create-missing')->expectsOutputToContain('another directory scope')->assertFailed();
|
||||
|
||||
directorySnapshot([directoryEntry('[email protected]', '11111111-1111-1111-1111-111111111111')]);
|
||||
$this->artisan('ad:sync --deactivate-missing')->assertSuccessful();
|
||||
$this->artisan('ad:sync --create-missing --deactivate-missing')->assertSuccessful();
|
||||
expect($existing->refresh()->deleted_at)->toBeNull();
|
||||
});
|
||||
|
||||
@@ -252,11 +260,11 @@ it('registers an overlap-protected ten-minute job only when scheduling is enable
|
||||
Illuminate\Support\Facades\Schedule::swap($schedule);
|
||||
require base_path('routes/console.php');
|
||||
|
||||
$events = collect($schedule->events())->filter(fn ($event) => str_contains($event->command, 'ad:sync'));
|
||||
$events = collect($schedule->events())->filter(fn ($event) => preg_match('/\\bad:sync(?:\\s|$)/', $event->command) === 1);
|
||||
expect($events)->toHaveCount($enabled ? 1 : 0);
|
||||
if ($enabled) {
|
||||
expect($events->sole()->expression)->toBe('*/10 * * * *')
|
||||
->and($events->sole()->withoutOverlapping)->toBeTrue()
|
||||
->and($events->sole()->command)->not->toContain('--deactivate-missing');
|
||||
->and($events->sole()->command)->toBe(Artisan::formatCommandString('ad:sync'));
|
||||
}
|
||||
})->with(['enabled' => [true], 'disabled' => [false]]);
|
||||
|
||||
Reference in New Issue
Block a user