fix dashes on the name and better error handling (#336)

This commit is contained in:
Saeed Vaziry 2024-11-02 22:44:02 +01:00 committed by GitHub
parent f743611b22
commit 6639fac9c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 53 additions and 96 deletions

View File

@ -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),
],
];

View File

@ -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';

View File

@ -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'
);
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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) {
Notification::make()
->danger()
->title($e->getMessage())
->send();
throw $e;
}
$this->dispatch('$refresh');
Notification::make()
->success()
->title('Cron Job created!')
->send();
});
}),
];
}

View File

@ -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;
}
});
}),
];
}

View File

@ -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;
}
});
}),
];
}

View File

@ -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');
});
}),
]);
}

View File

@ -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');
});
});
}
}

View File

@ -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');
});
}),
]);
}

View File

@ -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) {
Notification::make()
->danger()
->title($e->getMessage())
->send();
throw $e;
}
$this->dispatch('$refresh');
Notification::make()
->success()
->title('Firewall rule created!')
->send();
});
}),
];
}