- ReviewPackService: generate, fingerprint dedupe, signed download URL - GenerateReviewPackJob: 12-step pipeline, ZIP assembly, failure handling - ReviewPackDownloadController: signed URL streaming with SHA-256 header - ReviewPackResource: list/view pages, generate/expire/download actions - TenantReviewPackCard: dashboard widget with 5 display states - ReviewPackPolicy: RBAC via REVIEW_PACK_VIEW/MANAGE capabilities - PruneReviewPacksCommand: retention automation + hard-delete option - ReviewPackStatusNotification: database channel, ready/failed payloads - Schedule: daily prune + entra admin roles, posture:dispatch deferred - AlertRuleResource: hide sla_due from dropdown (backward compat kept) - 59 passing tests across 7 test files (1 skipped: posture deferred) - All 36 tasks completed per tasks.md
159 lines
4.5 KiB
PHP
159 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Widgets\Tenant;
|
|
|
|
use App\Models\ReviewPack;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\ReviewPackService;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\ReviewPackStatus;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Widgets\Widget;
|
|
|
|
class TenantReviewPackCard extends Widget
|
|
{
|
|
protected static bool $isLazy = false;
|
|
|
|
protected string $view = 'filament.widgets.tenant.tenant-review-pack-card';
|
|
|
|
public ?Tenant $record = null;
|
|
|
|
private function resolveTenant(): ?Tenant
|
|
{
|
|
$tenant = Filament::getTenant();
|
|
|
|
if ($tenant instanceof Tenant) {
|
|
return $tenant;
|
|
}
|
|
|
|
return $this->record instanceof Tenant ? $this->record : null;
|
|
}
|
|
|
|
public function generatePack(bool $includePii = true, bool $includeOperations = true): void
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
$tenant = $this->resolveTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $user->canAccessTenant($tenant)) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $user->can(Capabilities::REVIEW_PACK_MANAGE, $tenant)) {
|
|
abort(403);
|
|
}
|
|
|
|
/** @var ReviewPackService $service */
|
|
$service = app(ReviewPackService::class);
|
|
|
|
if ($service->checkActiveRun($tenant)) {
|
|
Notification::make()
|
|
->title('Generation already in progress')
|
|
->body('A review pack is currently being generated for this tenant.')
|
|
->warning()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$service->generate($tenant, $user, [
|
|
'include_pii' => $includePii,
|
|
'include_operations' => $includeOperations,
|
|
]);
|
|
|
|
Notification::make()
|
|
->title('Review pack generation started')
|
|
->body('The pack will be generated in the background. You will be notified when it is ready.')
|
|
->success()
|
|
->send();
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
protected function getViewData(): array
|
|
{
|
|
$tenant = $this->resolveTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return $this->emptyState();
|
|
}
|
|
|
|
$user = auth()->user();
|
|
$isTenantMember = $user instanceof User && $user->canAccessTenant($tenant);
|
|
$canView = $isTenantMember && $user->can(Capabilities::REVIEW_PACK_VIEW, $tenant);
|
|
$canManage = $isTenantMember && $user->can(Capabilities::REVIEW_PACK_MANAGE, $tenant);
|
|
|
|
$latestPack = ReviewPack::query()
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->orderByDesc('created_at')
|
|
->orderByDesc('id')
|
|
->first();
|
|
|
|
if (! $latestPack instanceof ReviewPack) {
|
|
return [
|
|
'tenant' => $tenant,
|
|
'pack' => null,
|
|
'statusEnum' => null,
|
|
'canView' => $canView,
|
|
'canManage' => $canManage,
|
|
'downloadUrl' => null,
|
|
'failedReason' => null,
|
|
];
|
|
}
|
|
|
|
$statusEnum = ReviewPackStatus::tryFrom((string) $latestPack->status);
|
|
|
|
$downloadUrl = null;
|
|
if ($statusEnum === ReviewPackStatus::Ready && $canView) {
|
|
/** @var ReviewPackService $service */
|
|
$service = app(ReviewPackService::class);
|
|
$downloadUrl = $service->generateDownloadUrl($latestPack);
|
|
}
|
|
|
|
$failedReason = null;
|
|
if ($statusEnum === ReviewPackStatus::Failed && $latestPack->operationRun) {
|
|
$opContext = is_array($latestPack->operationRun->context) ? $latestPack->operationRun->context : [];
|
|
$failedReason = (string) ($opContext['reason_code'] ?? 'Unknown error');
|
|
}
|
|
|
|
return [
|
|
'tenant' => $tenant,
|
|
'pack' => $latestPack,
|
|
'statusEnum' => $statusEnum,
|
|
'canView' => $canView,
|
|
'canManage' => $canManage,
|
|
'downloadUrl' => $downloadUrl,
|
|
'failedReason' => $failedReason,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function emptyState(): array
|
|
{
|
|
return [
|
|
'tenant' => null,
|
|
'pack' => null,
|
|
'statusEnum' => null,
|
|
'canView' => false,
|
|
'canManage' => false,
|
|
'downloadUrl' => null,
|
|
'failedReason' => null,
|
|
];
|
|
}
|
|
}
|