Prevent Select All

Prevent Select all on allocations, prevent people from trying to delete 30,000 ports at once ....
This commit is contained in:
notCharles 2024-11-13 16:21:27 -05:00
parent d9016702d6
commit a787af7a06
2 changed files with 26 additions and 30 deletions

View File

@ -46,8 +46,9 @@ class AllocationsRelationManager extends RelationManager
// ->checkIfRecordIsSelectableUsing(fn (Allocation $allocation) => $allocation->id !== $allocation->server?->allocation_id) // ->checkIfRecordIsSelectableUsing(fn (Allocation $allocation) => $allocation->id !== $allocation->server?->allocation_id)
// All assigned allocations // All assigned allocations
->checkIfRecordIsSelectableUsing(fn (Allocation $allocation) => $allocation->server_id === null) ->checkIfRecordIsSelectableUsing(fn(Allocation $allocation) => $allocation->server_id === null)
->searchable() ->searchable()
->selectCurrentPageOnly() //Prevent people from trying to nuke 30,000 ports at once.... -,-
->columns([ ->columns([
TextColumn::make('id'), TextColumn::make('id'),
TextColumn::make('port') TextColumn::make('port')
@ -57,7 +58,7 @@ class AllocationsRelationManager extends RelationManager
->label('Server') ->label('Server')
->icon('tabler-brand-docker') ->icon('tabler-brand-docker')
->searchable() ->searchable()
->url(fn (Allocation $allocation): string => $allocation->server ? route('filament.admin.resources.servers.edit', ['record' => $allocation->server]) : ''), ->url(fn(Allocation $allocation): string => $allocation->server ? route('filament.admin.resources.servers.edit', ['record' => $allocation->server]) : ''),
TextInputColumn::make('ip_alias') TextInputColumn::make('ip_alias')
->searchable() ->searchable()
->label('Alias'), ->label('Alias'),
@ -65,15 +66,9 @@ class AllocationsRelationManager extends RelationManager
->searchable() ->searchable()
->label('IP'), ->label('IP'),
]) ])
->filters([
//
])
->actions([
//
])
->headerActions([ ->headerActions([
Tables\Actions\Action::make('create new allocation')->label('Create Allocations') Tables\Actions\Action::make('create new allocation')->label('Create Allocations')
->form(fn () => [ ->form(fn() => [
TextInput::make('allocation_ip') TextInput::make('allocation_ip')
->datalist($this->getOwnerRecord()->ipAddresses()) ->datalist($this->getOwnerRecord()->ipAddresses())
->label('IP Address') ->label('IP Address')
@ -103,7 +98,7 @@ class AllocationsRelationManager extends RelationManager
foreach ($state as $portEntry) { foreach ($state as $portEntry) {
if (!str_contains($portEntry, '-')) { if (!str_contains($portEntry, '-')) {
if (is_numeric($portEntry)) { if (is_numeric($portEntry)) {
$ports->push((int) $portEntry); $ports->push((int)$portEntry);
continue; continue;
} }
@ -120,8 +115,8 @@ class AllocationsRelationManager extends RelationManager
continue; continue;
} }
$start = max((int) $start, 0); $start = max((int)$start, 0);
$end = min((int) $end, 2 ** 16 - 1); $end = min((int)$end, 2 ** 16 - 1);
foreach (range($start, $end) as $i) { foreach (range($start, $end) as $i) {
$ports->push($i); $ports->push($i);
} }
@ -139,7 +134,7 @@ class AllocationsRelationManager extends RelationManager
$ports = $sortedPorts; $ports = $sortedPorts;
} }
$ports = $ports->filter(fn ($port) => $port > 1024 && $port < 65535)->values(); $ports = $ports->filter(fn($port) => $port > 1024 && $port < 65535)->values();
if ($update) { if ($update) {
$set('allocation_ports', $ports->all()); $set('allocation_ports', $ports->all());
@ -148,12 +143,12 @@ class AllocationsRelationManager extends RelationManager
->splitKeys(['Tab', ' ', ',']) ->splitKeys(['Tab', ' ', ','])
->required(), ->required(),
]) ])
->action(fn (array $data, AssignmentService $service) => $service->handle($this->getOwnerRecord(), $data)), ->action(fn(array $data, AssignmentService $service) => $service->handle($this->getOwnerRecord(), $data)),
]) ])
->bulkActions([ ->bulkActions([
BulkActionGroup::make([ BulkActionGroup::make([
DeleteBulkAction::make() DeleteBulkAction::make()
->authorize(fn () => auth()->user()->can('delete allocation')), ->authorize(fn() => auth()->user()->can('delete allocation')),
]), ]),
]); ]);
} }

View File

