From 57c87ba1ac567b448c63d4e0c381cd7d26944fb1 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 16 Jan 2025 15:35:09 +0100 Subject: [PATCH 01/11] fix server access for admins without subuser --- .../ServerResource/Pages/ListServers.php | 2 +- app/Models/User.php | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/Filament/App/Resources/ServerResource/Pages/ListServers.php b/app/Filament/App/Resources/ServerResource/Pages/ListServers.php index ae9a0fbdfc..8e9ae820b8 100644 --- a/app/Filament/App/Resources/ServerResource/Pages/ListServers.php +++ b/app/Filament/App/Resources/ServerResource/Pages/ListServers.php @@ -19,7 +19,7 @@ class ListServers extends ListRecords public function table(Table $table): Table { - $baseQuery = auth()->user()->can('viewList server') ? Server::query() : auth()->user()->accessibleServers(); + $baseQuery = auth()->user()->accessibleServers(); return $table ->paginated(false) diff --git a/app/Models/User.php b/app/Models/User.php index 9e7fd435a2..5bb61251b4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -289,11 +289,15 @@ public function activity(): MorphToMany } /** - * Returns all the servers that a user can access by way of being the owner of the - * server, or because they are assigned as a subuser for that server. + * Returns all the servers that a user can access. + * Either because they are an admin or because they are the owner/ a subuser of the server. */ public function accessibleServers(): Builder { + if ($this->canned('viewList server')) { + return Server::query(); + } + return Server::query() ->select('servers.*') ->leftJoin('subusers', 'subusers.server_id', '=', 'servers.id') @@ -315,7 +319,12 @@ public function subServers(): BelongsToMany protected function checkPermission(Server $server, string $permission = ''): bool { - if ($this->isRootAdmin() || $server->owner_id === $this->id) { + if ($this->isRootAdmin() || $this->canned('edit server', $server) || $server->owner_id === $this->id) { + return true; + } + + // If the user only has "view" permissions allow viewing the console + if ($permission === Permission::ACTION_WEBSOCKET_CONNECT && $this->canned('view server', $server)) { return true; } @@ -401,7 +410,7 @@ public function getTenants(Panel $panel): array|Collection public function canAccessTenant(IlluminateModel $tenant): bool { if ($tenant instanceof Server) { - if ($this->isRootAdmin() || $tenant->owner_id === $this->id) { + if ($this->isRootAdmin() || $this->canned('view server', $tenant) || $tenant->owner_id === $this->id) { return true; } From 291e6f2e9c8ad90c9f5ce7c9abdcd20303299f66 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 16 Jan 2025 15:36:18 +0100 Subject: [PATCH 02/11] add permission checks to power buttons --- app/Filament/Server/Pages/Console.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Filament/Server/Pages/Console.php b/app/Filament/Server/Pages/Console.php index a133afa3de..3efffed00b 100644 --- a/app/Filament/Server/Pages/Console.php +++ b/app/Filament/Server/Pages/Console.php @@ -10,6 +10,7 @@ // use App\Filament\Server\Widgets\ServerNetworkChart; use App\Filament\Server\Widgets\ServerOverview; use App\Livewire\AlertBanner; +use App\Models\Permission; use App\Models\Server; use Filament\Actions\Action; use Filament\Facades\Filament; @@ -94,16 +95,19 @@ protected function getHeaderActions(): array ->color('primary') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'start', uuid: $server->uuid)) + ->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_START, $server)) ->disabled(fn () => $server->isInConflictState() || !$this->status->isStartable()), Action::make('restart') ->color('gray') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'restart', uuid: $server->uuid)) + ->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_RESTART, $server)) ->disabled(fn () => $server->isInConflictState() || !$this->status->isRestartable()), Action::make('stop') ->color('danger') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'stop', uuid: $server->uuid)) + ->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) ->hidden(fn () => $this->status->isStartingOrStopping() || $this->status->isKillable()) ->disabled(fn () => $server->isInConflictState() || !$this->status->isStoppable()), Action::make('kill') @@ -114,6 +118,7 @@ protected function getHeaderActions(): array ->modalSubmitActionLabel('Kill Server') ->size(ActionSize::ExtraLarge) ->action(fn () => $this->dispatch('setServerState', state: 'kill', uuid: $server->uuid)) + ->authorize(fn () => auth()->user()->can(Permission::ACTION_CONTROL_STOP, $server)) ->hidden(fn () => $server->isInConflictState() || !$this->status->isKillable()), ]; } From 5022118e13c5334d195ad95e3fa6cdadf64df6f7 Mon Sep 17 00:00:00 2001 From: Boy132 Date: Thu, 16 Jan 2025 15:44:01 +0100 Subject: [PATCH 03/11] add permission check for console command sending --- app/Filament/Server/Widgets/ServerConsole.php | 7 +++- .../components/server-console.blade.php | 36 ++++++++++--------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/app/Filament/Server/Widgets/ServerConsole.php b/app/Filament/Server/Widgets/ServerConsole.php index 851b4b3a30..85ced891d5 100644 --- a/app/Filament/Server/Widgets/ServerConsole.php +++ b/app/Filament/Server/Widgets/ServerConsole.php @@ -57,9 +57,14 @@ protected function getSocket(): string return $socket; } + protected function authorizeSendCommand(): bool + { + return $this->user->can(Permission::ACTION_CONTROL_CONSOLE, $this->server); + } + protected function canSendCommand(): bool { - return !$this->server->isInConflictState() && $this->server->retrieveStatus() === 'running'; + return $this->authorizeSendCommand() && !$this->server->isInConflictState() && $this->server->retrieveStatus() === 'running'; } public function up(): void diff --git a/resources/views/filament/components/server-console.blade.php b/resources/views/filament/components/server-console.blade.php index a27d638851..f69f93624e 100644 --- a/resources/views/filament/components/server-console.blade.php +++ b/resources/views/filament/components/server-console.blade.php @@ -11,23 +11,25 @@
-
- - -
+ @if ($this->authorizeSendCommand()) +
+ + +
+ @endif @script