Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Application/Nodes/AllocationController.php
10277 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Application\Nodes;
4
5
use Pterodactyl\Models\Node;
6
use Illuminate\Http\JsonResponse;
7
use Pterodactyl\Models\Allocation;
8
use Spatie\QueryBuilder\QueryBuilder;
9
use Spatie\QueryBuilder\AllowedFilter;
10
use Illuminate\Database\Eloquent\Builder;
11
use Pterodactyl\Services\Allocations\AssignmentService;
12
use Pterodactyl\Services\Allocations\AllocationDeletionService;
13
use Pterodactyl\Transformers\Api\Application\AllocationTransformer;
14
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
15
use Pterodactyl\Http\Requests\Api\Application\Allocations\GetAllocationsRequest;
16
use Pterodactyl\Http\Requests\Api\Application\Allocations\StoreAllocationRequest;
17
use Pterodactyl\Http\Requests\Api\Application\Allocations\DeleteAllocationRequest;
18
19
class AllocationController extends ApplicationApiController
20
{
21
/**
22
* AllocationController constructor.
23
*/
24
public function __construct(
25
private AssignmentService $assignmentService,
26
private AllocationDeletionService $deletionService,
27
) {
28
parent::__construct();
29
}
30
31
/**
32
* Return all the allocations that exist for a given node.
33
*/
34
public function index(GetAllocationsRequest $request, Node $node): array
35
{
36
$allocations = QueryBuilder::for($node->allocations())
37
->allowedFilters([
38
AllowedFilter::exact('ip'),
39
AllowedFilter::exact('port'),
40
'ip_alias',
41
AllowedFilter::callback('server_id', function (Builder $builder, $value) {
42
if (empty($value) || is_bool($value) || !ctype_digit((string) $value)) {
43
return $builder->whereNull('server_id');
44
}
45
46
return $builder->where('server_id', $value);
47
}),
48
])
49
->paginate($request->query('per_page') ?? 50);
50
51
return $this->fractal->collection($allocations)
52
->transformWith($this->getTransformer(AllocationTransformer::class))
53
->toArray();
54
}
55
56
/**
57
* Store new allocations for a given node.
58
*
59
* @throws \Pterodactyl\Exceptions\DisplayException
60
* @throws \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
61
* @throws \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
62
* @throws \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
63
* @throws \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
64
*/
65
public function store(StoreAllocationRequest $request, Node $node): JsonResponse
66
{
67
$this->assignmentService->handle($node, $request->validated());
68
69
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
70
}
71
72
/**
73
* Delete a specific allocation from the Panel.
74
*
75
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
76
*/
77
public function delete(DeleteAllocationRequest $request, Node $node, Allocation $allocation): JsonResponse
78
{
79
$this->deletionService->handle($allocation);
80
81
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
82
}
83
}
84
85