## Summary
- add a canonical queued execution legitimacy contract for actor-bound and system-authority operation runs
- enforce legitimacy before queued jobs transition runs to running across provider, inventory, restore, bulk, sync, and scheduled backup flows
- surface blocked execution outcomes consistently in Monitoring, notifications, audit data, and the tenantless operation viewer
- add Spec 149 artifacts and focused Pest coverage for legitimacy decisions, middleware ordering, blocked presentation, retry behavior, and cross-family adoption
## Testing
- vendor/bin/sail artisan test --compact tests/Unit/Operations/QueuedExecutionLegitimacyGateTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/QueuedExecutionMiddlewareOrderingTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Verification/ProviderExecutionReauthorizationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/RunInventorySyncExecutionReauthorizationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/ExecuteRestoreRunExecutionReauthorizationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/SystemRunBlockedExecutionNotificationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/BulkOperationExecutionReauthorizationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/QueuedExecutionRetryReauthorizationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/QueuedExecutionContractMatrixTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/OperationRunBlockedExecutionPresentationTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/QueuedExecutionAuditTrailTest.php
- vendor/bin/sail artisan test --compact tests/Feature/Operations/TenantlessOperationRunViewerTest.php
- vendor/bin/sail bin pint --dirty --format agent
## Manual validation
- validated queued provider execution blocking for tenant operability drift in the integrated browser on /admin/operations and /admin/operations/{run}
- validated 404 vs 403 route behavior for non-membership vs in-scope capability denial
- validated initiator-null blocked system-run behavior without creating a user terminal notification
Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #179
165 lines
6.1 KiB
PHP
165 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Verification;
|
|
|
|
use App\Jobs\ProviderConnectionHealthCheckJob;
|
|
use App\Models\OperationRun;
|
|
use App\Models\ProviderConnection;
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Providers\ProviderConnectionResolver;
|
|
use App\Services\Providers\ProviderConnectionStateProjector;
|
|
use App\Services\Providers\ProviderIdentityResolver;
|
|
use App\Services\Providers\ProviderOperationStartGate;
|
|
use App\Services\Providers\ProviderOperationStartResult;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\Operations\ExecutionAuthorityMode;
|
|
use App\Support\Providers\ProviderVerificationStatus;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use InvalidArgumentException;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
final class StartVerification
|
|
{
|
|
public function __construct(
|
|
private readonly ProviderOperationStartGate $providers,
|
|
private readonly ProviderConnectionResolver $connections,
|
|
private readonly ProviderIdentityResolver $identityResolver,
|
|
private readonly ProviderConnectionStateProjector $stateProjector,
|
|
) {}
|
|
|
|
/**
|
|
* Start (or dedupe) a provider-connection verification run.
|
|
*
|
|
* @param array<string, mixed> $extraContext
|
|
*/
|
|
public function providerConnectionCheck(
|
|
Tenant $tenant,
|
|
ProviderConnection $connection,
|
|
User $initiator,
|
|
array $extraContext = [],
|
|
): ProviderOperationStartResult {
|
|
return $this->providerConnectionCheckUsingConnection(
|
|
tenant: $tenant,
|
|
connection: $connection,
|
|
initiator: $initiator,
|
|
extraContext: $extraContext,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Start (or dedupe) a provider-connection verification run for the tenant default connection.
|
|
*
|
|
* @param array<string, mixed> $extraContext
|
|
*/
|
|
public function providerConnectionCheckForTenant(
|
|
Tenant $tenant,
|
|
User $initiator,
|
|
array $extraContext = [],
|
|
): ProviderOperationStartResult {
|
|
if (! $initiator->canAccessTenant($tenant)) {
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
Gate::forUser($initiator)->authorize(Capabilities::PROVIDER_RUN, $tenant);
|
|
|
|
$resolution = $this->connections->resolveDefault($tenant, 'microsoft');
|
|
|
|
if ($resolution->resolved && $resolution->connection instanceof ProviderConnection) {
|
|
return $this->providerConnectionCheckUsingConnection(
|
|
tenant: $tenant,
|
|
connection: $resolution->connection,
|
|
initiator: $initiator,
|
|
extraContext: $extraContext,
|
|
);
|
|
}
|
|
|
|
return $this->providers->start(
|
|
tenant: $tenant,
|
|
connection: null,
|
|
operationType: 'provider.connection.check',
|
|
dispatcher: fn (OperationRun $run): mixed => $this->dispatchConnectionHealthCheck($run, $tenant, $initiator),
|
|
initiator: $initiator,
|
|
extraContext: array_merge($extraContext, [
|
|
'execution_authority_mode' => ExecutionAuthorityMode::ActorBound->value,
|
|
'required_capability' => Capabilities::PROVIDER_RUN,
|
|
]),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Start (or dedupe) a provider-connection verification run for an explicit connection.
|
|
*
|
|
* @param array<string, mixed> $extraContext
|
|
*/
|
|
public function providerConnectionCheckUsingConnection(
|
|
Tenant $tenant,
|
|
ProviderConnection $connection,
|
|
User $initiator,
|
|
array $extraContext = [],
|
|
): ProviderOperationStartResult {
|
|
if (! $initiator->canAccessTenant($tenant)) {
|
|
throw new NotFoundHttpException;
|
|
}
|
|
|
|
Gate::forUser($initiator)->authorize(Capabilities::PROVIDER_RUN, $tenant);
|
|
|
|
$identity = $this->identityResolver->resolve($connection);
|
|
|
|
$result = $this->providers->start(
|
|
tenant: $tenant,
|
|
connection: $connection,
|
|
operationType: 'provider.connection.check',
|
|
dispatcher: fn (OperationRun $run): mixed => $this->dispatchConnectionHealthCheck($run, $tenant, $initiator),
|
|
initiator: $initiator,
|
|
extraContext: array_merge($extraContext, [
|
|
'execution_authority_mode' => ExecutionAuthorityMode::ActorBound->value,
|
|
'required_capability' => Capabilities::PROVIDER_RUN,
|
|
'identity' => [
|
|
'connection_type' => $identity->connectionType->value,
|
|
'credential_source' => $identity->credentialSource,
|
|
'effective_client_id' => $identity->effectiveClientId,
|
|
],
|
|
]),
|
|
);
|
|
|
|
if ($result->status === 'started') {
|
|
$projectedState = $this->stateProjector->project(
|
|
connectionType: $connection->connection_type,
|
|
consentStatus: $connection->consent_status,
|
|
verificationStatus: ProviderVerificationStatus::Pending,
|
|
currentStatus: is_string($connection->status) ? $connection->status : null,
|
|
);
|
|
|
|
$connection->update([
|
|
'verification_status' => ProviderVerificationStatus::Pending,
|
|
'status' => $projectedState['status'],
|
|
'health_status' => $projectedState['health_status'],
|
|
'last_error_reason_code' => null,
|
|
'last_error_message' => null,
|
|
]);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function dispatchConnectionHealthCheck(OperationRun $run, Tenant $tenant, User $initiator): mixed
|
|
{
|
|
$context = is_array($run->context ?? null) ? $run->context : [];
|
|
$providerConnectionId = $context['provider_connection_id'] ?? null;
|
|
|
|
if (! is_numeric($providerConnectionId)) {
|
|
throw new InvalidArgumentException('Provider connection id is missing from run context.');
|
|
}
|
|
|
|
return ProviderConnectionHealthCheckJob::dispatch(
|
|
tenantId: (int) $tenant->getKey(),
|
|
userId: (int) $initiator->getKey(),
|
|
providerConnectionId: (int) $providerConnectionId,
|
|
operationRun: $run,
|
|
);
|
|
}
|
|
}
|