## Summary - migrate provider connections to the canonical three-dimension state model: lifecycle via `is_enabled`, consent via `consent_status`, and verification via `verification_status` - remove legacy provider status and health badge paths, update admin and system directory surfaces, and align onboarding, consent callback, verification, resolver, and mutation flows with the new model - add the Spec 188 artifact set, schema migrations, guard coverage, and expanded provider-state tests across admin, system, onboarding, verification, and rendering paths ## Verification - `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent` - `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/Auth/SystemPanelAuthTest.php tests/Feature/Filament/TenantGlobalSearchLifecycleScopeTest.php tests/Feature/ProviderConnections/ProviderConnectionEnableDisableTest.php tests/Feature/ProviderConnections/ProviderConnectionTruthCleanupSpec179Test.php` - integrated browser smoke: validated admin provider list/detail/edit, tenant provider summary, system directory tenant detail, provider-connection search exclusion, and cleaned up the temporary smoke record afterward ## Filament / implementation notes - Livewire v4.0+ compliance: preserved; this change targets Filament v5 on Livewire v4 and does not introduce older APIs - Provider registration location: unchanged; Laravel 11+ panel providers remain registered in `bootstrap/providers.php` - Globally searchable resources: `ProviderConnectionResource` remains intentionally excluded from global search; tenant global search remains enabled and continues to resolve to view pages - Destructive actions: no new destructive action surface was introduced without confirmation or authorization; existing capability checks continue to gate provider mutations - Asset strategy: unchanged; no new Filament assets were added, so deploy behavior for `php artisan filament:assets` remains unchanged - Testing plan covered: system auth, tenant global search, provider lifecycle enable/disable behavior, and provider truth cleanup cutover behavior Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #219
201 lines
7.0 KiB
PHP
201 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ProviderConnection;
|
|
use App\Models\Tenant;
|
|
use App\Services\Intune\AuditLogger;
|
|
use App\Support\Providers\ProviderConnectionType;
|
|
use App\Support\Providers\ProviderConsentStatus;
|
|
use App\Support\Providers\ProviderReasonCodes;
|
|
use App\Support\Providers\ProviderVerificationStatus;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
use Symfony\Component\HttpFoundation\Response as ResponseAlias;
|
|
|
|
class AdminConsentCallbackController extends Controller
|
|
{
|
|
/**
|
|
* Handle the incoming request.
|
|
*/
|
|
public function __invoke(
|
|
Request $request,
|
|
AuditLogger $auditLogger,
|
|
): View {
|
|
$expectedState = $request->session()->pull('tenant_onboard_state');
|
|
$workspaceId = $request->session()->pull('tenant_onboard_workspace_id');
|
|
$tenantKey = $request->string('tenant')->toString();
|
|
$state = $request->string('state')->toString();
|
|
$tenantIdentifier = $tenantKey ?: $this->parseState($state);
|
|
|
|
if ($expectedState && $expectedState !== $state) {
|
|
abort(ResponseAlias::HTTP_FORBIDDEN, 'Invalid consent state');
|
|
}
|
|
|
|
abort_if(empty($tenantIdentifier), 404);
|
|
|
|
$tenant = $this->resolveTenant($tenantIdentifier, is_numeric($workspaceId) ? (int) $workspaceId : null);
|
|
|
|
$error = $request->string('error')->toString() ?: null;
|
|
$consentGranted = $request->has('admin_consent')
|
|
? filter_var($request->input('admin_consent'), FILTER_VALIDATE_BOOLEAN)
|
|
: null;
|
|
|
|
$status = match (true) {
|
|
$error !== null => 'error',
|
|
$consentGranted === false => 'consent_denied',
|
|
$consentGranted === true => 'ok',
|
|
default => 'pending',
|
|
};
|
|
|
|
$connection = $this->upsertProviderConnectionForConsent(
|
|
tenant: $tenant,
|
|
status: $status,
|
|
error: $error,
|
|
);
|
|
|
|
$legacyStatus = $status === 'ok' ? 'success' : 'failed';
|
|
$auditMetadata = [
|
|
'source' => 'admin.consent.callback',
|
|
'workspace_id' => (int) $connection->workspace_id,
|
|
'status' => $status,
|
|
'state' => $state,
|
|
'error' => $error,
|
|
'consent' => $consentGranted,
|
|
'provider_connection_id' => (int) $connection->getKey(),
|
|
'provider' => (string) $connection->provider,
|
|
'entra_tenant_id' => (string) $connection->entra_tenant_id,
|
|
'connection_type' => $connection->connection_type->value,
|
|
'consent_status' => $connection->consent_status->value,
|
|
'verification_status' => $connection->verification_status->value,
|
|
];
|
|
|
|
$auditLogger->log(
|
|
tenant: $tenant,
|
|
action: 'tenant.consent.callback',
|
|
context: [
|
|
'metadata' => $auditMetadata,
|
|
],
|
|
status: $legacyStatus,
|
|
resourceType: 'provider_connection',
|
|
resourceId: (string) $connection->getKey(),
|
|
);
|
|
|
|
$auditLogger->log(
|
|
tenant: $tenant,
|
|
action: 'provider_connection.consent_result',
|
|
context: [
|
|
'metadata' => $auditMetadata,
|
|
],
|
|
status: $legacyStatus,
|
|
resourceType: 'provider_connection',
|
|
resourceId: (string) $connection->getKey(),
|
|
);
|
|
|
|
return view('admin-consent-callback', [
|
|
'tenant' => $tenant,
|
|
'connection' => $connection,
|
|
'status' => $status,
|
|
'error' => $error,
|
|
'consentGranted' => $consentGranted,
|
|
]);
|
|
}
|
|
|
|
private function resolveTenant(string $tenantIdentifier, ?int $workspaceId): Tenant
|
|
{
|
|
/** @var Tenant|null $tenant */
|
|
$tenant = Tenant::withTrashed()
|
|
->forTenant($tenantIdentifier)
|
|
->first();
|
|
|
|
if ($tenant?->trashed()) {
|
|
$tenant->restore();
|
|
}
|
|
|
|
if ($tenant instanceof Tenant) {
|
|
if ($tenant->workspace_id === null && $workspaceId !== null) {
|
|
$tenant->forceFill(['workspace_id' => $workspaceId])->save();
|
|
}
|
|
|
|
return $tenant;
|
|
}
|
|
|
|
abort_if($workspaceId === null, ResponseAlias::HTTP_FORBIDDEN, 'Missing workspace context');
|
|
|
|
return Tenant::create([
|
|
'tenant_id' => $tenantIdentifier,
|
|
'name' => 'New Tenant',
|
|
'workspace_id' => $workspaceId,
|
|
]);
|
|
}
|
|
|
|
private function upsertProviderConnectionForConsent(Tenant $tenant, string $status, ?string $error): ProviderConnection
|
|
{
|
|
$hasDefault = ProviderConnection::query()
|
|
->where('tenant_id', (int) $tenant->getKey())
|
|
->where('provider', 'microsoft')
|
|
->where('is_default', true)
|
|
->exists();
|
|
|
|
$consentStatus = match ($status) {
|
|
'ok' => ProviderConsentStatus::Granted,
|
|
'error' => ProviderConsentStatus::Failed,
|
|
default => ProviderConsentStatus::Required,
|
|
};
|
|
$verificationStatus = ProviderVerificationStatus::Unknown;
|
|
$reasonCode = match ($status) {
|
|
'ok' => null,
|
|
'error' => ProviderReasonCodes::ProviderAuthFailed,
|
|
default => ProviderReasonCodes::ProviderConsentMissing,
|
|
};
|
|
|
|
$connection = ProviderConnection::query()->updateOrCreate(
|
|
[
|
|
'tenant_id' => (int) $tenant->getKey(),
|
|
'provider' => 'microsoft',
|
|
'entra_tenant_id' => (string) ($tenant->graphTenantId() ?? $tenant->tenant_id ?? $tenant->external_id),
|
|
],
|
|
[
|
|
'workspace_id' => (int) $tenant->workspace_id,
|
|
'display_name' => (string) ($tenant->name ?? 'Microsoft Connection'),
|
|
'is_enabled' => true,
|
|
'connection_type' => ProviderConnectionType::Platform->value,
|
|
'consent_status' => $consentStatus->value,
|
|
'consent_granted_at' => $status === 'ok' ? now() : null,
|
|
'consent_last_checked_at' => now(),
|
|
'consent_error_code' => $status === 'error' ? $reasonCode : null,
|
|
'consent_error_message' => $status === 'error' ? $error : null,
|
|
'verification_status' => $verificationStatus->value,
|
|
'migration_review_required' => false,
|
|
'migration_reviewed_at' => null,
|
|
'last_error_reason_code' => $reasonCode,
|
|
'last_error_message' => $status === 'ok' ? null : $error,
|
|
'is_default' => $hasDefault ? false : true,
|
|
],
|
|
);
|
|
|
|
$connection->credential()->delete();
|
|
|
|
if (! $hasDefault && ! $connection->is_default) {
|
|
$connection->makeDefault();
|
|
}
|
|
|
|
return $connection->fresh();
|
|
}
|
|
|
|
private function parseState(?string $state): ?string
|
|
{
|
|
if (empty($state)) {
|
|
return null;
|
|
}
|
|
|
|
if (str_contains($state, '|')) {
|
|
[, $value] = explode('|', $state, 2);
|
|
|
|
return $value;
|
|
}
|
|
|
|
return $state;
|
|
}
|
|
}
|