Skip to content

Commit 634b80e

Browse files
committed
Add support for filtering allocations to determine if they're assigned or not; closes pterodactyl#3872
1 parent 1ae9860 commit 634b80e

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

app/Http/Controllers/Api/Application/Nodes/AllocationController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use Pterodactyl\Models\Node;
66
use Illuminate\Http\JsonResponse;
77
use Pterodactyl\Models\Allocation;
8+
use Spatie\QueryBuilder\QueryBuilder;
9+
use Spatie\QueryBuilder\AllowedFilter;
10+
use Illuminate\Database\Eloquent\Builder;
811
use Pterodactyl\Services\Allocations\AssignmentService;
912
use Pterodactyl\Services\Allocations\AllocationDeletionService;
1013
use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
@@ -43,7 +46,20 @@ public function __construct(
4346
*/
4447
public function index(GetAllocationsRequest $request, Node $node): array
4548
{
46-
$allocations = $node->allocations()->paginate($request->query('per_page') ?? 50);
49+
$allocations = QueryBuilder::for($node->allocations())
50+
->allowedFilters([
51+
AllowedFilter::exact('ip'),
52+
AllowedFilter::exact('port'),
53+
'ip_alias',
54+
AllowedFilter::callback('server_id', function (Builder $builder, $value) {
55+
if (empty($value) || is_bool($value) || !ctype_digit((string) $value)) {
56+
return $builder->whereNull('server_id');
57+
}
58+
59+
return $builder->where('server_id', $value);
60+
}),
61+
])
62+
->paginate($request->query('per_page') ?? 50);
4763

4864
return $this->fractal->collection($allocations)
4965
->transformWith($this->getTransformer(AllocationTransformer::class))

0 commit comments

Comments
 (0)