Files
pterodactyl-panel/app/Http/Controllers/Api/Application/Nodes/AllocationController.php
T

84 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2018-01-19 19:58:57 -06:00
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
2018-01-13 14:08:19 -06:00
use Pterodactyl\Models\Node;
2020-07-06 20:05:42 -07:00
use Illuminate\Http\JsonResponse;
use Pterodactyl\Models\Allocation;
use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter;
use Illuminate\Database\Eloquent\Builder;
2018-01-27 12:38:56 -06:00
use Pterodactyl\Services\Allocations\AssignmentService;
use Pterodactyl\Services\Allocations\AllocationDeletionService;
use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
2018-01-27 12:38:56 -06:00
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
2018-01-19 19:58:57 -06:00
use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
2018-01-27 12:38:56 -06:00
use Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest;
2018-01-27 12:38:56 -06:00
class AllocationController extends ApplicationApiController
{
/**
* AllocationController constructor.
*/
2018-01-27 12:38:56 -06:00
public function __construct(
2022-10-14 10:59:20 -06:00
private AssignmentService $assignmentService,
2024-10-21 19:18:20 -06:00
private AllocationDeletionService $deletionService,
2018-01-27 12:38:56 -06:00
) {
parent::__construct();
}
/**
2022-10-14 10:59:20 -06:00
* Return all the allocations that exist for a given node.
*/
2020-07-06 20:05:42 -07:00
public function index(GetAllocationsRequest $request, Node $node): array
{
$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)
2018-01-27 12:38:56 -06:00
->transformWith($this->getTransformer(AllocationTransformer::class))
->toArray();
}
2018-01-27 12:38:56 -06:00
/**
* Store new allocations for a given node.
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
2018-01-27 12:38:56 -06:00
*/
2020-07-06 20:05:42 -07:00
public function store(StoreAllocationRequest $request, Node $node): JsonResponse
2018-01-27 12:38:56 -06:00
{
2020-07-06 20:05:42 -07:00
$this->assignmentService->handle($node, $request->validated());
2018-01-27 12:38:56 -06:00
2020-07-06 20:05:42 -07:00
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
2018-01-27 12:38:56 -06:00
}
/**
* Delete a specific allocation from the Panel.
*
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
*/
2020-07-06 20:05:42 -07:00
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse
{
2020-07-06 20:05:42 -07:00
$this->deletionService->handle($allocation);
2020-07-06 20:05:42 -07:00
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
}