misc_pterodactyl-panel/app/Http/Controllers/Api/Application/Nodes/AllocationController.php

89 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2018-01-20 01:58:57 +00:00
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
2018-01-13 20:08:19 +00:00
use Pterodactyl\Models\Node;
2022-12-15 00:05:46 +00:00
use Illuminate\Http\Response;
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;
2022-12-15 00:05:46 +00:00
use Pterodactyl\Exceptions\Http\QueryValueOutOfRangeHttpException;
use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
2018-01-20 01:58:57 +00:00
use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
use Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest;
use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest;
class AllocationController extends ApplicationApiController
{
/**
* AllocationController constructor.
*/
public function __construct(
private AssignmentService $assignmentService,
private AllocationDeletionService $deletionService
) {
parent::__construct();
}
/**
* Return all the allocations that exist for a given node.
*/
2020-07-07 03:05:42 +00:00
public function index(GetAllocationsRequest $request, Node $node): array
{
2022-12-15 00:05:46 +00:00
$perPage = (int) $request->query('per_page', '10');
if ($perPage < 1 || $perPage > 100) {
throw new QueryValueOutOfRangeHttpException('per_page', 1, 100);
}
$allocations = QueryBuilder::for(Allocation::query()->where('node_id', '=', $node->id))
->allowedFilters([
2022-12-15 00:05:46 +00:00
'id', 'ip', 'port', 'alias',
AllowedFilter::callback('server_id', function (Builder $query, $value) {
if ($value === '0') {
$query->whereNull('server_id');
} else {
$query->where('server_id', '=', $value);
}
}),
])
2022-12-15 00:05:46 +00:00
->allowedSorts(['id', 'ip', 'port', 'server_id'])
->paginate($perPage);
return $this->fractal->collection($allocations)
2022-12-15 00:05:46 +00:00
->transformWith(AllocationTransformer::class)
->toArray();
}
/**
* 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
*/
2022-12-15 00:05:46 +00:00
public function store(StoreAllocationRequest $request, Node $node): Response
{
2020-07-07 03:05:42 +00:00
$this->assignmentService->handle($node, $request->validated());
2022-12-15 00:05:46 +00:00
return $this->returnNoContent();
}
/**
* Delete a specific allocation from the Panel.
*
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
*/
2022-12-15 00:05:46 +00:00
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): Response
{
2020-07-07 03:05:42 +00:00
$this->deletionService->handle($allocation);
2022-12-15 00:05:46 +00:00
return $this->returnNoContent();
}
}