Kontext / Ziel Diese PR standardisiert Tenant‑RBAC Enforcement in der Filament‑UI: statt ad-hoc Gate::*, abort_if/abort_unless und kopierten ->visible()/->disabled()‑Closures gibt es jetzt eine zentrale, wiederverwendbare Implementierung für Actions (Header/Table/Bulk). Links zur Spec: spec.md plan.md quickstart.md Was ist drin Neue zentrale Helper-API: UiEnforcement (Tenant-plane RBAC‑UX “source of truth” für Filament Actions) Standardisierte Tooltip-Texte und Context-DTO (UiTooltips, TenantAccessContext) Migration vieler tenant‑scoped Filament Action-Surfaces auf das Standardpattern (ohne ad-hoc Auth-Patterns) CI‑Guard (Test) gegen neue ad-hoc Patterns in app/Filament/**: verbietet Gate::allows/denies/check/authorize, use Illuminate\Support\Facades\Gate, abort_if/abort_unless Legacy-Allowlist ist aktuell leer (neue Verstöße failen sofort) RBAC-UX Semantik (konsequent & testbar) Non-member: UI Actions hidden (kein Tenant‑Leak); Execution wird blockiert (Filament hidden→disabled chain), Defense‑in‑depth enthält zusätzlich serverseitige Guards. Member ohne Capability: Action visible aber disabled + Standard-Tooltip; Execution wird blockiert (keine Side Effects). Member mit Capability: Action enabled und ausführbar. Destructive actions: über ->destructive() immer mit ->requiresConfirmation() + klare Warntexte (Execution bleibt über ->action(...)). Wichtig: In Filament v5 sind hidden/disabled Actions typischerweise “silently blocked” (200, keine Ausführung). Die Tests prüfen daher UI‑State + “no side effects”, nicht nur HTTP‑Statuscodes. Sicherheit / Scope Keine neuen DB-Tabellen, keine Migrations, keine Microsoft Graph Calls (DB‑only bei Render; kein outbound HTTP). Tenant Isolation bleibt Isolation‑Boundary (deny-as-not-found auf Tenant‑Ebene, Capability erst nach Membership). Kein Asset-Setup erforderlich; keine neuen Filament Assets. Compliance Notes (Repo-Regeln) Filament v5 / Livewire v4.0+ kompatibel. Keine Änderungen an Provider‑Registrierung (Laravel 11+/12: providers.php bleibt der Ort; hier unverändert). Global Search: keine gezielte Änderung am Global‑Search-Verhalten in dieser PR. Tests / Qualität Pest Feature/Unit Tests für Member/Non-member/Tooltip/Destructive/Regression‑Guard. Guard-Test: “No ad-hoc Filament auth patterns”. Full suite laut Tasks: vendor/bin/sail artisan test --compact → 837 passed, 5 skipped. Checklist: requirements.md vollständig (16/16). Review-Fokus API‑Usage in neuen/angepassten Filament Actions: UiEnforcement::forAction/forTableAction/forBulkAction(...)->requireCapability(...)->apply() Guard-Test soll “red” werden, sobald jemand neue ad-hoc Auth‑Patterns einführt (by design). Co-authored-by: Ahmed Darrazi <ahmeddarrazi@MacBookPro.fritz.box> Reviewed-on: #81
285 lines
9.5 KiB
PHP
285 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Filament\Resources\FindingResource;
|
|
use App\Filament\Resources\InventorySyncRunResource;
|
|
use App\Jobs\GenerateDriftFindingsJob;
|
|
use App\Models\Finding;
|
|
use App\Models\InventorySyncRun;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Auth\CapabilityResolver;
|
|
use App\Services\Drift\DriftRunSelector;
|
|
use App\Services\OperationRunService;
|
|
use App\Services\Operations\BulkSelectionIdentity;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\OperationRunLinks;
|
|
use App\Support\OpsUx\OperationUxPresenter;
|
|
use App\Support\OpsUx\OpsUxBrowserEvents;
|
|
use BackedEnum;
|
|
use Filament\Actions\Action;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use UnitEnum;
|
|
|
|
class DriftLanding extends Page
|
|
{
|
|
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-arrows-right-left';
|
|
|
|
protected static string|UnitEnum|null $navigationGroup = 'Drift';
|
|
|
|
protected static ?string $navigationLabel = 'Drift';
|
|
|
|
protected string $view = 'filament.pages.drift-landing';
|
|
|
|
public ?string $state = null;
|
|
|
|
public ?string $message = null;
|
|
|
|
public ?string $scopeKey = null;
|
|
|
|
public ?int $baselineRunId = null;
|
|
|
|
public ?int $currentRunId = null;
|
|
|
|
public ?string $baselineFinishedAt = null;
|
|
|
|
public ?string $currentFinishedAt = null;
|
|
|
|
public ?int $operationRunId = null;
|
|
|
|
/** @var array<string, int>|null */
|
|
public ?array $statusCounts = null;
|
|
|
|
public static function canAccess(): bool
|
|
{
|
|
return FindingResource::canAccess();
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$tenant = Tenant::current();
|
|
|
|
$user = auth()->user();
|
|
if (! $user instanceof User) {
|
|
abort(403, 'Not allowed');
|
|
}
|
|
|
|
$latestSuccessful = InventorySyncRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('status', InventorySyncRun::STATUS_SUCCESS)
|
|
->whereNotNull('finished_at')
|
|
->orderByDesc('finished_at')
|
|
->first();
|
|
|
|
if (! $latestSuccessful instanceof InventorySyncRun) {
|
|
$this->state = 'blocked';
|
|
$this->message = 'No successful inventory runs found yet.';
|
|
|
|
return;
|
|
}
|
|
|
|
$scopeKey = (string) $latestSuccessful->selection_hash;
|
|
$this->scopeKey = $scopeKey;
|
|
|
|
$selector = app(DriftRunSelector::class);
|
|
$comparison = $selector->selectBaselineAndCurrent($tenant, $scopeKey);
|
|
|
|
if ($comparison === null) {
|
|
$this->state = 'blocked';
|
|
$this->message = 'Need at least 2 successful runs for this scope to calculate drift.';
|
|
|
|
return;
|
|
}
|
|
|
|
$baseline = $comparison['baseline'];
|
|
$current = $comparison['current'];
|
|
|
|
$this->baselineRunId = (int) $baseline->getKey();
|
|
$this->currentRunId = (int) $current->getKey();
|
|
|
|
$this->baselineFinishedAt = $baseline->finished_at?->toDateTimeString();
|
|
$this->currentFinishedAt = $current->finished_at?->toDateTimeString();
|
|
|
|
$existingOperationRun = OperationRun::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('type', 'drift.generate')
|
|
->where('context->scope_key', $scopeKey)
|
|
->where('context->baseline_run_id', (int) $baseline->getKey())
|
|
->where('context->current_run_id', (int) $current->getKey())
|
|
->latest('id')
|
|
->first();
|
|
|
|
if ($existingOperationRun instanceof OperationRun) {
|
|
$this->operationRunId = (int) $existingOperationRun->getKey();
|
|
}
|
|
|
|
$exists = Finding::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->where('scope_key', $scopeKey)
|
|
->where('baseline_run_id', $baseline->getKey())
|
|
->where('current_run_id', $current->getKey())
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
$this->state = 'ready';
|
|
$newCount = (int) Finding::query()
|
|
->where('tenant_id', $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->where('scope_key', $scopeKey)
|
|
->where('baseline_run_id', $baseline->getKey())
|
|
->where('current_run_id', $current->getKey())
|
|
->where('status', Finding::STATUS_NEW)
|
|
->count();
|
|
|
|
$this->statusCounts = [Finding::STATUS_NEW => $newCount];
|
|
|
|
return;
|
|
}
|
|
|
|
$existingOperationRun?->refresh();
|
|
|
|
if ($existingOperationRun instanceof OperationRun
|
|
&& in_array($existingOperationRun->status, ['queued', 'running'], true)
|
|
) {
|
|
$this->state = 'generating';
|
|
$this->operationRunId = (int) $existingOperationRun->getKey();
|
|
|
|
return;
|
|
}
|
|
|
|
if ($existingOperationRun instanceof OperationRun
|
|
&& $existingOperationRun->status === 'completed'
|
|
) {
|
|
$counts = is_array($existingOperationRun->summary_counts ?? null) ? $existingOperationRun->summary_counts : [];
|
|
$created = (int) ($counts['created'] ?? 0);
|
|
|
|
if ($existingOperationRun->outcome === 'failed') {
|
|
$this->state = 'error';
|
|
$this->message = 'Drift generation failed for this comparison. See the run for details.';
|
|
$this->operationRunId = (int) $existingOperationRun->getKey();
|
|
|
|
return;
|
|
}
|
|
|
|
if ($created === 0) {
|
|
$this->state = 'ready';
|
|
$this->statusCounts = [Finding::STATUS_NEW => 0];
|
|
$this->message = 'No drift findings for this comparison. If you changed settings after the current run, run Inventory Sync again to capture a newer snapshot.';
|
|
$this->operationRunId = (int) $existingOperationRun->getKey();
|
|
|
|
return;
|
|
}
|
|
}
|
|
|
|
/** @var CapabilityResolver $resolver */
|
|
$resolver = app(CapabilityResolver::class);
|
|
|
|
if (! $resolver->can($user, $tenant, Capabilities::TENANT_SYNC)) {
|
|
$this->state = 'blocked';
|
|
$this->message = 'You can view existing drift findings and run history, but you do not have permission to generate drift.';
|
|
|
|
return;
|
|
}
|
|
|
|
/** @var BulkSelectionIdentity $selection */
|
|
$selection = app(BulkSelectionIdentity::class);
|
|
$selectionIdentity = $selection->fromQuery([
|
|
'scope_key' => $scopeKey,
|
|
'baseline_run_id' => (int) $baseline->getKey(),
|
|
'current_run_id' => (int) $current->getKey(),
|
|
]);
|
|
|
|
/** @var OperationRunService $opService */
|
|
$opService = app(OperationRunService::class);
|
|
|
|
$opRun = $opService->enqueueBulkOperation(
|
|
tenant: $tenant,
|
|
type: 'drift.generate',
|
|
targetScope: [
|
|
'entra_tenant_id' => (string) ($tenant->tenant_id ?? $tenant->external_id),
|
|
],
|
|
selectionIdentity: $selectionIdentity,
|
|
dispatcher: function ($operationRun) use ($tenant, $user, $baseline, $current, $scopeKey): void {
|
|
GenerateDriftFindingsJob::dispatch(
|
|
tenantId: (int) $tenant->getKey(),
|
|
userId: (int) $user->getKey(),
|
|
baselineRunId: (int) $baseline->getKey(),
|
|
currentRunId: (int) $current->getKey(),
|
|
scopeKey: $scopeKey,
|
|
operationRun: $operationRun,
|
|
);
|
|
},
|
|
initiator: $user,
|
|
extraContext: [
|
|
'scope_key' => $scopeKey,
|
|
'baseline_run_id' => (int) $baseline->getKey(),
|
|
'current_run_id' => (int) $current->getKey(),
|
|
],
|
|
emitQueuedNotification: false,
|
|
);
|
|
|
|
$this->operationRunId = (int) $opRun->getKey();
|
|
$this->state = 'generating';
|
|
|
|
if (! $opRun->wasRecentlyCreated) {
|
|
Notification::make()
|
|
->title('Drift generation already active')
|
|
->body('This operation is already queued or running.')
|
|
->warning()
|
|
->actions([
|
|
Action::make('view_run')
|
|
->label('View run')
|
|
->url(OperationRunLinks::view($opRun, $tenant)),
|
|
])
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
OpsUxBrowserEvents::dispatchRunEnqueued($this);
|
|
OperationUxPresenter::queuedToast((string) $opRun->type)
|
|
->actions([
|
|
Action::make('view_run')
|
|
->label('View run')
|
|
->url(OperationRunLinks::view($opRun, $tenant)),
|
|
])
|
|
->send();
|
|
}
|
|
|
|
public function getFindingsUrl(): string
|
|
{
|
|
return FindingResource::getUrl('index', tenant: Tenant::current());
|
|
}
|
|
|
|
public function getBaselineRunUrl(): ?string
|
|
{
|
|
if (! is_int($this->baselineRunId)) {
|
|
return null;
|
|
}
|
|
|
|
return InventorySyncRunResource::getUrl('view', ['record' => $this->baselineRunId], tenant: Tenant::current());
|
|
}
|
|
|
|
public function getCurrentRunUrl(): ?string
|
|
{
|
|
if (! is_int($this->currentRunId)) {
|
|
return null;
|
|
}
|
|
|
|
return InventorySyncRunResource::getUrl('view', ['record' => $this->currentRunId], tenant: Tenant::current());
|
|
}
|
|
|
|
public function getOperationRunUrl(): ?string
|
|
{
|
|
if (! is_int($this->operationRunId)) {
|
|
return null;
|
|
}
|
|
|
|
return OperationRunLinks::view($this->operationRunId, Tenant::current());
|
|
}
|
|
}
|