Summary: - Baseline Compare landing: enterprise UI (stats grid, critical drift banner, better actions), navigation grouping under Governance, and Action Surface Contract declaration. - Baseline Profile view page: switches from disabled form fields to proper Infolist entries for a clean read-only view. - Fixes tenant name column usages (`display_name` → `name`) in baseline assignment flows. - Dashboard: improved baseline governance widget with severity breakdown + last compared. Notes: - Filament v5 / Livewire v4 compatible. - Destructive actions remain confirmed (`->requiresConfirmation()`). Tests: - `vendor/bin/sail artisan test --compact tests/Feature/Baselines` - `vendor/bin/sail artisan test --compact tests/Feature/Guards/ActionSurfaceContractTest.php` Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #123
98 lines
3.0 KiB
PHP
98 lines
3.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Baselines;
|
|
|
|
use App\Jobs\CompareBaselineToTenantJob;
|
|
use App\Models\BaselineProfile;
|
|
use App\Models\BaselineTenantAssignment;
|
|
use App\Models\OperationRun;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\OperationRunService;
|
|
use App\Support\Baselines\BaselineReasonCodes;
|
|
use App\Support\Baselines\BaselineScope;
|
|
|
|
final class BaselineCompareService
|
|
{
|
|
public function __construct(
|
|
private readonly OperationRunService $runs,
|
|
) {}
|
|
|
|
/**
|
|
* @return array{ok: bool, run?: OperationRun, reason_code?: string}
|
|
*/
|
|
public function startCompare(
|
|
Tenant $tenant,
|
|
User $initiator,
|
|
): array {
|
|
$assignment = BaselineTenantAssignment::query()
|
|
->where('workspace_id', $tenant->workspace_id)
|
|
->where('tenant_id', $tenant->getKey())
|
|
->first();
|
|
|
|
if (! $assignment instanceof BaselineTenantAssignment) {
|
|
return ['ok' => false, 'reason_code' => BaselineReasonCodes::COMPARE_NO_ASSIGNMENT];
|
|
}
|
|
|
|
$profile = BaselineProfile::query()->find($assignment->baseline_profile_id);
|
|
|
|
if (! $profile instanceof BaselineProfile) {
|
|
return ['ok' => false, 'reason_code' => BaselineReasonCodes::COMPARE_PROFILE_NOT_ACTIVE];
|
|
}
|
|
|
|
$precondition = $this->validatePreconditions($profile);
|
|
|
|
if ($precondition !== null) {
|
|
return ['ok' => false, 'reason_code' => $precondition];
|
|
}
|
|
|
|
$snapshotId = (int) $profile->active_snapshot_id;
|
|
|
|
$profileScope = BaselineScope::fromJsonb(
|
|
is_array($profile->scope_jsonb) ? $profile->scope_jsonb : null,
|
|
);
|
|
$overrideScope = $assignment->override_scope_jsonb !== null
|
|
? BaselineScope::fromJsonb(is_array($assignment->override_scope_jsonb) ? $assignment->override_scope_jsonb : null)
|
|
: null;
|
|
|
|
$effectiveScope = BaselineScope::effective($profileScope, $overrideScope);
|
|
|
|
$context = [
|
|
'baseline_profile_id' => (int) $profile->getKey(),
|
|
'baseline_snapshot_id' => $snapshotId,
|
|
'effective_scope' => $effectiveScope->toJsonb(),
|
|
];
|
|
|
|
$run = $this->runs->ensureRunWithIdentity(
|
|
tenant: $tenant,
|
|
type: 'baseline_compare',
|
|
identityInputs: [
|
|
'baseline_profile_id' => (int) $profile->getKey(),
|
|
],
|
|
context: $context,
|
|
initiator: $initiator,
|
|
);
|
|
|
|
if ($run->wasRecentlyCreated) {
|
|
CompareBaselineToTenantJob::dispatch($run);
|
|
}
|
|
|
|
return ['ok' => true, 'run' => $run];
|
|
}
|
|
|
|
private function validatePreconditions(BaselineProfile $profile): ?string
|
|
{
|
|
if ($profile->status !== BaselineProfile::STATUS_ACTIVE) {
|
|
return BaselineReasonCodes::COMPARE_PROFILE_NOT_ACTIVE;
|
|
}
|
|
|
|
if ($profile->active_snapshot_id === null) {
|
|
return BaselineReasonCodes::COMPARE_NO_ACTIVE_SNAPSHOT;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|