Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php
10280 views
1
<?php
2
3
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
4
5
use Pterodactyl\Models\Server;
6
use Illuminate\Http\JsonResponse;
7
use Pterodactyl\Facades\Activity;
8
use Pterodactyl\Models\Allocation;
9
use Illuminate\Database\ConnectionInterface;
10
use Pterodactyl\Exceptions\DisplayException;
11
use Pterodactyl\Repositories\Eloquent\ServerRepository;
12
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
13
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
14
use Pterodactyl\Services\Allocations\FindAssignableAllocationService;
15
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
16
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;
17
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
18
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
19
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
20
21
class NetworkAllocationController extends ClientApiController
22
{
23
/**
24
* NetworkAllocationController constructor.
25
*/
26
public function __construct(
27
protected readonly ConnectionInterface $connection,
28
private FindAssignableAllocationService $assignableAllocationService,
29
private ServerRepository $serverRepository,
30
) {
31
parent::__construct();
32
}
33
34
/**
35
* Lists all the allocations available to a server and whether
36
* they are currently assigned as the primary for this server.
37
*/
38
public function index(GetNetworkRequest $request, Server $server): array
39
{
40
return $this->fractal->collection($server->allocations)
41
->transformWith($this->getTransformer(AllocationTransformer::class))
42
->toArray();
43
}
44
45
/**
46
* Set the primary allocation for a server.
47
*
48
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
49
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
50
*/
51
public function update(UpdateAllocationRequest $request, Server $server, Allocation $allocation): array
52
{
53
$original = $allocation->notes;
54
55
$allocation->forceFill(['notes' => $request->input('notes')])->save();
56
57
if ($original !== $allocation->notes) {
58
Activity::event('server:allocation.notes')
59
->subject($allocation)
60
->property(['allocation' => $allocation->toString(), 'old' => $original, 'new' => $allocation->notes])
61
->log();
62
}
63
64
return $this->fractal->item($allocation)
65
->transformWith($this->getTransformer(AllocationTransformer::class))
66
->toArray();
67
}
68
69
/**
70
* Set the primary allocation for a server.
71
*
72
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
73
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
74
*/
75
public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array
76
{
77
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
78
79
Activity::event('server:allocation.primary')
80
->subject($allocation)
81
->property('allocation', $allocation->toString())
82
->log();
83
84
return $this->fractal->item($allocation)
85
->transformWith($this->getTransformer(AllocationTransformer::class))
86
->toArray();
87
}
88
89
/**
90
* Set the notes for the allocation for a server.
91
*s.
92
*
93
* @throws DisplayException
94
*/
95
public function store(NewAllocationRequest $request, Server $server): array
96
{
97
$allocation = Activity::event('server:allocation.create')->transaction(function ($log) use ($server) {
98
if ($server->allocations()->lockForUpdate()->count() >= $server->allocation_limit) {
99
throw new DisplayException('Cannot assign additional allocations to this server: limit has been reached.');
100
}
101
102
$allocation = $this->assignableAllocationService->handle($server);
103
104
$log->subject($allocation)->property('allocation', $allocation->toString());
105
106
return $allocation;
107
});
108
109
return $this->fractal->item($allocation)
110
->transformWith($this->getTransformer(AllocationTransformer::class))
111
->toArray();
112
}
113
114
/**
115
* Delete an allocation from a server.
116
*
117
* @throws DisplayException
118
*/
119
public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation): JsonResponse
120
{
121
// Don't allow the deletion of allocations if the server does not have an
122
// allocation limit set.
123
if (empty($server->allocation_limit)) {
124
throw new DisplayException('You cannot delete allocations for this server: no allocation limit is set.');
125
}
126
127
if ($allocation->id === $server->allocation_id) {
128
throw new DisplayException('You cannot delete the primary allocation for this server.');
129
}
130
131
Allocation::query()->where('id', $allocation->id)->update([
132
'notes' => null,
133
'server_id' => null,
134
]);
135
136
Activity::event('server:allocation.delete')
137
->subject($allocation)
138
->property('allocation', $allocation->toString())
139
->log();
140
141
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
142
}
143
}
144
145