## Summary - harden the canonical operation run viewer so mismatched, missing, archived, onboarding, and selector-excluded tenant context no longer invalidates authorized canonical run viewing - extend canonical route, header-context, deep-link, and presentation coverage for Spec 144 and add the full spec artifact set under `specs/144-canonical-operation-viewer-context-decoupling/` - harden onboarding draft provider-connection resume logic so stale persisted provider connections fall back to the connect-provider step instead of resuming invalid state - add architecture-audit follow-up candidate material and prompt assets for the next governance hardening wave ## Testing - `vendor/bin/sail bin pint --dirty --format agent` - `vendor/bin/sail artisan test --compact tests/Feature/144/CanonicalOperationViewerContextMismatchTest.php tests/Feature/144/CanonicalOperationViewerDeepLinkTrustTest.php tests/Feature/Operations/TenantlessOperationRunViewerTest.php tests/Feature/OpsUx/OperateHubShellTest.php tests/Feature/Monitoring/OperationsTenantScopeTest.php tests/Feature/RunAuthorizationTenantIsolationTest.php tests/Feature/Filament/OperationRunEnterpriseDetailPageTest.php tests/Feature/Monitoring/HeaderContextBarTest.php tests/Feature/Monitoring/OperationRunResolvedReferencePresentationTest.php tests/Feature/Monitoring/OperationsCanonicalUrlsTest.php` - `vendor/bin/sail artisan test --compact tests/Feature/ManagedTenantOnboardingWizardTest.php tests/Unit/Onboarding/OnboardingDraftStageResolverTest.php tests/Unit/Onboarding/OnboardingLifecycleServiceTest.php` ## Notes - branch: `144-canonical-operation-viewer-context-decoupling` - base: `dev` Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #173
109 lines
3.4 KiB
PHP
109 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Onboarding;
|
|
|
|
use App\Models\TenantOnboardingSession;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use App\Services\Audit\WorkspaceAuditLogger;
|
|
use App\Support\Audit\AuditActionId;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
class OnboardingDraftResolver
|
|
{
|
|
public function __construct(
|
|
private readonly OnboardingLifecycleService $lifecycleService,
|
|
private readonly WorkspaceAuditLogger $auditLogger,
|
|
) {}
|
|
|
|
/**
|
|
* @throws AuthorizationException
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
public function resolve(TenantOnboardingSession|int|string $draft, User $user, Workspace $workspace): TenantOnboardingSession
|
|
{
|
|
$draftId = $draft instanceof TenantOnboardingSession
|
|
? (int) $draft->getKey()
|
|
: (int) $draft;
|
|
|
|
$resolvedDraft = TenantOnboardingSession::query()
|
|
->with(['tenant', 'startedByUser', 'updatedByUser'])
|
|
->whereKey($draftId)
|
|
->first();
|
|
|
|
if (! $resolvedDraft instanceof TenantOnboardingSession) {
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
if ((int) $resolvedDraft->workspace_id !== (int) $workspace->getKey()) {
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
Gate::forUser($user)->authorize('view', $resolvedDraft);
|
|
|
|
$resolvedDraft = $this->lifecycleService
|
|
->syncPersistedLifecycle($resolvedDraft)
|
|
->loadMissing(['tenant', 'startedByUser', 'updatedByUser']);
|
|
|
|
$normalizedTenant = $this->lifecycleService->syncLinkedTenantAfterCancellation($resolvedDraft);
|
|
|
|
if ($normalizedTenant !== null) {
|
|
$this->auditLogger->logTenantLifecycleAction(
|
|
tenant: $normalizedTenant,
|
|
action: AuditActionId::TenantReturnedToDraft,
|
|
actor: $user,
|
|
context: [
|
|
'metadata' => [
|
|
'source' => 'onboarding_draft_resolver',
|
|
'onboarding_session_id' => (int) $resolvedDraft->getKey(),
|
|
],
|
|
],
|
|
);
|
|
|
|
$resolvedDraft->setRelation('tenant', $normalizedTenant);
|
|
}
|
|
|
|
return $resolvedDraft;
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, TenantOnboardingSession>
|
|
*/
|
|
public function resumableDraftsFor(User $user, Workspace $workspace): Collection
|
|
{
|
|
$drafts = TenantOnboardingSession::query()
|
|
->with(['tenant', 'startedByUser', 'updatedByUser'])
|
|
->where('workspace_id', (int) $workspace->getKey())
|
|
->resumable()
|
|
->orderByDesc('updated_at')
|
|
->get();
|
|
|
|
$resolvedDrafts = [];
|
|
|
|
foreach ($drafts as $draft) {
|
|
try {
|
|
Gate::forUser($user)->authorize('view', $draft);
|
|
} catch (AuthorizationException) {
|
|
continue;
|
|
}
|
|
|
|
$resolvedDraft = $this->lifecycleService
|
|
->syncPersistedLifecycle($draft)
|
|
->loadMissing(['tenant', 'startedByUser', 'updatedByUser']);
|
|
|
|
if (! $this->lifecycleService->canResumeDraft($resolvedDraft)) {
|
|
continue;
|
|
}
|
|
|
|
$resolvedDrafts[] = $resolvedDraft;
|
|
}
|
|
|
|
return new Collection($resolvedDrafts);
|
|
}
|
|
}
|