Prevent Server primary allocation dissociation (#1197)

This commit is contained in:
MartinOscar 2025-04-04 00:56:15 +02:00 committed by GitHub
parent c0fa8c1cd8
commit 484a3b445a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 5 deletions

View File

@ -12,14 +12,16 @@ use Filament\Forms\Components\TextInput;
use Filament\Forms\Get; use Filament\Forms\Get;
use Filament\Forms\Set; use Filament\Forms\Set;
use Filament\Resources\RelationManagers\RelationManager; use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables; use Filament\Support\Exceptions\Halt;
use Filament\Tables\Actions\Action; use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\AssociateAction; use Filament\Tables\Actions\AssociateAction;
use Filament\Tables\Actions\CreateAction; use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Actions\DissociateBulkAction;
use Filament\Tables\Columns\IconColumn; use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn; use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\TextInputColumn; use Filament\Tables\Columns\TextInputColumn;
use Filament\Tables\Table; use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Collection;
/** /**
* @method Server getOwnerRecord() * @method Server getOwnerRecord()
@ -96,10 +98,21 @@ class AllocationsRelationManager extends RelationManager
->recordSelectSearchColumns(['ip', 'port']) ->recordSelectSearchColumns(['ip', 'port'])
->label(trans('admin/server.add_allocation')), ->label(trans('admin/server.add_allocation')),
]) ])
->bulkActions([ ->groupedBulkActions([
Tables\Actions\BulkActionGroup::make([ DissociateBulkAction::make()
Tables\Actions\DissociateBulkAction::make(), ->before(function (DissociateBulkAction $action, Collection $records) {
]), $records = $records->filter(function ($allocation) {
/** @var Allocation $allocation */
return $allocation->id !== $this->getOwnerRecord()->allocation_id;
});
if ($records->isEmpty()) {
$action->failureNotificationTitle(trans('admin/server.notifications.dissociate_primary'))->failure();
throw new Halt();
}
return $records;
}),
]); ]);
} }
} }

View File

@ -72,6 +72,20 @@ class Allocation extends Model
static::deleting(function (self $allocation) { static::deleting(function (self $allocation) {
throw_if($allocation->server_id, new ServerUsingAllocationException(trans('exceptions.allocations.server_using'))); throw_if($allocation->server_id, new ServerUsingAllocationException(trans('exceptions.allocations.server_using')));
}); });
static::updating(function ($allocation) {
$originalServerId = $allocation->getOriginal('server_id');
if (!$originalServerId) {
return;
}
$server = Server::find($originalServerId);
if (!$server) {
return;
}
if ($allocation->isDirty('server_id') && is_null($allocation->server_id) && $allocation->id === $server->allocation_id) {
return false;
}
});
} }
protected function casts(): array protected function casts(): array

View File

@ -120,6 +120,7 @@ return [
'too_many_ports_body' => 'The current limit is :limit number of ports at one time.', 'too_many_ports_body' => 'The current limit is :limit number of ports at one time.',
'invalid_port' => 'Port not in valid range', 'invalid_port' => 'Port not in valid range',
'invalid_port_body' => ':i is not in the valid port range between :portFloor-:portCeil', 'invalid_port_body' => ':i is not in the valid port range between :portFloor-:portCeil',
'dissociate_primary' => 'Cannot dissociate primary allocation',
'already_exists' => 'Port already in use', 'already_exists' => 'Port already in use',
'already_exists_body' => ':i is already with an allocation', 'already_exists_body' => ':i is already with an allocation',
'error_connecting' => 'Error connecting to :node', 'error_connecting' => 'Error connecting to :node',