## Summary - add a first-class finding exception domain with request, approval, rejection, renewal, and revocation lifecycle support - add tenant-scoped exception register, finding governance surfaces, and a canonical workspace approval queue in Filament - add audit, badge, evidence, and review-pack integrations plus focused Pest coverage for workflow, authorization, and governance validity ## Validation - vendor/bin/sail bin pint --dirty --format agent - CI=1 vendor/bin/sail artisan test --compact - manual integrated-browser smoke test for the request-exception happy path, tenant register visibility, and canonical queue visibility ## Notes - Filament implementation remains on v5 with Livewire v4-compatible surfaces - canonical queue lives in the admin panel; provider registration stays in bootstrap/providers.php - finding exceptions stay out of global search in this rollout Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #184
139 lines
3.9 KiB
PHP
139 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\FindingException;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use App\Services\Auth\CapabilityResolver;
|
|
use App\Services\Auth\WorkspaceCapabilityResolver;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Filament\Facades\Filament;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
use Illuminate\Auth\Access\Response;
|
|
|
|
class FindingExceptionPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
|
|
public function viewAny(User $user): bool
|
|
{
|
|
$tenant = $this->resolvedTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return false;
|
|
}
|
|
|
|
if (! $user->canAccessTenant($tenant)) {
|
|
return false;
|
|
}
|
|
|
|
return app(CapabilityResolver::class)->can($user, $tenant, Capabilities::FINDING_EXCEPTION_VIEW);
|
|
}
|
|
|
|
public function view(User $user, FindingException $exception): Response|bool
|
|
{
|
|
$tenant = $this->authorizedTenantOrNull($user, $exception);
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return Response::denyAsNotFound();
|
|
}
|
|
|
|
return app(CapabilityResolver::class)->can($user, $tenant, Capabilities::FINDING_EXCEPTION_VIEW);
|
|
}
|
|
|
|
public function approve(User $user, FindingException $exception): Response|bool
|
|
{
|
|
return $this->authorizeCanonicalApproval($user, $exception);
|
|
}
|
|
|
|
public function reject(User $user, FindingException $exception): Response|bool
|
|
{
|
|
return $this->authorizeCanonicalApproval($user, $exception);
|
|
}
|
|
|
|
private function authorizeCanonicalApproval(User $user, FindingException $exception): Response|bool
|
|
{
|
|
$tenant = $exception->tenant;
|
|
|
|
if (! $tenant instanceof Tenant || ! $user->canAccessTenant($tenant)) {
|
|
return Response::denyAsNotFound();
|
|
}
|
|
|
|
$workspaceId = app(WorkspaceContext::class)->currentWorkspaceId(request());
|
|
|
|
if (! is_int($workspaceId) || $workspaceId !== (int) $exception->workspace_id) {
|
|
return Response::denyAsNotFound();
|
|
}
|
|
|
|
$workspace = $tenant->workspace;
|
|
|
|
if (! $workspace instanceof Workspace) {
|
|
return Response::denyAsNotFound();
|
|
}
|
|
|
|
/** @var WorkspaceCapabilityResolver $resolver */
|
|
$resolver = app(WorkspaceCapabilityResolver::class);
|
|
|
|
if (! $resolver->isMember($user, $workspace)) {
|
|
return Response::denyAsNotFound();
|
|
}
|
|
|
|
return $resolver->can($user, $workspace, Capabilities::FINDING_EXCEPTION_APPROVE)
|
|
? true
|
|
: Response::deny();
|
|
}
|
|
|
|
private function authorizedTenantOrNull(User $user, FindingException $exception): ?Tenant
|
|
{
|
|
$tenant = $this->resolvedTenant();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
return null;
|
|
}
|
|
|
|
if (! $user->canAccessTenant($tenant)) {
|
|
return null;
|
|
}
|
|
|
|
if ((int) $exception->tenant_id !== (int) $tenant->getKey()) {
|
|
return null;
|
|
}
|
|
|
|
if ((int) $exception->workspace_id !== (int) $tenant->workspace_id) {
|
|
return null;
|
|
}
|
|
|
|
return $tenant;
|
|
}
|
|
|
|
private function resolvedTenant(): ?Tenant
|
|
{
|
|
if (Filament::getCurrentPanel()?->getId() === 'admin') {
|
|
$workspaceId = app(WorkspaceContext::class)->currentWorkspaceId(request());
|
|
|
|
if (! is_int($workspaceId)) {
|
|
return null;
|
|
}
|
|
|
|
$tenantId = app(WorkspaceContext::class)->lastTenantId(request());
|
|
|
|
if (! is_int($tenantId)) {
|
|
return null;
|
|
}
|
|
|
|
$tenant = Tenant::query()->whereKey($tenantId)->first();
|
|
|
|
return $tenant instanceof Tenant && (int) $tenant->workspace_id === $workspaceId ? $tenant : null;
|
|
}
|
|
|
|
$tenant = Tenant::current();
|
|
|
|
return $tenant instanceof Tenant ? $tenant : null;
|
|
}
|
|
}
|