165 lines
4.7 KiB
PHP
165 lines
4.7 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\Facades\Filament;
|
|
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
|
|
{
|
|
$tenants = $user->getTenants(Filament::getCurrentOrDefaultPanel());
|
|
|
|
$tenants = $tenants instanceof Collection ? $tenants : collect($tenants);
|
|
|
|
if ($tenants->isEmpty()) {
|
|
$workspaceId = app(WorkspaceContext::class)->currentWorkspaceId();
|
|
|
|
if ($workspaceId !== null) {
|
|
$role = WorkspaceMembership::query()
|
|
->where('workspace_id', $workspaceId)
|
|
->where('user_id', $user->getKey())
|
|
->value('role');
|
|
|
|
if (in_array($role, ['owner', 'manager'], true)) {
|
|
return route('filament.admin.tenant.registration');
|
|
}
|
|
}
|
|
|
|
return ChooseTenant::getUrl();
|
|
}
|
|
|
|
return ChooseTenant::getUrl();
|
|
}
|
|
}
|