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
173 lines
4.8 KiB
PHP
173 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Workspace;
|
|
use App\Models\WorkspaceMembership;
|
|
use App\Support\Workspaces\WorkspaceContext;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
class ChooseWorkspace extends Page
|
|
{
|
|
protected static string $layout = 'filament-panels::components.layout.simple';
|
|
|
|
protected static bool $shouldRegisterNavigation = false;
|
|
|
|
protected static bool $isDiscovered = false;
|
|
|
|
protected static ?string $slug = 'choose-workspace';
|
|
|
|
protected static ?string $title = 'Choose workspace';
|
|
|
|
protected string $view = 'filament.pages.choose-workspace';
|
|
|
|
/**
|
|
* @return array<Action>
|
|
*/
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('createWorkspace')
|
|
->label('Create workspace')
|
|
->modalHeading('Create workspace')
|
|
->form([
|
|
TextInput::make('name')
|
|
->required()
|
|
->maxLength(255),
|
|
TextInput::make('slug')
|
|
->helperText('Optional. Used in URLs if set.')
|
|
->maxLength(255)
|
|
->rules(['nullable', 'string', 'max:255', 'alpha_dash', 'unique:workspaces,slug'])
|
|
->dehydrateStateUsing(fn ($state) => filled($state) ? $state : null)
|
|
->dehydrated(fn ($state) => filled($state)),
|
|
])
|
|
->action(fn (array $data) => $this->createWorkspace($data)),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Workspace>
|
|
*/
|
|
public function getWorkspaces(): Collection
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
return Workspace::query()->whereRaw('1 = 0')->get();
|
|
}
|
|
|
|
return Workspace::query()
|
|
->whereIn('id', function ($query) use ($user): void {
|
|
$query->from('workspace_memberships')
|
|
->select('workspace_id')
|
|
->where('user_id', $user->getKey());
|
|
})
|
|
->whereNull('archived_at')
|
|
->orderBy('name')
|
|
->get();
|
|
}
|
|
|
|
public function selectWorkspace(int $workspaceId): void
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
$workspace = Workspace::query()->whereKey($workspaceId)->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());
|
|
|
|
$this->redirect($this->redirectAfterWorkspaceSelected($user));
|
|
}
|
|
|
|
/**
|
|
* @param array{name: string, slug?: string|null} $data
|
|
*/
|
|
public function createWorkspace(array $data): void
|
|
{
|
|
$user = auth()->user();
|
|
|
|
if (! $user instanceof User) {
|
|
abort(403);
|
|
}
|
|
|
|
$workspace = Workspace::query()->create([
|
|
'name' => $data['name'],
|
|
'slug' => $data['slug'] ?? null,
|
|
]);
|
|
|
|
WorkspaceMembership::query()->create([
|
|
'workspace_id' => $workspace->getKey(),
|
|
'user_id' => $user->getKey(),
|
|
'role' => 'owner',
|
|
]);
|
|
|
|
app(WorkspaceContext::class)->setCurrentWorkspace($workspace, $user, request());
|
|
|
|
Notification::make()
|
|
->title('Workspace created')
|
|
->success()
|
|
->send();
|
|
|
|
$this->redirect($this->redirectAfterWorkspaceSelected($user));
|
|
}
|
|
|
|
private function redirectAfterWorkspaceSelected(User $user): string
|
|
{
|
|
$workspaceId = app(WorkspaceContext::class)->currentWorkspaceId();
|
|
|
|
if ($workspaceId === null) {
|
|
return self::getUrl();
|
|
}
|
|
|
|
$workspace = Workspace::query()->whereKey($workspaceId)->first();
|
|
|
|
if (! $workspace instanceof Workspace) {
|
|
return self::getUrl();
|
|
}
|
|
|
|
$tenantsQuery = $user->tenants()
|
|
->where('workspace_id', $workspace->getKey())
|
|
->where('status', 'active');
|
|
|
|
$tenantCount = (int) $tenantsQuery->count();
|
|
|
|
if ($tenantCount === 0) {
|
|
return route('admin.workspace.managed-tenants.index', ['workspace' => $workspace->slug ?? $workspace->getKey()]);
|
|
}
|
|
|
|
if ($tenantCount === 1) {
|
|
$tenant = $tenantsQuery->first();
|
|
|
|
if ($tenant !== null) {
|
|
return TenantDashboard::getUrl(tenant: $tenant);
|
|
}
|
|
}
|
|
|
|
return ChooseTenant::getUrl();
|
|
}
|
|
}
|