feat: Add toggle for automatic allocation creation in panel settings (#1884)

This commit is contained in:
PalmarHealer 2025-12-01 08:59:07 +01:00 committed by GitHub
parent 2dd6e3d4fc
commit 6c02f9a663
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 5 deletions

View File

@ -623,6 +623,18 @@ class Settings extends Page implements HasSchemas
->columnSpanFull()
->stateCast(new BooleanStateCast(false))
->default(env('PANEL_CLIENT_ALLOCATIONS_ENABLED', config('panel.client_features.allocations.enabled'))),
Toggle::make('PANEL_CLIENT_ALLOCATIONS_CREATE_NEW')
->label(trans('admin/setting.misc.auto_allocation.create_new'))
->helperText(trans('admin/setting.misc.auto_allocation.create_new_help'))
->onIcon('tabler-check')
->offIcon('tabler-x')
->onColor('success')
->offColor('danger')
->live()
->columnSpanFull()
->visible(fn (Get $get) => $get('PANEL_CLIENT_ALLOCATIONS_ENABLED'))
->stateCast(new BooleanStateCast(false))
->default(env('PANEL_CLIENT_ALLOCATIONS_CREATE_NEW', config('panel.client_features.allocations.create_new'))),
TextInput::make('PANEL_CLIENT_ALLOCATIONS_RANGE_START')
->label(trans('admin/setting.misc.auto_allocation.start'))
->required()

View File

@ -21,9 +21,11 @@ class FindAssignableAllocationService
public function __construct(private AssignmentService $service) {}
/**
* Finds an existing unassigned allocation and attempts to assign it to the given server. If
* no allocation can be found, a new one will be created with a random port between the defined
* range from the configuration.
* Finds an existing unassigned allocation and attempts to assign it to the given server.
*
* Always attempts to find an existing unassigned allocation first. If create_new is enabled
* and no unassigned allocation is available, creates a new one from the configured port range.
* If create_new is disabled, throws an exception when no unassigned allocations are available.
*
* @throws DisplayException
* @throws CidrOutOfRangeException
@ -37,9 +39,11 @@ class FindAssignableAllocationService
throw new AutoAllocationNotEnabledException();
}
$createNew = config('panel.client_features.allocations.create_new', true);
// Attempt to find a given available allocation for a server. If one cannot be found
// we will fall back to attempting to create a new allocation that can be used for the
// server.
// and create_new is enabled, we will fall back to attempting to create a new allocation
// that can be used for the server.
$start = config('panel.client_features.allocations.range_start', null);
$end = config('panel.client_features.allocations.range_end', null);
@ -61,6 +65,12 @@ class FindAssignableAllocationService
->inRandomOrder()
->first();
// If create_new is disabled, only pick from existing allocations
if (!$createNew && !$allocation) {
throw new NoAutoAllocationSpaceAvailableException();
}
// If create_new is enabled, create a new allocation if none available
$allocation ??= $this->createNewAllocation($server, $start, $end);
$allocation->update(['server_id' => $server->id]);

View File

@ -33,6 +33,7 @@ return [
'allocations' => [
'enabled' => env('PANEL_CLIENT_ALLOCATIONS_ENABLED', false),
'create_new' => env('PANEL_CLIENT_ALLOCATIONS_CREATE_NEW', true),
'range_start' => env('PANEL_CLIENT_ALLOCATIONS_RANGE_START'),
'range_end' => env('PANEL_CLIENT_ALLOCATIONS_RANGE_END'),
],

View File

@ -108,6 +108,8 @@ return [
'title' => 'Automatic Allocation Creation',
'helper' => 'Toggle if Users can create allocations via the client area.',
'question' => 'Allow Users to create Allocations?',
'create_new' => 'Create new allocations if none available?',
'create_new_help' => 'When enabled, creates new allocations. When disabled, only assigns from existing unassigned allocations. Both options faktor the port range below into account.',
'start' => 'Start Port',
'end' => 'End Port',
],