@ -40,36 +40,37 @@ class AllocationsRelationManager extends RelationManager
public function table(Table $table): Table public function table(Table $table): Table
{ {
return $table return $table
->selectCurrentPageOnly()
->recordTitleAttribute('ip') ->recordTitleAttribute('ip')
->recordTitle(fn (Allocation $allocation) => "$allocation->ip:$allocation->port") ->recordTitle(fn(Allocation $allocation) => "$allocation->ip:$allocation->port")
->checkIfRecordIsSelectableUsing(fn (Allocation $record) => $record->id !== $this->getOwnerRecord()->allocation_id) ->checkIfRecordIsSelectableUsing(fn(Allocation $record) => $record->id !== $this->getOwnerRecord()->allocation_id)
->inverseRelationship('server') ->inverseRelationship('server')
->columns([ ->columns([
TextColumn::make('ip')->label('IP'), TextColumn::make('ip')->label('IP'),
TextColumn::make('port')->label('Port'), TextColumn::make('port')->label('Port'),
TextInputColumn::make('ip_alias')->label('Alias'), TextInputColumn::make('ip_alias')->label('Alias'),
IconColumn::make('primary') IconColumn::make('primary')
->icon(fn ($state) => match ($state) { ->icon(fn($state) => match ($state) {
true => 'tabler-star-filled', true => 'tabler-star-filled',
default => 'tabler-star', default => 'tabler-star',
}) })
->color(fn ($state) => match ($state) { ->color(fn($state) => match ($state) {
true => 'warning', true => 'warning',
default => 'gray', default => 'gray',
}) })
->action(fn (Allocation $allocation) => $this->getOwnerRecord()->update(['allocation_id' => $allocation->id]) && $this->deselectAllTableRecords()) ->action(fn(Allocation $allocation) => $this->getOwnerRecord()->update(['allocation_id' => $allocation->id]) && $this->deselectAllTableRecords())
->default(fn (Allocation $allocation) => $allocation->id === $this->getOwnerRecord()->allocation_id) ->default(fn(Allocation $allocation) => $allocation->id === $this->getOwnerRecord()->allocation_id)
->label('Primary'), ->label('Primary'),
]) ])
->actions([ ->actions([
Action::make('make-primary') Action::make('make-primary')
->action(fn (Allocation $allocation) => $this->getOwnerRecord()->update(['allocation_id' => $allocation->id]) && $this->deselectAllTableRecords()) ->action(fn(Allocation $allocation) => $this->getOwnerRecord()->update(['allocation_id' => $allocation->id]) && $this->deselectAllTableRecords())
->label(fn (Allocation $allocation) => $allocation->id === $this->getOwnerRecord()->allocation_id ? '' : 'Make Primary'), ->label(fn(Allocation $allocation) => $allocation->id === $this->getOwnerRecord()->allocation_id ? '' : 'Make Primary'),
]) ])
->headerActions([ ->headerActions([
CreateAction::make()->label('Create Allocation') CreateAction::make()->label('Create Allocation')
->createAnother(false) ->createAnother(false)
->form(fn () => [ ->form(fn() => [
TextInput::make('allocation_ip') TextInput::make('allocation_ip')
->datalist($this->getOwnerRecord()->node->ipAddresses()) ->datalist($this->getOwnerRecord()->node->ipAddresses())
->label('IP Address') ->label('IP Address')
@ -99,7 +100,7 @@ class AllocationsRelationManager extends RelationManager
foreach ($state as $portEntry) { foreach ($state as $portEntry) {
if (!str_contains($portEntry, '-')) { if (!str_contains($portEntry, '-')) {
if (is_numeric($portEntry)) { if (is_numeric($portEntry)) {
$ports->push((int) $portEntry); $ports->push((int)$portEntry);
continue; continue;
} }
@ -116,8 +117,8 @@ class AllocationsRelationManager extends RelationManager
continue; continue;
} }
$start = max((int) $start, 0); $start = max((int)$start, 0);
$end = min((int) $end, 2 ** 16 - 1); $end = min((int)$end, 2 ** 16 - 1);
foreach (range($start, $end) as $i) { foreach (range($start, $end) as $i) {
$ports->push($i); $ports->push($i);
} }
@ -135,7 +136,7 @@ class AllocationsRelationManager extends RelationManager
$ports = $sortedPorts; $ports = $sortedPorts;
} }
$ports = $ports->filter(fn ($port) => $port > 1024 && $port < 65535)->values(); $ports = $ports->filter(fn($port) => $port > 1024 && $port < 65535)->values();
if ($update) { if ($update) {
$set('allocation_ports', $ports->all()); $set('allocation_ports', $ports->all());
@ -144,12 +145,12 @@ class AllocationsRelationManager extends RelationManager
->splitKeys(['Tab', ' ', ',']) ->splitKeys(['Tab', ' ', ','])
->required(), ->required(),
]) ])
->action(fn (array $data, AssignmentService $service) => $service->handle($this->getOwnerRecord()->node, $data, $this->getOwnerRecord())), ->action(fn(array $data, AssignmentService $service) => $service->handle($this->getOwnerRecord()->node, $data, $this->getOwnerRecord())),
AssociateAction::make() AssociateAction::make()
->multiple() ->multiple()
->associateAnother(false) ->associateAnother(false)
->preloadRecordSelect() ->preloadRecordSelect()
->recordSelectOptionsQuery(fn ($query) => $query->whereBelongsTo($this->getOwnerRecord()->node)->whereNull('server_id')) ->recordSelectOptionsQuery(fn($query) => $query->whereBelongsTo($this->getOwnerRecord()->node)->whereNull('server_id'))
->label('Add Allocation'), ->label('Add Allocation'),
]) ])
->bulkActions([ ->bulkActions([