Summary Consolidates the “Tenant Operate Hub” work (Spec 085) and the follow-up adjustments from the 086 session merge into a single branch ready to merge into dev. Primary focus: stabilize Ops/Operate Hub UX flows, tighten/align authorization semantics, and make the full Sail test suite green. Key Changes Ops UX / Verification Readonly members can view verification operation runs (reports) while starting verification remains restricted. Normalized failure reason-code handling and aligned UX expectations with the provider reason-code taxonomy. Onboarding wizard UX “Start verification” CTA is hidden while a verification run is active; “Refresh” is shown during in-progress runs. Treats provider_permission_denied as a blocking reason (while keeping legacy compatibility). Test + fixture hardening Standardized use of default provider connection fixtures in tests where sync/restore flows require it. Fixed multiple Filament URL/tenant-context test cases to avoid 404s and reduce tenancy routing brittleness. Policy sync / restore safety Enrollment configuration type collision classification tests now exercise the real sync path (with required provider connection present). Restore edge-case safety tests updated to reflect current provider-connection requirements. Testing vendor/bin/sail artisan test --compact (green) vendor/bin/sail bin pint --dirty (green) Notes Includes merged 086 session work already (no separate PR needed). Co-authored-by: Ahmed Darrazi <ahmeddarrazi@ebc83aaa-d947-4a08-b88e-bd72ac9645f7.fritz.box> Co-authored-by: Ahmed Darrazi <ahmeddarrazi@MacBookPro.fritz.box> Co-authored-by: Ahmed Darrazi <ahmeddarrazi@adsmac.fritz.box> Reviewed-on: #103
212 lines
6.9 KiB
PHP
212 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Middleware;
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use App\Models\WorkspaceMembership;
|
|
use App\Services\Auth\WorkspaceRoleCapabilityMap;
|
|
use App\Support\Auth\Capabilities;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Closure;
|
|
use Filament\Facades\Filament;
|
|
use Filament\Models\Contracts\HasTenants;
|
|
use Filament\Navigation\NavigationBuilder;
|
|
use Filament\Navigation\NavigationItem;
|
|
use Illuminate\Http\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class EnsureFilamentTenantSelected
|
|
{
|
|
/**
|
|
* @param Closure(Request): Response $next
|
|
*/
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
$panel = Filament::getCurrentOrDefaultPanel();
|
|
|
|
$path = '/'.ltrim($request->path(), '/');
|
|
|
|
$workspaceContext = app(WorkspaceContext::class);
|
|
$workspaceId = $workspaceContext->currentWorkspaceId($request);
|
|
|
|
$existingTenant = Filament::getTenant();
|
|
if ($existingTenant instanceof Tenant && $workspaceId !== null && (int) $existingTenant->workspace_id !== (int) $workspaceId) {
|
|
Filament::setTenant(null, true);
|
|
$existingTenant = null;
|
|
}
|
|
|
|
$user = $request->user();
|
|
if ($existingTenant instanceof Tenant && $user instanceof User && ! $user->canAccessTenant($existingTenant)) {
|
|
Filament::setTenant(null, true);
|
|
}
|
|
|
|
if ($path === '/livewire/update') {
|
|
$refererPath = parse_url((string) $request->headers->get('referer', ''), PHP_URL_PATH) ?? '';
|
|
$refererPath = '/'.ltrim((string) $refererPath, '/');
|
|
|
|
if (preg_match('#^/admin/operations/[^/]+$#', $refererPath) === 1) {
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
}
|
|
|
|
if (preg_match('#^/admin/operations/[^/]+$#', $path) === 1) {
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
if ($path === '/admin/operations') {
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
$tenantParameter = null;
|
|
if ($request->route()?->hasParameter('tenant')) {
|
|
$tenantParameter = $request->route()->parameter('tenant');
|
|
} elseif (filled($request->query('tenant'))) {
|
|
$tenantParameter = $request->query('tenant');
|
|
}
|
|
|
|
if ($tenantParameter !== null) {
|
|
$user = $request->user();
|
|
|
|
if ($user === null) {
|
|
return $next($request);
|
|
}
|
|
|
|
if (! $user instanceof HasTenants) {
|
|
abort(404);
|
|
}
|
|
|
|
$tenant = $tenantParameter instanceof Tenant
|
|
? $tenantParameter
|
|
: Tenant::query()->withTrashed()->where('external_id', (string) $tenantParameter)->first();
|
|
|
|
if (! $tenant instanceof Tenant) {
|
|
abort(404);
|
|
}
|
|
|
|
if ($workspaceId === null) {
|
|
abort(404);
|
|
}
|
|
|
|
if ((int) $tenant->workspace_id !== (int) $workspaceId) {
|
|
abort(404);
|
|
}
|
|
|
|
$workspace = Workspace::query()->whereKey($workspaceId)->first();
|
|
|
|
if (! $workspace instanceof Workspace) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $user instanceof User || ! $workspaceContext->isMember($user, $workspace)) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! $user->canAccessTenant($tenant)) {
|
|
abort(404);
|
|
}
|
|
|
|
Filament::setTenant($tenant, true);
|
|
|
|
app(WorkspaceContext::class)->rememberLastTenantId((int) $workspaceId, (int) $tenant->getKey(), $request);
|
|
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
if (
|
|
str_starts_with($path, '/admin/w/')
|
|
|| str_starts_with($path, '/admin/workspaces')
|
|
|| str_starts_with($path, '/admin/operations')
|
|
|| in_array($path, ['/admin/choose-workspace', '/admin/choose-tenant', '/admin/no-access', '/admin/alerts', '/admin/audit-log', '/admin/onboarding'], true)
|
|
) {
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
if (filled(Filament::getTenant())) {
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
if (! $user instanceof User) {
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
$this->configureNavigationForRequest($panel);
|
|
|
|
return $next($request);
|
|
}
|
|
|
|
private function configureNavigationForRequest(\Filament\Panel $panel): void
|
|
{
|
|
if (! $panel->hasTenancy()) {
|
|
return;
|
|
}
|
|
|
|
if (filled(Filament::getTenant())) {
|
|
$panel->navigation(true);
|
|
|
|
return;
|
|
}
|
|
|
|
$panel->navigation(function (): NavigationBuilder {
|
|
return app(NavigationBuilder::class)
|
|
->item(
|
|
NavigationItem::make('Manage workspaces')
|
|
->url(fn (): string => route('filament.admin.resources.workspaces.index'))
|
|
->icon('heroicon-o-squares-2x2')
|
|
->group('Settings')
|
|
->sort(10)
|
|
->visible(function (): bool {
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
return false;
|
|
}
|
|
|
|
$roles = WorkspaceRoleCapabilityMap::rolesWithCapability(Capabilities::WORKSPACE_MEMBERSHIP_MANAGE);
|
|
|
|
return WorkspaceMembership::query()
|
|
->where('user_id', (int) $user->getKey())
|
|
->whereIn('role', $roles)
|
|
->exists();
|
|
}),
|
|
)
|
|
->item(
|
|
NavigationItem::make('Operations')
|
|
->url(fn (): string => route('admin.operations.index'))
|
|
->icon('heroicon-o-queue-list')
|
|
->group('Monitoring')
|
|
->sort(10),
|
|
)
|
|
->item(
|
|
NavigationItem::make('Alerts')
|
|
->url(fn (): string => '/admin/alerts')
|
|
->icon('heroicon-o-bell-alert')
|
|
->group('Monitoring')
|
|
->sort(20),
|
|
)
|
|
->item(
|
|
NavigationItem::make('Audit Log')
|
|
->url(fn (): string => '/admin/audit-log')
|
|
->icon('heroicon-o-clipboard-document-list')
|
|
->group('Monitoring')
|
|
->sort(30),
|
|
);
|
|
});
|
|
}
|
|
}
|