@php use App\Filament\Pages\ChooseWorkspace; use App\Models\Tenant; use App\Models\User; use App\Models\WorkspaceMembership; use App\Services\Auth\WorkspaceRoleCapabilityMap; use App\Support\Auth\Capabilities; use App\Support\Workspaces\WorkspaceContext; use Filament\Facades\Filament; /** @var WorkspaceContext $workspaceContext */ $workspaceContext = app(WorkspaceContext::class); $workspace = $workspaceContext->currentWorkspace(request()); $user = auth()->user(); $canSeeAllWorkspaceTenants = false; if ($user instanceof User && $workspace) { $roles = WorkspaceRoleCapabilityMap::rolesWithCapability(Capabilities::WORKSPACE_MEMBERSHIP_MANAGE); $canSeeAllWorkspaceTenants = WorkspaceMembership::query() ->where('workspace_id', (int) $workspace->getKey()) ->where('user_id', (int) $user->getKey()) ->whereIn('role', $roles) ->exists(); } $tenants = collect(); if ($user instanceof User && $workspace) { if ($canSeeAllWorkspaceTenants) { $tenants = Tenant::query() ->where('workspace_id', (int) $workspace->getKey()) ->orderBy('name') ->get(); } else { $tenants = collect($user->getTenants(Filament::getCurrentOrDefaultPanel())) ->filter(fn ($tenant): bool => $tenant instanceof Tenant && (int) $tenant->workspace_id === (int) $workspace->getKey()) ->values(); } } $currentTenant = Filament::getTenant(); $currentTenantName = $currentTenant instanceof Tenant ? $currentTenant->getFilamentName() : null; $path = '/'.ltrim(request()->path(), '/'); $isTenantScopedRoute = request()->route()?->hasParameter('tenant') || str_starts_with($path, '/admin/t/'); $lastTenantId = $workspaceContext->lastTenantId(request()); $canClearTenantContext = $currentTenantName !== null || $lastTenantId !== null; @endphp
{{ $workspace?->name ?? 'Select workspace' }} Switch workspace
@if (! $workspace)
Select tenant
Choose a workspace first.
@elseif ($isTenantScopedRoute) {{ $currentTenantName ?? 'Tenant' }} @else {{ $currentTenantName ?? 'Select tenant' }}
Tenant context @if ($canSeeAllWorkspaceTenants) · all workspace tenants @endif
@if ($tenants->isEmpty())
{{ $canSeeAllWorkspaceTenants ? 'No tenants exist in this workspace.' : 'No tenants you can access in this workspace.' }}
@else
@foreach ($tenants as $tenant)
@csrf
@endforeach
@if ($canClearTenantContext)
@csrf Clear tenant context
@endif
Switching tenants is explicit. Canonical monitoring URLs do not change tenant context.
@endif
@endif