Implements workspace-first enforcement and UX: - Workspace selected before tenant flows; /admin routes into choose-workspace/choose-tenant - Tenant lists and default tenant selection are scoped to current workspace - Workspaces UI is tenantless at /admin/workspaces Security hardening: - Workspaces can never have 0 owners (blocks last-owner removal/demotion) - Blocked attempts are audited with action_id=workspace_membership.last_owner_blocked + required metadata - Optional break-glass recovery page to re-assign workspace owner (audited) Tests: - Added/updated Pest feature tests covering redirects, scoping, tenantless workspaces, last-owner guards, and break-glass recovery. Notes: - Filament v5 strict Page property signatures respected in RepairWorkspaceOwners. Co-authored-by: Ahmed Darrazi <ahmeddarrazi@MacBookPro.fritz.box> Reviewed-on: #86
68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Filament\Pages\ChooseTenant;
|
|
use App\Filament\Pages\TenantDashboard;
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
final class SwitchWorkspaceController
|
|
{
|
|
public function __invoke(Request $request): RedirectResponse
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'workspace_id' => ['required', 'integer'],
|
|
]);
|
|
|
|
$workspace = Workspace::query()->whereKey($validated['workspace_id'])->first();
|
|
|
|
if (! $workspace instanceof Workspace) {
|
|
abort(404);
|
|
}
|
|
|
|
if (! empty($workspace->archived_at)) {
|
|
abort(404);
|
|
}
|
|
|
|
$context = app(WorkspaceContext::class);
|
|
|
|
if (! $context->isMember($user, $workspace)) {
|
|
abort(404);
|
|
}
|
|
|
|
$context->setCurrentWorkspace($workspace, $user, $request);
|
|
|
|
$tenantsQuery = $user->tenants()
|
|
->where('workspace_id', $workspace->getKey())
|
|
->where('status', 'active');
|
|
|
|
$tenantCount = (int) $tenantsQuery->count();
|
|
|
|
if ($tenantCount === 0) {
|
|
return redirect()->route('admin.workspace.managed-tenants.index', ['workspace' => $workspace->slug ?? $workspace->getKey()]);
|
|
}
|
|
|
|
if ($tenantCount === 1) {
|
|
$tenant = $tenantsQuery->first();
|
|
|
|
if ($tenant !== null) {
|
|
return redirect()->to(TenantDashboard::getUrl(tenant: $tenant));
|
|
}
|
|
}
|
|
|
|
return redirect()->to(ChooseTenant::getUrl());
|
|
}
|
|
}
|