From 634b80ed42cfcb500c55ec024aa386b5f949675f Mon Sep 17 00:00:00 2001 From: DaneEveritt Date: Sat, 7 May 2022 16:16:11 -0400 Subject: [PATCH] Add support for filtering allocations to determine if they're assigned or not; closes #3872 --- .../Application/Nodes/AllocationController.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php index 5a66e8175..ecc7e1cd4 100644 --- a/app/Http/Controllers/Api/Application/Nodes/AllocationController.php +++ b/app/Http/Controllers/Api/Application/Nodes/AllocationController.php @@ -5,6 +5,9 @@ namespace Pterodactyl\Http\Controllers\Api\Application\Nodes; use Pterodactyl\Models\Node; use Illuminate\Http\JsonResponse; use Pterodactyl\Models\Allocation; +use Spatie\QueryBuilder\QueryBuilder; +use Spatie\QueryBuilder\AllowedFilter; +use Illuminate\Database\Eloquent\Builder; use Pterodactyl\Services\Allocations\AssignmentService; use Pterodactyl\Services\Allocations\AllocationDeletionService; use Pterodactyl\Transformers\Api\Application\AllocationTransformer; @@ -43,7 +46,20 @@ class AllocationController extends ApplicationApiController */ public function index(GetAllocationsRequest $request, Node $node): array { - $allocations = $node->allocations()->paginate($request->query('per_page') ?? 50); + $allocations = QueryBuilder::for($node->allocations()) + ->allowedFilters([ + AllowedFilter::exact('ip'), + AllowedFilter::exact('port'), + 'ip_alias', + AllowedFilter::callback('server_id', function (Builder $builder, $value) { + if (empty($value) || is_bool($value) || !ctype_digit((string) $value)) { + return $builder->whereNull('server_id'); + } + + return $builder->where('server_id', $value); + }), + ]) + ->paginate($request->query('per_page') ?? 50); return $this->fractal->collection($allocations) ->transformWith($this->getTransformer(AllocationTransformer::class))