## Summary - introduce a shared tenant-owned query and record-resolution canon for first-slice Filament resources - harden direct views, row actions, bulk actions, relation managers, and workspace-admin canonical viewers against wrong-tenant access - add registry-backed rollout metadata, search posture handling, architectural guards, and focused Pest coverage for scope parity and 404/403 semantics ## Included - Spec 150 package under `specs/150-tenant-owned-query-canon-and-wrong-tenant-guards/` - shared support classes: `TenantOwnedModelFamilies`, `TenantOwnedQueryScope`, `TenantOwnedRecordResolver` - shared Filament concern: `InteractsWithTenantOwnedRecords` - resource/page/policy hardening across findings, policies, policy versions, backup schedules, backup sets, restore runs, inventory items, and Entra groups - additional regression coverage for canonical tenant state, wrong-tenant record resolution, relation-manager congruence, and action-surface guardrails ## Validation - `vendor/bin/sail artisan test --compact` passed - full suite result: `2733 passed, 8 skipped` - formatting applied with `vendor/bin/sail bin pint --dirty --format agent` ## Notes - Livewire v4.0+ compliant via existing Filament v5 stack - provider registration remains in `bootstrap/providers.php` - globally searchable first-slice posture: Entra groups scoped; policies and policy versions explicitly disabled - destructive actions continue to use confirmation and policy authorization - no new Filament assets added; existing deployment flow remains unchanged, including `php artisan filament:assets` when registered assets are used Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #180
112 lines
3.6 KiB
PHP
112 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Filament\Concerns\InteractsWithTenantOwnedRecords;
|
|
use App\Models\Policy;
|
|
use App\Models\Tenant;
|
|
use App\Support\WorkspaceIsolation\TenantOwnedQueryScope;
|
|
use App\Support\WorkspaceIsolation\TenantOwnedRecordResolver;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Schemas\Schema;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
it('applies a tenant-owned scope to matching records only', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
$foreignTenant = Tenant::factory()->create();
|
|
|
|
$ownedRecord = Policy::factory()->create(['tenant_id' => $tenant->getKey()]);
|
|
Policy::factory()->create(['tenant_id' => $foreignTenant->getKey()]);
|
|
|
|
$records = app(TenantOwnedQueryScope::class)
|
|
->apply(Policy::query(), $tenant)
|
|
->pluck('id')
|
|
->all();
|
|
|
|
expect($records)->toBe([(int) $ownedRecord->getKey()]);
|
|
});
|
|
|
|
it('fails closed when no tenant context is available', function (): void {
|
|
Tenant::factory()->count(2)->create()->each(function (Tenant $tenant): void {
|
|
Policy::factory()->create(['tenant_id' => $tenant->getKey()]);
|
|
});
|
|
|
|
$count = app(TenantOwnedQueryScope::class)
|
|
->apply(Policy::query(), null)
|
|
->count();
|
|
|
|
expect($count)->toBe(0);
|
|
});
|
|
|
|
it('resolves only records inside the already-scoped tenant query', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
$foreignTenant = Tenant::factory()->create();
|
|
|
|
$ownedRecord = Policy::factory()->create(['tenant_id' => $tenant->getKey()]);
|
|
$foreignRecord = Policy::factory()->create(['tenant_id' => $foreignTenant->getKey()]);
|
|
|
|
$resolver = app(TenantOwnedRecordResolver::class);
|
|
$scopedQuery = app(TenantOwnedQueryScope::class)->apply(Policy::query(), $tenant);
|
|
|
|
expect($resolver->resolve($scopedQuery, $ownedRecord))
|
|
->toBeInstanceOf(Model::class)
|
|
->and($resolver->resolve($scopedQuery, $ownedRecord)?->is($ownedRecord))->toBeTrue();
|
|
|
|
expect($resolver->resolve($scopedQuery, $foreignRecord))->toBeNull();
|
|
});
|
|
|
|
it('lets the shared filament trait expose the scoped query and resolver helpers', function (): void {
|
|
$tenant = Tenant::factory()->create();
|
|
$foreignTenant = Tenant::factory()->create();
|
|
|
|
$ownedRecord = Policy::factory()->create(['tenant_id' => $tenant->getKey()]);
|
|
Policy::factory()->create(['tenant_id' => $foreignTenant->getKey()]);
|
|
|
|
TestTenantOwnedPolicyResource::fakeTenant($tenant);
|
|
|
|
expect(TestTenantOwnedPolicyResource::queryViaTrait()->pluck('id')->all())
|
|
->toBe([(int) $ownedRecord->getKey()]);
|
|
|
|
expect(TestTenantOwnedPolicyResource::resolveViaTrait($ownedRecord)?->is($ownedRecord))
|
|
->toBeTrue();
|
|
});
|
|
|
|
class TestTenantOwnedPolicyResource extends Resource
|
|
{
|
|
use InteractsWithTenantOwnedRecords;
|
|
|
|
protected static ?string $model = Policy::class;
|
|
|
|
protected static ?string $tenantOwnershipRelationshipName = 'tenant';
|
|
|
|
protected static ?Tenant $fakeTenant = null;
|
|
|
|
public static function fakeTenant(?Tenant $tenant): void
|
|
{
|
|
static::$fakeTenant = $tenant;
|
|
}
|
|
|
|
public static function queryViaTrait()
|
|
{
|
|
return static::getTenantOwnedEloquentQuery();
|
|
}
|
|
|
|
public static function resolveViaTrait(Model|int|string|null $record): ?Model
|
|
{
|
|
return static::resolveTenantOwnedRecord($record);
|
|
}
|
|
|
|
protected static function resolveTenantContextForTenantOwnedRecords(): ?Tenant
|
|
{
|
|
return static::$fakeTenant;
|
|
}
|
|
|
|
public static function form(Schema $schema): Schema
|
|
{
|
|
return $schema;
|
|
}
|
|
}
|