Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Create Database btn on admin side #721

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7160fc9
Add Create Database btn on admin side
notAreYouScared Nov 20, 2024
6363b74
Remove unused function
notAreYouScared Nov 20, 2024
aba55ef
readd function
notAreYouScared Nov 20, 2024
01d7456
replace refreshform function
notAreYouScared Nov 20, 2024
96275e0
add authorize, remove database limit check
notAreYouScared Nov 20, 2024
1aeb017
add random words, use proper name function, catch exceptions on creation
notAreYouScared Nov 20, 2024
f18adda
add validation, match old client area more
notAreYouScared Nov 20, 2024
af3b87f
Merge remote-tracking branch 'origin/main' into feature/create-db
notAreYouScared Nov 22, 2024
ff2d059
Add more authorize to Database tab
notAreYouScared Nov 22, 2024
4592fde
Add confirmation to delete
notAreYouScared Nov 22, 2024
eb79fad
make password hidden / revealable
notAreYouScared Nov 22, 2024
c5e4488
better clarification
notAreYouScared Nov 22, 2024
9324da0
Set default and remove placeholder.
notAreYouScared Nov 22, 2024
203956b
Remove server import, add database model to auth
notAreYouScared Nov 22, 2024
f3c0e02
Make same changes for the database host page
notAreYouScared Nov 22, 2024
c55657f
Update app/Filament/Resources/ServerResource/Pages/EditServer.php
notAreYouScared Nov 23, 2024
0cf715e
Update app/Filament/Resources/DatabaseHostResource/RelationManagers/D…
notAreYouScared Nov 23, 2024
89955cc
Update app/Filament/Resources/DatabaseHostResource/RelationManagers/D…
notAreYouScared Nov 23, 2024
cca1999
Remove each hidden
notAreYouScared Nov 24, 2024
246c90a
Return nothing if user has no perms
notAreYouScared Nov 24, 2024
a9a5a2b
This is the way... Im done messing with it...
notAreYouScared Nov 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 74 additions & 7 deletions app/Filament/Resources/ServerResource/Pages/EditServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -608,7 +609,9 @@ public function form(Form $form): Form
->columnSpanFull(),
]),
Tab::make('Databases')
->hidden(fn (Server $server) => !auth()->user()->can('view database'))
notAreYouScared marked this conversation as resolved.
Show resolved Hide resolved
->icon('tabler-database')
->columns(4)
->schema([
Repeater::make('databases')
->grid()
Expand All @@ -622,35 +625,49 @@ public function form(Form $form): Form
->formatStateUsing(fn ($record) => $record->database)
->hintAction(
Action::make('Delete')
->authorize(fn (Server $server) => auth()->user()->can('delete database'))
notAreYouScared marked this conversation as resolved.
Show resolved Hide resolved
->color('danger')
->icon('tabler-trash')
notAreYouScared marked this conversation as resolved.
Show resolved Hide resolved
->action(fn (DatabaseManagementService $databaseManagementService, $record) => $databaseManagementService->delete($record))
->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();
})
),
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'))
notAreYouScared marked this conversation as resolved.
Show resolved Hide resolved
->icon('tabler-refresh')
->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)
->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()
->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')),
Expand All @@ -659,7 +676,57 @@ public function form(Form $form): Form
->deletable(false)
->addable(false)
->columnSpan(4),
])->columns(4),
Forms\Components\Actions::make([
Action::make('createDatabase')
notAreYouScared marked this conversation as resolved.
Show resolved Hide resolved
Boy132 marked this conversation as resolved.
Show resolved Hide resolved
->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 Database')
->action(function (array $data, DatabaseManagementService $service, Server $server, RandomWordService $randomWordService) {
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);

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([
Select::make('database_host_id')
->label('Database Host')
->required()
->placeholder('Select Database Host')
->relationship('node.databaseHosts', 'name')
->default(fn () => (DatabaseHost::query()->first())?->id)
->selectablePlaceholder(false),
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')
->hintIcon('tabler-question-mark')
->hintIconTooltip('Where connections should be allowed from. Leave blank to allow connections from anywhere.'),
]),
])->alignCenter()->columnSpanFull(),
]),
Tab::make('Actions')
->icon('tabler-settings')
->schema([
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Loading