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