Implements Spec 093 (SCOPE-001) workspace isolation at the data layer. What changed - Adds `workspace_id` to 12 tenant-owned tables and enforces correct binding. - Model write-path enforcement derives workspace from tenant + rejects mismatches. - Prevents `tenant_id` changes (immutability) on tenant-owned records. - Adds queued backfill command + job (`tenantpilot:backfill-workspace-ids`) with OperationRun + AuditLog observability. - Enforces DB constraints (NOT NULL + FK `workspace_id` → `workspaces.id` + composite FK `(tenant_id, workspace_id)` → `tenants(id, workspace_id)`), plus audit_logs invariant. UI / operator visibility - Monitor backfill runs in **Monitoring → Operations** (OperationRun). Tests - `vendor/bin/sail artisan test --compact tests/Feature/WorkspaceIsolation` Notes - Backfill is queued: ensure a queue worker is running (`vendor/bin/sail artisan queue:work`). Spec package - `specs/093-scope-001-workspace-id-isolation/` (plan, tasks, contracts, quickstart, research) Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #112
145 lines
4.8 KiB
PHP
145 lines
4.8 KiB
PHP
<?php
|
|
|
|
use App\Models\AuditLog;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\Workspace;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
it('backfills missing workspace_id values for tenant-owned rows', function (): void {
|
|
$workspace = Workspace::factory()->create();
|
|
|
|
$tenant = Tenant::factory()->create([
|
|
'workspace_id' => (int) $workspace->getKey(),
|
|
]);
|
|
|
|
DB::table('policies')->insert([
|
|
[
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => null,
|
|
'external_id' => 'legacy-policy-a',
|
|
'policy_type' => 'settingsCatalogPolicy',
|
|
'platform' => 'windows',
|
|
'display_name' => 'Legacy Policy A',
|
|
'metadata' => json_encode([], JSON_THROW_ON_ERROR),
|
|
'last_synced_at' => now(),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
],
|
|
[
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => null,
|
|
'external_id' => 'legacy-policy-b',
|
|
'policy_type' => 'settingsCatalogPolicy',
|
|
'platform' => 'windows',
|
|
'display_name' => 'Legacy Policy B',
|
|
'metadata' => json_encode([], JSON_THROW_ON_ERROR),
|
|
'last_synced_at' => now(),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
],
|
|
]);
|
|
|
|
$this->artisan('tenantpilot:backfill-workspace-ids', [
|
|
'--table' => 'policies',
|
|
'--batch-size' => 1,
|
|
])->assertSuccessful();
|
|
|
|
$missingAfter = DB::table('policies')
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->whereNull('workspace_id')
|
|
->count();
|
|
|
|
expect($missingAfter)->toBe(0);
|
|
|
|
$workspaceIds = DB::table('policies')
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->pluck('workspace_id')
|
|
->map(static fn (mixed $workspaceId): int => (int) $workspaceId)
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
|
|
expect($workspaceIds)->toBe([(int) $workspace->getKey()]);
|
|
|
|
$run = OperationRun::query()
|
|
->where('workspace_id', (int) $workspace->getKey())
|
|
->where('type', 'workspace_isolation_backfill_workspace_ids')
|
|
->latest('id')
|
|
->first();
|
|
|
|
expect($run)->not->toBeNull();
|
|
expect((int) data_get($run?->summary_counts, 'processed', 0))->toBe(2);
|
|
|
|
$actions = AuditLog::query()
|
|
->where('workspace_id', (int) $workspace->getKey())
|
|
->pluck('action')
|
|
->all();
|
|
|
|
expect($actions)->toContain('workspace_isolation.backfill_workspace_ids.started');
|
|
expect($actions)->toContain('workspace_isolation.backfill_workspace_ids.dispatched');
|
|
});
|
|
|
|
it('is idempotent when re-run after backfill', function (): void {
|
|
$workspace = Workspace::factory()->create();
|
|
|
|
$tenant = Tenant::factory()->create([
|
|
'workspace_id' => (int) $workspace->getKey(),
|
|
]);
|
|
|
|
DB::table('policies')->insert([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => null,
|
|
'external_id' => 'legacy-policy-retry',
|
|
'policy_type' => 'settingsCatalogPolicy',
|
|
'platform' => 'windows',
|
|
'display_name' => 'Legacy Retry Policy',
|
|
'metadata' => json_encode([], JSON_THROW_ON_ERROR),
|
|
'last_synced_at' => now(),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$this->artisan('tenantpilot:backfill-workspace-ids', ['--table' => 'policies'])->assertSuccessful();
|
|
|
|
$this->artisan('tenantpilot:backfill-workspace-ids', ['--table' => 'policies'])
|
|
->expectsOutputToContain('No rows require workspace_id backfill.')
|
|
->assertSuccessful();
|
|
|
|
$missingAfter = DB::table('policies')
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->whereNull('workspace_id')
|
|
->count();
|
|
|
|
expect($missingAfter)->toBe(0);
|
|
});
|
|
|
|
it('aborts and reports when tenant to workspace mapping is unresolvable', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
$tenant->forceFill(['workspace_id' => null])->save();
|
|
|
|
DB::table('policies')->insert([
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'workspace_id' => null,
|
|
'external_id' => 'legacy-policy-unresolvable',
|
|
'policy_type' => 'settingsCatalogPolicy',
|
|
'platform' => 'windows',
|
|
'display_name' => 'Legacy Unresolvable Policy',
|
|
'metadata' => json_encode([], JSON_THROW_ON_ERROR),
|
|
'last_synced_at' => now(),
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
$this->artisan('tenantpilot:backfill-workspace-ids', ['--table' => 'policies'])
|
|
->expectsOutputToContain('Unresolvable tenant->workspace mapping')
|
|
->assertFailed();
|
|
|
|
$missingAfter = DB::table('policies')
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->whereNull('workspace_id')
|
|
->count();
|
|
|
|
expect($missingAfter)->toBe(1);
|
|
});
|