Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Nodes/AllocationController.php
10277 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;34use Pterodactyl\Models\Node;5use Illuminate\Http\JsonResponse;6use Pterodactyl\Models\Allocation;7use Spatie\QueryBuilder\QueryBuilder;8use Spatie\QueryBuilder\AllowedFilter;9use Illuminate\Database\Eloquent\Builder;10use Pterodactyl\Services\Allocations\AssignmentService;11use Pterodactyl\Services\Allocations\AllocationDeletionService;12use Pterodactyl\Transformers\Api\Application\AllocationTransformer;13use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;14use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;15use Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest;16use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest;1718class AllocationController extends ApplicationApiController19{20/**21* AllocationController constructor.22*/23public function __construct(24private AssignmentService $assignmentService,25private AllocationDeletionService $deletionService,26) {27parent::__construct();28}2930/**31* Return all the allocations that exist for a given node.32*/33public function index(GetAllocationsRequest $request, Node $node): array34{35$allocations = QueryBuilder::for($node->allocations())36->allowedFilters([37AllowedFilter::exact('ip'),38AllowedFilter::exact('port'),39'ip_alias',40AllowedFilter::callback('server_id', function (Builder $builder, $value) {41if (empty($value) || is_bool($value) || !ctype_digit((string) $value)) {42return $builder->whereNull('server_id');43}4445return $builder->where('server_id', $value);46}),47])48->paginate($request->query('per_page') ?? 50);4950return $this->fractal->collection($allocations)51->transformWith($this->getTransformer(AllocationTransformer::class))52->toArray();53}5455/**56* Store new allocations for a given node.57*58* @throws \Pterodactyl\Exceptions\DisplayException59* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException60* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException61* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException62* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException63*/64public function store(StoreAllocationRequest $request, Node $node): JsonResponse65{66$this->assignmentService->handle($node, $request->validated());6768return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);69}7071/**72* Delete a specific allocation from the Panel.73*74* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException75*/76public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse77{78$this->deletionService->handle($allocation);7980return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);81}82}838485