From 7160fc9b4779be13234edb5022d716f377ab5d6d Mon Sep 17 00:00:00 2001 From: notCharles Date: Tue, 19 Nov 2024 20:40:34 -0500 Subject: [PATCH 01/23] Add Create Database btn on admin side --- .../ServerResource/Pages/EditServer.php | 44 ++++++++++++++++++- app/Models/Node.php | 5 +++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index e3479b35ec..4bf05a21a2 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -7,6 +7,7 @@ use App\Filament\Resources\ServerResource; use App\Filament\Resources\ServerResource\RelationManagers\AllocationsRelationManager; use App\Models\Database; +use App\Models\DatabaseHost; use App\Models\Egg; use App\Models\Mount; use App\Models\Server; @@ -609,6 +610,7 @@ public function form(Form $form): Form ]), Tab::make('Databases') ->icon('tabler-database') + ->columns(4) ->schema([ Repeater::make('databases') ->grid() @@ -624,7 +626,10 @@ public function form(Form $form): Form Action::make('Delete') ->color('danger') ->icon('tabler-trash') - ->action(fn (DatabaseManagementService $databaseManagementService, $record) => $databaseManagementService->delete($record)) + ->action(function (DatabaseManagementService $databaseManagementService, $record) { + $databaseManagementService->delete($record); + $this->refreshForm(); + }) ), TextInput::make('username') ->disabled() @@ -659,7 +664,37 @@ public function form(Form $form): Form ->deletable(false) ->addable(false) ->columnSpan(4), - ])->columns(4), + Forms\Components\Actions::make([ + Action::make('createDatabase') + ->disabled(fn (Server $server) => DatabaseHost::query()->count() < 1 || $server->databases()->count() >= $server->database_limit) + ->label(fn (Server $server) => $server->databases()->count() >= $server->database_limit ? 'Database Limit Reached' : (DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database')) + ->color(fn (Server $server) => $server->databases()->count() >= $server->database_limit || DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') + ->modalSubmitActionLabel('Create') + ->action(function (array $data, DatabaseManagementService $service, Server $server) { + if (empty($data['database'])) { + $data['database'] = str_random(12); + } + $data['database'] = 's'. $server->id . '_' . $data['database']; + $service->create($server, $data); + $this->refreshForm(); + }) + ->form([ + Select::make('database_host_id') + ->label('Database Host') + ->placeholder('Select Database Host') + ->relationship('node.databaseHosts', 'name'), + TextInput::make('database') + ->label('Database Name') + ->prefix(fn (Server $server) => 's' . $server->id . '_') + ->hintIcon('tabler-question-mark') + ->hintIconTooltip('Leaving this blank will auto generate a random name'), + TextInput::make('remote') + ->columnSpan(1) + ->label('Connections From') + ->default('%'), + ]), + ])->alignCenter()->columnSpanFull(), + ]), Tab::make('Actions') ->icon('tabler-settings') ->schema([ @@ -870,4 +905,9 @@ protected function rotatePassword(DatabasePasswordService $service, Database $re $set('password', $newPassword); $set('JDBC', $jdbcString); } + + public function refreshForm(): void + { + $this->fillForm(); + } } diff --git a/app/Models/Node.php b/app/Models/Node.php index ea31ba2fdf..e13f0a592f 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -243,6 +243,11 @@ public function allocations(): HasMany return $this->hasMany(Allocation::class); } + public function databaseHosts(): HasMany + { + return $this->hasMany(DatabaseHost::class); + } + /** * Returns a boolean if the node is viable for an additional server to be placed on it. */ From 6363b74fba996f11fc4308bc4839fe87d4fa1358 Mon Sep 17 00:00:00 2001 From: notCharles Date: Tue, 19 Nov 2024 20:43:00 -0500 Subject: [PATCH 02/23] Remove unused function --- app/Models/Node.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/Models/Node.php b/app/Models/Node.php index e13f0a592f..ea31ba2fdf 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -243,11 +243,6 @@ public function allocations(): HasMany return $this->hasMany(Allocation::class); } - public function databaseHosts(): HasMany - { - return $this->hasMany(DatabaseHost::class); - } - /** * Returns a boolean if the node is viable for an additional server to be placed on it. */ From aba55ef4e8380151f7bf6239f8b23674d8a9e4f0 Mon Sep 17 00:00:00 2001 From: notCharles Date: Wed, 20 Nov 2024 17:06:58 -0500 Subject: [PATCH 03/23] readd function --- app/Models/Node.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Models/Node.php b/app/Models/Node.php index ea31ba2fdf..e13f0a592f 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -243,6 +243,11 @@ public function allocations(): HasMany return $this->hasMany(Allocation::class); } + public function databaseHosts(): HasMany + { + return $this->hasMany(DatabaseHost::class); + } + /** * Returns a boolean if the node is viable for an additional server to be placed on it. */ From 01d745675b0a042f87e0fc58ce5a951ad44a1840 Mon Sep 17 00:00:00 2001 From: notCharles Date: Wed, 20 Nov 2024 17:09:51 -0500 Subject: [PATCH 04/23] replace refreshform function --- .../Resources/ServerResource/Pages/EditServer.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 4bf05a21a2..14e1e7cd8c 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -628,7 +628,7 @@ public function form(Form $form): Form ->icon('tabler-trash') ->action(function (DatabaseManagementService $databaseManagementService, $record) { $databaseManagementService->delete($record); - $this->refreshForm(); + $this->fillForm(); }) ), TextInput::make('username') @@ -676,7 +676,8 @@ public function form(Form $form): Form } $data['database'] = 's'. $server->id . '_' . $data['database']; $service->create($server, $data); - $this->refreshForm(); + + $this->fillForm(); }) ->form([ Select::make('database_host_id') @@ -905,9 +906,4 @@ protected function rotatePassword(DatabasePasswordService $service, Database $re $set('password', $newPassword); $set('JDBC', $jdbcString); } - - public function refreshForm(): void - { - $this->fillForm(); - } } From 96275e0d81b25482696d726dc45854df516a5e91 Mon Sep 17 00:00:00 2001 From: notCharles Date: Wed, 20 Nov 2024 17:10:23 -0500 Subject: [PATCH 05/23] add authorize, remove database limit check --- app/Filament/Resources/ServerResource/Pages/EditServer.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 14e1e7cd8c..71dae75f19 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -666,9 +666,10 @@ public function form(Form $form): Form ->columnSpan(4), Forms\Components\Actions::make([ Action::make('createDatabase') - ->disabled(fn (Server $server) => DatabaseHost::query()->count() < 1 || $server->databases()->count() >= $server->database_limit) - ->label(fn (Server $server) => $server->databases()->count() >= $server->database_limit ? 'Database Limit Reached' : (DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database')) - ->color(fn (Server $server) => $server->databases()->count() >= $server->database_limit || DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') + ->authorize(fn (Server $server) => auth()->user()->can('create database')) + ->disabled(fn (Server $server) => DatabaseHost::query()->count() < 1) + ->label(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database') + ->color(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') ->modalSubmitActionLabel('Create') ->action(function (array $data, DatabaseManagementService $service, Server $server) { if (empty($data['database'])) { From 1aeb0170b037cd4eb1770144e2a6f3eecdf97af5 Mon Sep 17 00:00:00 2001 From: notCharles Date: Wed, 20 Nov 2024 17:11:17 -0500 Subject: [PATCH 06/23] add random words, use proper name function, catch exceptions on creation --- .../ServerResource/Pages/EditServer.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 71dae75f19..f01dbd8292 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -671,13 +671,22 @@ public function form(Form $form): Form ->label(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database') ->color(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') ->modalSubmitActionLabel('Create') - ->action(function (array $data, DatabaseManagementService $service, Server $server) { + ->action(function (array $data, DatabaseManagementService $service, Server $server, RandomWordService $randomWordService) { if (empty($data['database'])) { - $data['database'] = str_random(12); + $data['database'] = $randomWordService->word() . random_int(1, 420); } - $data['database'] = 's'. $server->id . '_' . $data['database']; - $service->create($server, $data); + $data['database'] = $service->generateUniqueDatabaseName($data['database'], $server->id); + + try { + $service->setValidateDatabaseLimit(false)->create($server, $data); + } catch (Exception $e) { + Notification::make() + ->title('Failed to Create Database') + ->body($e->getMessage()) + ->danger() + ->persistent()->send(); + } $this->fillForm(); }) ->form([ From f18addaf0fd8ac857dc2e4b12e93e14d12435500 Mon Sep 17 00:00:00 2001 From: notCharles Date: Wed, 20 Nov 2024 17:23:13 -0500 Subject: [PATCH 07/23] add validation, match old client area more --- .../Resources/ServerResource/Pages/EditServer.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index f01dbd8292..a4509874ae 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -675,6 +675,9 @@ public function form(Form $form): Form if (empty($data['database'])) { $data['database'] = $randomWordService->word() . random_int(1, 420); } + if (empty($data['remote'])) { + $data['remote'] = '%'; + } $data['database'] = $service->generateUniqueDatabaseName($data['database'], $server->id); @@ -692,17 +695,21 @@ public function form(Form $form): Form ->form([ Select::make('database_host_id') ->label('Database Host') + ->required() ->placeholder('Select Database Host') ->relationship('node.databaseHosts', 'name'), TextInput::make('database') ->label('Database Name') + ->alphaDash() ->prefix(fn (Server $server) => 's' . $server->id . '_') ->hintIcon('tabler-question-mark') ->hintIconTooltip('Leaving this blank will auto generate a random name'), TextInput::make('remote') ->columnSpan(1) + ->regex('/^[\w\-\/.%:]+$/') ->label('Connections From') - ->default('%'), + ->hintIcon('tabler-question-mark') + ->hintIconTooltip('Where connections should be allowed from. Leave blank to allow connections from anywhere.'), ]), ])->alignCenter()->columnSpanFull(), ]), From ff2d0598830b07262ace97796d350bc52f0fac72 Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 09:30:45 -0500 Subject: [PATCH 08/23] Add more authorize to Database tab --- app/Filament/Resources/ServerResource/Pages/EditServer.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index a4509874ae..b27d2ead1f 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -609,6 +609,7 @@ public function form(Form $form): Form ->columnSpanFull(), ]), Tab::make('Databases') + ->hidden(fn (Server $server) => !auth()->user()->can('view database')) ->icon('tabler-database') ->columns(4) ->schema([ @@ -624,6 +625,7 @@ public function form(Form $form): Form ->formatStateUsing(fn ($record) => $record->database) ->hintAction( Action::make('Delete') + ->authorize(fn (Server $server) => auth()->user()->can('delete database')) ->color('danger') ->icon('tabler-trash') ->action(function (DatabaseManagementService $databaseManagementService, $record) { @@ -639,6 +641,7 @@ public function form(Form $form): Form ->disabled() ->hintAction( Action::make('rotate') + ->authorize(fn (Server $server) => auth()->user()->can('update database')) ->icon('tabler-refresh') ->requiresConfirmation() ->action(fn (DatabasePasswordService $service, $record, $set, $get) => $this->rotatePassword($service, $record, $set, $get)) From 4592fde6a97d27284b844f25af074d95ddda2515 Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 09:38:25 -0500 Subject: [PATCH 09/23] Add confirmation to delete --- app/Filament/Resources/ServerResource/Pages/EditServer.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index b27d2ead1f..6530dfc2a0 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -628,6 +628,11 @@ public function form(Form $form): Form ->authorize(fn (Server $server) => auth()->user()->can('delete database')) ->color('danger') ->icon('tabler-trash') + ->requiresConfirmation() + ->modalIcon('tabler-database-x') + ->modalHeading('Delete Database?') + ->modalSubmitActionLabel(fn (Get $get) => 'Delete ' . $get('database') . '?') + ->modalDescription(fn (Get $get) => 'Are you sure you want to delete ' . $get('database') . '?') ->action(function (DatabaseManagementService $databaseManagementService, $record) { $databaseManagementService->delete($record); $this->fillForm(); From eb79fad90eb17aa6572d818e3230b2cc69c1ed7c Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 09:38:52 -0500 Subject: [PATCH 10/23] make password hidden / revealable --- .../Resources/ServerResource/Pages/EditServer.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 6530dfc2a0..0d02325bca 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -641,9 +641,12 @@ public function form(Form $form): Form TextInput::make('username') ->disabled() ->formatStateUsing(fn ($record) => $record->username) - ->columnSpan(2), + ->columnSpan(1), TextInput::make('password') ->disabled() + ->password() + ->revealable() + ->columnSpan(1) ->hintAction( Action::make('rotate') ->authorize(fn (Server $server) => auth()->user()->can('update database')) @@ -651,8 +654,7 @@ public function form(Form $form): Form ->requiresConfirmation() ->action(fn (DatabasePasswordService $service, $record, $set, $get) => $this->rotatePassword($service, $record, $set, $get)) ) - ->formatStateUsing(fn (Database $database) => $database->password) - ->columnSpan(2), + ->formatStateUsing(fn (Database $database) => $database->password), TextInput::make('remote') ->disabled() ->formatStateUsing(fn ($record) => $record->remote) @@ -664,6 +666,8 @@ public function form(Form $form): Form ->columnSpan(1), TextInput::make('JDBC') ->disabled() + ->password() + ->revealable() ->label('JDBC Connection String') ->columnSpan(2) ->formatStateUsing(fn (Get $get, $record) => 'jdbc:mysql://' . $get('username') . ':' . urlencode($record->password) . '@' . $record->host->host . ':' . $record->host->port . '/' . $get('database')), From c5e4488d9d78208e8b2851ecb40f289588ef65b3 Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 09:41:04 -0500 Subject: [PATCH 11/23] better clarification --- app/Filament/Resources/ServerResource/Pages/EditServer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 0d02325bca..53f0ece268 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -657,12 +657,12 @@ public function form(Form $form): Form ->formatStateUsing(fn (Database $database) => $database->password), TextInput::make('remote') ->disabled() - ->formatStateUsing(fn ($record) => $record->remote) + ->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote) ->columnSpan(1) ->label('Connections From'), TextInput::make('max_connections') ->disabled() - ->formatStateUsing(fn ($record) => $record->max_connections) + ->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections) ->columnSpan(1), TextInput::make('JDBC') ->disabled() From 9324da06bb559c99b2827667dce5cb9de7d1dc56 Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 09:48:07 -0500 Subject: [PATCH 12/23] Set default and remove placeholder. --- app/Filament/Resources/ServerResource/Pages/EditServer.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 53f0ece268..419df7e9be 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -682,7 +682,7 @@ public function form(Form $form): Form ->disabled(fn (Server $server) => DatabaseHost::query()->count() < 1) ->label(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database') ->color(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') - ->modalSubmitActionLabel('Create') + ->modalSubmitActionLabel('Create Database') ->action(function (array $data, DatabaseManagementService $service, Server $server, RandomWordService $randomWordService) { if (empty($data['database'])) { $data['database'] = $randomWordService->word() . random_int(1, 420); @@ -709,7 +709,9 @@ public function form(Form $form): Form ->label('Database Host') ->required() ->placeholder('Select Database Host') - ->relationship('node.databaseHosts', 'name'), + ->relationship('node.databaseHosts', 'name') + ->default(fn () => (DatabaseHost::query()->first())?->id) + ->selectablePlaceholder(false), TextInput::make('database') ->label('Database Name') ->alphaDash() From 203956b017ca40736dc1c8646e0cdb4ce689fd2c Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 18:51:48 -0500 Subject: [PATCH 13/23] Remove server import, add database model to auth --- .../ServerResource/Pages/EditServer.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 419df7e9be..7e3ba5edd2 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -609,7 +609,7 @@ public function form(Form $form): Form ->columnSpanFull(), ]), Tab::make('Databases') - ->hidden(fn (Server $server) => !auth()->user()->can('view database')) + ->hidden(fn () => !auth()->user()->can('viewList database')) ->icon('tabler-database') ->columns(4) ->schema([ @@ -625,7 +625,7 @@ public function form(Form $form): Form ->formatStateUsing(fn ($record) => $record->database) ->hintAction( Action::make('Delete') - ->authorize(fn (Server $server) => auth()->user()->can('delete database')) + ->authorize(fn (Database $database) => auth()->user()->can('delete database', $database)) ->color('danger') ->icon('tabler-trash') ->requiresConfirmation() @@ -649,10 +649,11 @@ public function form(Form $form): Form ->columnSpan(1) ->hintAction( Action::make('rotate') - ->authorize(fn (Server $server) => auth()->user()->can('update database')) + ->authorize(fn (Database $database) => auth()->user()->can('update database', $database)) ->icon('tabler-refresh') - ->requiresConfirmation() + ->modalHeading('Change Database Password?') ->action(fn (DatabasePasswordService $service, $record, $set, $get) => $this->rotatePassword($service, $record, $set, $get)) + ->requiresConfirmation() ) ->formatStateUsing(fn (Database $database) => $database->password), TextInput::make('remote') @@ -678,10 +679,10 @@ public function form(Form $form): Form ->columnSpan(4), Forms\Components\Actions::make([ Action::make('createDatabase') - ->authorize(fn (Server $server) => auth()->user()->can('create database')) - ->disabled(fn (Server $server) => DatabaseHost::query()->count() < 1) - ->label(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database') - ->color(fn (Server $server) => DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') + ->authorize(fn (Database $database) => auth()->user()->can('create database', $database)) + ->disabled(fn () => DatabaseHost::query()->count() < 1) + ->label(fn () => DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database') + ->color(fn () => DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') ->modalSubmitActionLabel('Create Database') ->action(function (array $data, DatabaseManagementService $service, Server $server, RandomWordService $randomWordService) { if (empty($data['database'])) { From f3c0e0253f58c26990189d4744e3628cf3160617 Mon Sep 17 00:00:00 2001 From: notCharles Date: Fri, 22 Nov 2024 18:52:04 -0500 Subject: [PATCH 14/23] Make same changes for the database host page --- .../DatabasesRelationManager.php | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php index dd77c6ae22..c36834343d 100644 --- a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php +++ b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php @@ -24,21 +24,29 @@ public function form(Form $form): Form { return $form ->schema([ - TextInput::make('database')->columnSpanFull(), + TextInput::make('database') + ->columnSpanFull(), TextInput::make('username'), TextInput::make('password') + ->password() + ->revealable() ->hintAction( Action::make('rotate') ->icon('tabler-refresh') ->requiresConfirmation() ->action(fn (DatabasePasswordService $service, Database $database, $set, $get) => $this->rotatePassword($service, $database, $set, $get)) + ->authorize(fn () => auth()->user()->can('update database')) ) ->formatStateUsing(fn (Database $database) => $database->password), - TextInput::make('remote')->label('Connections From'), - TextInput::make('max_connections'), + TextInput::make('remote')->label('Connections From') + ->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote), + TextInput::make('max_connections') + ->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections), TextInput::make('JDBC') ->label('JDBC Connection String') ->columnSpanFull() + ->password() + ->revealable() ->formatStateUsing(fn (Get $get, Database $database) => 'jdbc:mysql://' . $get('username') . ':' . urlencode($database->password) . '@' . $database->host->host . ':' . $database->host->port . '/' . $get('database')), ]); } @@ -48,18 +56,29 @@ public function table(Table $table): Table return $table ->recordTitleAttribute('servers') ->columns([ - TextColumn::make('database')->icon('tabler-database'), - TextColumn::make('username')->icon('tabler-user'), - TextColumn::make('remote'), + TextColumn::make('database') + ->hidden(fn () => !auth()->user()->can('viewList database')) + ->icon('tabler-database'), + TextColumn::make('username') + ->hidden(fn () => !auth()->user()->can('viewList database')) + ->icon('tabler-user'), + TextColumn::make('remote') + ->hidden(fn () => !auth()->user()->can('viewList database')), TextColumn::make('server.name') + ->hidden(fn () => !auth()->user()->can('viewList database')) ->icon('tabler-brand-docker') ->url(fn (Database $database) => route('filament.admin.resources.servers.edit', ['record' => $database->server_id])), - TextColumn::make('max_connections'), - DateTimeColumn::make('created_at'), + TextColumn::make('max_connections') + ->hidden(fn () => !auth()->user()->can('viewList database')), + DateTimeColumn::make('created_at') + ->hidden(fn () => !auth()->user()->can('viewList database')), ]) ->actions([ - DeleteAction::make(), - ViewAction::make()->color('primary'), + DeleteAction::make() + ->authorize(fn () => auth()->user()->can('delete database')), + ViewAction::make() + ->color('primary') + ->hidden(fn () => !auth()->user()->can('viewList database')), ]); } From c55657fc2c191959134a895a7bcaeff4327a8f67 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 23 Nov 2024 11:40:44 -0500 Subject: [PATCH 15/23] Update app/Filament/Resources/ServerResource/Pages/EditServer.php Co-authored-by: Boy132 --- app/Filament/Resources/ServerResource/Pages/EditServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Resources/ServerResource/Pages/EditServer.php b/app/Filament/Resources/ServerResource/Pages/EditServer.php index 7e3ba5edd2..a61a4e3bba 100644 --- a/app/Filament/Resources/ServerResource/Pages/EditServer.php +++ b/app/Filament/Resources/ServerResource/Pages/EditServer.php @@ -679,7 +679,7 @@ public function form(Form $form): Form ->columnSpan(4), Forms\Components\Actions::make([ Action::make('createDatabase') - ->authorize(fn (Database $database) => auth()->user()->can('create database', $database)) + ->authorize(fn () => auth()->user()->can('create database')) ->disabled(fn () => DatabaseHost::query()->count() < 1) ->label(fn () => DatabaseHost::query()->count() < 1 ? 'No Database Hosts' : 'Create Database') ->color(fn () => DatabaseHost::query()->count() < 1 ? 'danger' : 'primary') From 0cf715e85202f04a29179a61a0f2e74a9b9656f1 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 23 Nov 2024 11:40:52 -0500 Subject: [PATCH 16/23] Update app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php Co-authored-by: Boy132 --- .../RelationManagers/DatabasesRelationManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php index c36834343d..fce3d4f76f 100644 --- a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php +++ b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php @@ -35,7 +35,7 @@ public function form(Form $form): Form ->icon('tabler-refresh') ->requiresConfirmation() ->action(fn (DatabasePasswordService $service, Database $database, $set, $get) => $this->rotatePassword($service, $database, $set, $get)) - ->authorize(fn () => auth()->user()->can('update database')) + ->authorize(fn (Database $database) => auth()->user()->can('update database', $database)) ) ->formatStateUsing(fn (Database $database) => $database->password), TextInput::make('remote')->label('Connections From') From 89955cc9c64000c6674a63c0aeb335f33afa1fa6 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 23 Nov 2024 11:40:59 -0500 Subject: [PATCH 17/23] Update app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php Co-authored-by: Boy132 --- .../RelationManagers/DatabasesRelationManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php index fce3d4f76f..f7c386277d 100644 --- a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php +++ b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php @@ -75,7 +75,7 @@ public function table(Table $table): Table ]) ->actions([ DeleteAction::make() - ->authorize(fn () => auth()->user()->can('delete database')), + ->authorize(fn (Database $database) => auth()->user()->can('delete database', $database)), ViewAction::make() ->color('primary') ->hidden(fn () => !auth()->user()->can('viewList database')), From cca1999abf8fecd25d9f7eaa85abd21af230e055 Mon Sep 17 00:00:00 2001 From: notCharles Date: Sat, 23 Nov 2024 20:36:59 -0500 Subject: [PATCH 18/23] Remove each hidden --- .../RelationManagers/DatabasesRelationManager.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php index f7c386277d..3f6d8809d6 100644 --- a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php +++ b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php @@ -57,21 +57,17 @@ public function table(Table $table): Table ->recordTitleAttribute('servers') ->columns([ TextColumn::make('database') - ->hidden(fn () => !auth()->user()->can('viewList database')) ->icon('tabler-database'), TextColumn::make('username') - ->hidden(fn () => !auth()->user()->can('viewList database')) ->icon('tabler-user'), TextColumn::make('remote') - ->hidden(fn () => !auth()->user()->can('viewList database')), + ->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote), TextColumn::make('server.name') - ->hidden(fn () => !auth()->user()->can('viewList database')) ->icon('tabler-brand-docker') ->url(fn (Database $database) => route('filament.admin.resources.servers.edit', ['record' => $database->server_id])), TextColumn::make('max_connections') - ->hidden(fn () => !auth()->user()->can('viewList database')), - DateTimeColumn::make('created_at') - ->hidden(fn () => !auth()->user()->can('viewList database')), + ->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections), + DateTimeColumn::make('created_at'), ]) ->actions([ DeleteAction::make() From 246c90a931ebb9dcdec16be8b307abe8071095ef Mon Sep 17 00:00:00 2001 From: notCharles Date: Sat, 23 Nov 2024 20:37:22 -0500 Subject: [PATCH 19/23] Return nothing if user has no perms --- .../Resources/DatabaseHostResource/Pages/EditDatabaseHost.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php index 5d0176699a..ddf50376cb 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php @@ -101,6 +101,10 @@ protected function getFormActions(): array public function getRelationManagers(): array { + if (auth()->user()->cannot('viewList database')) { + return []; + } + return [ DatabasesRelationManager::class, ]; From a9a5a2bd6f27765107db18170777337ba3eeeab0 Mon Sep 17 00:00:00 2001 From: notCharles Date: Mon, 25 Nov 2024 20:04:44 -0500 Subject: [PATCH 20/23] This is the way... Im done messing with it... --- .../DatabaseHostResource/Pages/EditDatabaseHost.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php index ddf50376cb..e4988e979a 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php @@ -101,13 +101,11 @@ protected function getFormActions(): array public function getRelationManagers(): array { - if (auth()->user()->cannot('viewList database')) { - return []; + if (auth()->user()->can('viewList database')) { + return [DatabasesRelationManager::class]; } - return [ - DatabasesRelationManager::class, - ]; + return []; } protected function handleRecordUpdate(Model $record, array $data): Model From fb9900c59f99904f22c23d23237fbde7adc4b85c Mon Sep 17 00:00:00 2001 From: notCharles Date: Sat, 30 Nov 2024 21:56:02 -0500 Subject: [PATCH 21/23] Fix view permission for relationship manager --- .../DatabaseHostResource/Pages/EditDatabaseHost.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php index e4988e979a..424482fde2 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php @@ -101,8 +101,10 @@ protected function getFormActions(): array public function getRelationManagers(): array { - if (auth()->user()->can('viewList database')) { - return [DatabasesRelationManager::class]; + if (DatabasesRelationManager::canViewForRecord($this->getRecord(), static::class)) { + return [ + DatabasesRelationManager::class, + ]; } return []; From 38218372ec9b2e052d374ffcf409d48db82e1bd8 Mon Sep 17 00:00:00 2001 From: MartinOscar <40749467+RMartinOscar@users.noreply.github.com> Date: Sun, 1 Dec 2024 03:58:40 +0100 Subject: [PATCH 22/23] Update app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php --- .../RelationManagers/DatabasesRelationManager.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php index 3f6d8809d6..bdb09bd7f9 100644 --- a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php +++ b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php @@ -38,7 +38,8 @@ public function form(Form $form): Form ->authorize(fn (Database $database) => auth()->user()->can('update database', $database)) ) ->formatStateUsing(fn (Database $database) => $database->password), - TextInput::make('remote')->label('Connections From') + TextInput::make('remote') + ->label('Connections From') ->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote), TextInput::make('max_connections') ->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections), From ea88a4a65a07f5585afca54dcb69a2541c4e3728 Mon Sep 17 00:00:00 2001 From: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com> Date: Sun, 1 Dec 2024 03:00:38 +0000 Subject: [PATCH 23/23] Pint --- .../RelationManagers/DatabasesRelationManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php index bdb09bd7f9..448245e421 100644 --- a/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php +++ b/app/Filament/Resources/DatabaseHostResource/RelationManagers/DatabasesRelationManager.php @@ -39,7 +39,7 @@ public function form(Form $form): Form ) ->formatStateUsing(fn (Database $database) => $database->password), TextInput::make('remote') - ->label('Connections From') + ->label('Connections From') ->formatStateUsing(fn ($record) => $record->remote === '%' ? 'Anywhere ( % )' : $record->remote), TextInput::make('max_connections') ->formatStateUsing(fn ($record) => $record->max_connections === 0 ? 'Unlimited' : $record->max_connections),