From 6639fac9c0288e39eb9f3ec84eb6629cbb442348 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry <61919774+saeedvaziry@users.noreply.github.com> Date: Sat, 2 Nov 2024 22:44:02 +0100 Subject: [PATCH] fix dashes on the name and better error handling (#336) --- app/Actions/Database/LinkUser.php | 2 +- app/SSH/HasScripts.php | 2 +- app/SSH/Services/Database/Postgresql.php | 16 ++++++++++++ .../scripts/postgresql/create-user.sh | 2 +- .../Database/scripts/postgresql/create.sh | 3 +-- .../scripts/postgresql/delete-user.sh | 2 +- .../Database/scripts/postgresql/delete.sh | 2 +- .../Database/scripts/postgresql/link.sh | 2 +- .../Database/scripts/postgresql/unlink.sh | 2 +- app/Web/Pages/Servers/CronJobs/Index.php | 17 +++++------- app/Web/Pages/Servers/Databases/Backups.php | 12 ++------- app/Web/Pages/Servers/Databases/Users.php | 12 ++------- .../Servers/Databases/Widgets/BackupsList.php | 16 +++--------- .../Databases/Widgets/DatabaseUsersList.php | 26 ++++--------------- .../Databases/Widgets/DatabasesList.php | 16 +++--------- app/Web/Pages/Servers/Firewall/Index.php | 17 +++++------- 16 files changed, 53 insertions(+), 96 deletions(-) diff --git a/app/Actions/Database/LinkUser.php b/app/Actions/Database/LinkUser.php index d2a4c3d..8b5e8a0 100755 --- a/app/Actions/Database/LinkUser.php +++ b/app/Actions/Database/LinkUser.php @@ -53,7 +53,7 @@ public static function rules(Server $server, array $input): array { return [ 'databases.*' => [ - 'required', + 'nullable', Rule::exists('databases', 'name')->where('server_id', $server->id), ], ]; diff --git a/app/SSH/HasScripts.php b/app/SSH/HasScripts.php index 7a5ddf3..a74b97b 100644 --- a/app/SSH/HasScripts.php +++ b/app/SSH/HasScripts.php @@ -6,7 +6,7 @@ trait HasScripts { - private function getScript(string $name, array $vars = []): string + protected function getScript(string $name, array $vars = []): string { $reflector = new ReflectionClass($this); $scriptsDir = dirname($reflector->getFileName()).'/scripts'; diff --git a/app/SSH/Services/Database/Postgresql.php b/app/SSH/Services/Database/Postgresql.php index 0f06049..2a6916c 100644 --- a/app/SSH/Services/Database/Postgresql.php +++ b/app/SSH/Services/Database/Postgresql.php @@ -2,10 +2,26 @@ namespace App\SSH\Services\Database; +use App\Exceptions\SSHError; + class Postgresql extends AbstractDatabase { protected function getScriptsDir(): string { return 'postgresql'; } + + /** + * @throws SSHError + */ + public function create(string $name): void + { + $this->service->server->ssh()->exec( + $this->getScript($this->getScriptsDir().'/create.sh', [ + 'name' => $name, + 'ssh_user' => $this->service->server->ssh_user, + ]), + 'create-database' + ); + } } diff --git a/app/SSH/Services/Database/scripts/postgresql/create-user.sh b/app/SSH/Services/Database/scripts/postgresql/create-user.sh index 6c747a5..2547722 100755 --- a/app/SSH/Services/Database/scripts/postgresql/create-user.sh +++ b/app/SSH/Services/Database/scripts/postgresql/create-user.sh @@ -1,4 +1,4 @@ -if ! sudo -u postgres psql -c "CREATE ROLE __username__ WITH LOGIN PASSWORD '__password__';"; then +if ! sudo -u postgres psql -c "CREATE ROLE \"__username__\" WITH LOGIN PASSWORD '__password__';"; then echo 'VITO_SSH_ERROR' && exit 1 fi diff --git a/app/SSH/Services/Database/scripts/postgresql/create.sh b/app/SSH/Services/Database/scripts/postgresql/create.sh index 481354f..dbb827a 100644 --- a/app/SSH/Services/Database/scripts/postgresql/create.sh +++ b/app/SSH/Services/Database/scripts/postgresql/create.sh @@ -1,5 +1,4 @@ - -if ! sudo -u postgres psql -c "CREATE DATABASE __name__"; then +if ! sudo -u postgres psql -c "CREATE DATABASE \"__name__\""; then echo 'VITO_SSH_ERROR' && exit 1 fi diff --git a/app/SSH/Services/Database/scripts/postgresql/delete-user.sh b/app/SSH/Services/Database/scripts/postgresql/delete-user.sh index eee0851..b596699 100755 --- a/app/SSH/Services/Database/scripts/postgresql/delete-user.sh +++ b/app/SSH/Services/Database/scripts/postgresql/delete-user.sh @@ -1,4 +1,4 @@ -if ! sudo -u postgres psql -c "DROP USER __username__"; then +if ! sudo -u postgres psql -c "DROP USER \"__username__\""; then echo 'VITO_SSH_ERROR' && exit 1 fi diff --git a/app/SSH/Services/Database/scripts/postgresql/delete.sh b/app/SSH/Services/Database/scripts/postgresql/delete.sh index 811b7c2..ab71c13 100755 --- a/app/SSH/Services/Database/scripts/postgresql/delete.sh +++ b/app/SSH/Services/Database/scripts/postgresql/delete.sh @@ -1,4 +1,4 @@ -if ! sudo -u postgres psql -c "DROP DATABASE __name__"; then +if ! sudo -u postgres psql -c "DROP DATABASE \"__name__\""; then echo 'VITO_SSH_ERROR' && exit 1 fi diff --git a/app/SSH/Services/Database/scripts/postgresql/link.sh b/app/SSH/Services/Database/scripts/postgresql/link.sh index 2459c2e..ff3d511 100755 --- a/app/SSH/Services/Database/scripts/postgresql/link.sh +++ b/app/SSH/Services/Database/scripts/postgresql/link.sh @@ -1,4 +1,4 @@ -if ! sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE __database__ TO __username__;"; then +if ! sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE \"__database__\" TO \"__username__\";"; then echo 'VITO_SSH_ERROR' && exit 1 fi diff --git a/app/SSH/Services/Database/scripts/postgresql/unlink.sh b/app/SSH/Services/Database/scripts/postgresql/unlink.sh index 7c4e3f2..bc0574b 100755 --- a/app/SSH/Services/Database/scripts/postgresql/unlink.sh +++ b/app/SSH/Services/Database/scripts/postgresql/unlink.sh @@ -4,7 +4,7 @@ DATABASES=$(sudo -u postgres psql -t -c "SELECT datname FROM pg_database WHERE d for DB in $DATABASES; do echo "Revoking privileges in database: $DB" - sudo -u postgres psql -d "$DB" -c "REVOKE ALL PRIVILEGES ON DATABASE \"$DB\" FROM $USER_TO_REVOKE;" + sudo -u postgres psql -d "$DB" -c "REVOKE ALL PRIVILEGES ON DATABASE \"$DB\" FROM \"$USER_TO_REVOKE\";" done echo "Privileges revoked from $USER_TO_REVOKE" diff --git a/app/Web/Pages/Servers/CronJobs/Index.php b/app/Web/Pages/Servers/CronJobs/Index.php index 9a736f5..39009a5 100644 --- a/app/Web/Pages/Servers/CronJobs/Index.php +++ b/app/Web/Pages/Servers/CronJobs/Index.php @@ -5,7 +5,6 @@ use App\Actions\CronJob\CreateCronJob; use App\Models\CronJob; use App\Web\Pages\Servers\Page; -use Exception; use Filament\Actions\Action; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; @@ -70,18 +69,16 @@ protected function getHeaderActions(): array ->placeholder('0 * * * *'), ]) ->action(function (array $data) { - try { + run_action($this, function () use ($data) { app(CreateCronJob::class)->create($this->server, $data); - } catch (Exception $e) { + + $this->dispatch('$refresh'); + Notification::make() - ->danger() - ->title($e->getMessage()) + ->success() + ->title('Cron Job created!') ->send(); - - throw $e; - } - - $this->dispatch('$refresh'); + }); }), ]; } diff --git a/app/Web/Pages/Servers/Databases/Backups.php b/app/Web/Pages/Servers/Databases/Backups.php index 171b849..bba8380 100644 --- a/app/Web/Pages/Servers/Databases/Backups.php +++ b/app/Web/Pages/Servers/Databases/Backups.php @@ -8,7 +8,6 @@ use App\Web\Contracts\HasSecondSubNav; use App\Web\Pages\Servers\Page; use App\Web\Pages\Settings\StorageProviders\Actions\Create; -use Exception; use Filament\Actions\Action; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; @@ -73,7 +72,7 @@ protected function getHeaderActions(): array ]) ->modalSubmitActionLabel('Create') ->action(function (array $data) { - try { + run_action($this, function () use ($data) { app(CreateBackup::class)->create($this->server, $data); $this->dispatch('$refresh'); @@ -82,14 +81,7 @@ protected function getHeaderActions(): array ->success() ->title('Backup created!') ->send(); - } catch (Exception $e) { - Notification::make() - ->danger() - ->title($e->getMessage()) - ->send(); - - throw $e; - } + }); }), ]; } diff --git a/app/Web/Pages/Servers/Databases/Users.php b/app/Web/Pages/Servers/Databases/Users.php index ba70042..b43eabe 100644 --- a/app/Web/Pages/Servers/Databases/Users.php +++ b/app/Web/Pages/Servers/Databases/Users.php @@ -7,7 +7,6 @@ use App\Models\DatabaseUser; use App\Web\Contracts\HasSecondSubNav; use App\Web\Pages\Servers\Page; -use Exception; use Filament\Actions\Action; use Filament\Forms\Components\Checkbox; use Filament\Forms\Components\TextInput; @@ -52,7 +51,7 @@ protected function getHeaderActions(): array ]) ->modalSubmitActionLabel('Create') ->action(function (array $data) { - try { + run_action($this, function () use ($data) { app(CreateDatabaseUser::class)->create($this->server, $data); $this->dispatch('$refresh'); @@ -61,14 +60,7 @@ protected function getHeaderActions(): array ->success() ->title('Database user created!') ->send(); - } catch (Exception $e) { - Notification::make() - ->danger() - ->title($e->getMessage()) - ->send(); - - throw $e; - } + }); }), ]; } diff --git a/app/Web/Pages/Servers/Databases/Widgets/BackupsList.php b/app/Web/Pages/Servers/Databases/Widgets/BackupsList.php index 5345eda..519d3b7 100644 --- a/app/Web/Pages/Servers/Databases/Widgets/BackupsList.php +++ b/app/Web/Pages/Servers/Databases/Widgets/BackupsList.php @@ -6,8 +6,6 @@ use App\Models\Backup; use App\Models\BackupFile; use App\Models\Server; -use Exception; -use Filament\Notifications\Notification; use Filament\Support\Enums\MaxWidth; use Filament\Tables\Actions\Action; use Filament\Tables\Columns\TextColumn; @@ -95,18 +93,10 @@ public function getTable(): Table ->authorize(fn (Backup $record) => auth()->user()->can('delete', $record)) ->requiresConfirmation() ->action(function (Backup $record) { - try { + run_action($this, function () use ($record) { $record->delete(); - } catch (Exception $e) { - Notification::make() - ->danger() - ->title($e->getMessage()) - ->send(); - - throw $e; - } - - $this->dispatch('$refresh'); + $this->dispatch('$refresh'); + }); }), ]); } diff --git a/app/Web/Pages/Servers/Databases/Widgets/DatabaseUsersList.php b/app/Web/Pages/Servers/Databases/Widgets/DatabaseUsersList.php index 990c0fc..90741a1 100644 --- a/app/Web/Pages/Servers/Databases/Widgets/DatabaseUsersList.php +++ b/app/Web/Pages/Servers/Databases/Widgets/DatabaseUsersList.php @@ -6,7 +6,6 @@ use App\Actions\Database\LinkUser; use App\Models\DatabaseUser; use App\Models\Server; -use Exception; use Filament\Forms\Components\CheckboxList; use Filament\Forms\Components\TextInput; use Filament\Notifications\Notification; @@ -98,21 +97,14 @@ private function linkAction(): Action ->default(fn (DatabaseUser $record) => $record->databases), ]) ->action(function (DatabaseUser $record, array $data) { - try { + run_action($this, function () use ($record, $data) { app(LinkUser::class)->link($record, $data); Notification::make() ->success() ->title('User linked to databases!') ->send(); - } catch (Exception $e) { - Notification::make() - ->danger() - ->title($e->getMessage()) - ->send(); - - throw $e; - } + }); }); } @@ -127,18 +119,10 @@ private function deleteAction(): Action ->authorize(fn ($record) => auth()->user()->can('delete', $record)) ->requiresConfirmation() ->action(function (DatabaseUser $record) { - try { + run_action($this, function () use ($record) { app(DeleteDatabaseUser::class)->delete($this->server, $record); - } catch (Exception $e) { - Notification::make() - ->danger() - ->title($e->getMessage()) - ->send(); - - throw $e; - } - - $this->dispatch('$refresh'); + $this->dispatch('$refresh'); + }); }); } } diff --git a/app/Web/Pages/Servers/Databases/Widgets/DatabasesList.php b/app/Web/Pages/Servers/Databases/Widgets/DatabasesList.php index 08de517..00f0087 100644 --- a/app/Web/Pages/Servers/Databases/Widgets/DatabasesList.php +++ b/app/Web/Pages/Servers/Databases/Widgets/DatabasesList.php @@ -5,8 +5,6 @@ use App\Actions\Database\DeleteDatabase; use App\Models\Database; use App\Models\Server; -use Exception; -use Filament\Notifications\Notification; use Filament\Tables\Actions\Action; use Filament\Tables\Columns\TextColumn; use Filament\Tables\Table; @@ -56,18 +54,10 @@ public function getTable(): Table ->authorize(fn ($record) => auth()->user()->can('delete', $record)) ->requiresConfirmation() ->action(function (Database $record) { - try { + run_action($this, function () use ($record) { app(DeleteDatabase::class)->delete($this->server, $record); - } catch (Exception $e) { - Notification::make() - ->danger() - ->title($e->getMessage()) - ->send(); - - throw $e; - } - - $this->dispatch('$refresh'); + $this->dispatch('$refresh'); + }); }), ]); } diff --git a/app/Web/Pages/Servers/Firewall/Index.php b/app/Web/Pages/Servers/Firewall/Index.php index 98d8342..365c842 100644 --- a/app/Web/Pages/Servers/Firewall/Index.php +++ b/app/Web/Pages/Servers/Firewall/Index.php @@ -5,7 +5,6 @@ use App\Actions\FirewallRule\CreateRule; use App\Models\FirewallRule; use App\Web\Pages\Servers\Page; -use Exception; use Filament\Actions\Action; use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; @@ -69,18 +68,16 @@ protected function getHeaderActions(): array ->rules(CreateRule::rules()['mask']), ]) ->action(function (array $data) { - try { + run_action($this, function () use ($data) { app(CreateRule::class)->create($this->server, $data); - } catch (Exception $e) { + + $this->dispatch('$refresh'); + Notification::make() - ->danger() - ->title($e->getMessage()) + ->success() + ->title('Firewall rule created!') ->send(); - - throw $e; - } - - $this->dispatch('$refresh'); + }); }), ]; }