diff --git a/app/Filament/Admin/Pages/Settings.php b/app/Filament/Admin/Pages/Settings.php index 3887feea7..f7a8c9b63 100644 --- a/app/Filament/Admin/Pages/Settings.php +++ b/app/Filament/Admin/Pages/Settings.php @@ -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() diff --git a/app/Services/Allocations/FindAssignableAllocationService.php b/app/Services/Allocations/FindAssignableAllocationService.php index d242564be..69aa8470d 100644 --- a/app/Services/Allocations/FindAssignableAllocationService.php +++ b/app/Services/Allocations/FindAssignableAllocationService.php @@ -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]); diff --git a/config/panel.php b/config/panel.php index 70a0c2952..c03b626b1 100644 --- a/config/panel.php +++ b/config/panel.php @@ -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'), ], diff --git a/lang/en/admin/setting.php b/lang/en/admin/setting.php index 963143c10..356842bf1 100644 --- a/lang/en/admin/setting.php +++ b/lang/en/admin/setting.php @@ -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', ],