Skip to content

Commit cf21fd5

Browse files
committed
More API updates, better support for node config edits
1 parent 800e2df commit cf21fd5

File tree

21 files changed

+449
-125
lines changed

21 files changed

+449
-125
lines changed

app/Contracts/Repository/AllocationRepositoryInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Contracts\Repository;
44

55
use Illuminate\Support\Collection;
6+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
67

78
interface AllocationRepositoryInterface extends RepositoryInterface
89
{
@@ -23,6 +24,15 @@ public function assignAllocationsToServer(int $server = null, array $ids): int;
2324
*/
2425
public function getAllocationsForNode(int $node): Collection;
2526

27+
/**
28+
* Return all of the allocations for a node in a paginated format.
29+
*
30+
* @param int $node
31+
* @param int $perPage
32+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
33+
*/
34+
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator;
35+
2636
/**
2737
* Return all of the unique IPs that exist for a given node.
2838
*

app/Contracts/Repository/RepositoryInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Contracts\Repository;
44

55
use Illuminate\Support\Collection;
6+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
67

78
interface RepositoryInterface
89
{
@@ -175,6 +176,14 @@ public function updateOrCreate(array $where, array $fields, bool $validate = tru
175176
*/
176177
public function all(): Collection;
177178

179+
/**
180+
* Return a paginated result set using a search term if set on the repository.
181+
*
182+
* @param int $perPage
183+
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
184+
*/
185+
public function paginated(int $perPage): LengthAwarePaginator;
186+
178187
/**
179188
* Insert a single or multiple records into the database at once skipping
180189
* validation and mass assignment checking.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pterodactyl\Exceptions\Service\Allocation;
4+
5+
use Pterodactyl\Exceptions\DisplayException;
6+
7+
class ServerUsingAllocationException extends DisplayException
8+
{
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Pterodactyl\Exceptions\Service\Node;
4+
5+
use Pterodactyl\Exceptions\DisplayException;
6+
7+
class ConfigurationNotPersistedException extends DisplayException
8+
{
9+
}

app/Http/Controllers/API/Admin/Locations/LocationController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(
7474
*/
7575
public function index(Request $request): array
7676
{
77-
$locations = $this->repository->all(50);
77+
$locations = $this->repository->paginated(100);
7878

7979
return $this->fractal->collection($locations)
8080
->transformWith(new LocationTransformer($request))
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\API\Admin\Nodes;
4+
5+
use Spatie\Fractal\Fractal;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Response;
8+
use Pterodactyl\Models\Allocation;
9+
use Pterodactyl\Http\Controllers\Controller;
10+
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
11+
use Pterodactyl\Transformers\Api\Admin\AllocationTransformer;
12+
use Pterodactyl\Services\Allocations\AllocationDeletionService;
13+
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
14+
15+
class AllocationController extends Controller
16+
{
17+
/**
18+
* @var \Pterodactyl\Services\Allocations\AllocationDeletionService
19+
*/
20+
private $deletionService;
21+
22+
/**
23+
* @var \Spatie\Fractal\Fractal
24+
*/
25+
private $fractal;
26+
27+
/**
28+
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
29+
*/
30+
private $repository;
31+
32+
/**
33+
* AllocationController constructor.
34+
*
35+
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService
36+
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
37+
* @param \Spatie\Fractal\Fractal $fractal
38+
*/
39+
public function __construct(AllocationDeletionService $deletionService, AllocationRepositoryInterface $repository, Fractal $fractal)
40+
{
41+
$this->deletionService = $deletionService;
42+
$this->fractal = $fractal;
43+
$this->repository = $repository;
44+
}
45+
46+
/**
47+
* Return all of the allocations that exist for a given node.
48+
*
49+
* @param \Illuminate\Http\Request $request
50+
* @param int $node
51+
* @return array
52+
*/
53+
public function index(Request $request, int $node): array
54+
{
55+
$allocations = $this->repository->getPaginatedAllocationsForNode($node, 100);
56+
57+
return $this->fractal->collection($allocations)
58+
->transformWith(new AllocationTransformer($request))
59+
->withResourceName('allocation')
60+
->paginateWith(new IlluminatePaginatorAdapter($allocations))
61+
->toArray();
62+
}
63+
64+
/**
65+
* Delete a specific allocation from the Panel.
66+
*
67+
* @param \Pterodactyl\Models\Allocation $allocation
68+
* @return \Illuminate\Http\Response
69+
*
70+
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
71+
*/
72+
public function delete(Request $request, int $node, Allocation $allocation): Response
73+
{
74+
$this->deletionService->handle($allocation);
75+
76+
return response('', 204);
77+
}
78+
}

app/Http/Controllers/API/Admin/Nodes/NodeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function __construct(
7474
*/
7575
public function index(Request $request): array
7676
{
77-
$nodes = $this->repository->all(config('pterodactyl.paginate.api.nodes'));
77+
$nodes = $this->repository->paginated(100);
7878

7979
$fractal = $this->fractal->collection($nodes)
8080
->transformWith(new NodeTransformer($request))

app/Http/Controllers/API/Admin/Users/UserController.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function __construct(
7676
*/
7777
public function index(Request $request): array
7878
{
79-
$users = $this->repository->all(config('pterodactyl.paginate.api.users'));
79+
$users = $this->repository->paginated(100);
8080

8181
return $this->fractal->collection($users)
8282
->transformWith(new UserTransformer($request))
@@ -113,7 +113,6 @@ public function view(Request $request, User $user): array
113113
* @param \Pterodactyl\Models\User $user
114114
* @return array
115115
*
116-
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
117116
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
118117
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
119118
*/

app/Http/Controllers/Admin/NodesController.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Javascript;
1313
use Illuminate\Http\Request;
1414
use Pterodactyl\Models\Node;
15+
use Illuminate\Http\Response;
16+
use Pterodactyl\Models\Allocation;
1517
use Prologue\Alerts\AlertsMessageBag;
1618
use Pterodactyl\Http\Controllers\Controller;
1719
use Pterodactyl\Services\Nodes\NodeUpdateService;
@@ -23,6 +25,7 @@
2325
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
2426
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
2527
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
28+
use Pterodactyl\Services\Allocations\AllocationDeletionService;
2629
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
2730
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
2831
use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;
@@ -78,11 +81,16 @@ class NodesController extends Controller
7881
* @var \Pterodactyl\Services\Helpers\SoftwareVersionService
7982
*/
8083
protected $versionService;
84+
/**
85+
* @var \Pterodactyl\Services\Allocations\AllocationDeletionService
86+
*/
87+
private $allocationDeletionService;
8188

8289
/**
8390
* NodesController constructor.
8491
*
8592
* @param \Prologue\Alerts\AlertsMessageBag $alert
93+
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $allocationDeletionService
8694
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
8795
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
8896
* @param \Illuminate\Cache\Repository $cache
@@ -95,6 +103,7 @@ class NodesController extends Controller
95103
*/
96104
public function __construct(
97105
AlertsMessageBag $alert,
106+
AllocationDeletionService $allocationDeletionService,
98107
AllocationRepositoryInterface $allocationRepository,
99108
AssignmentService $assignmentService,
100109
CacheRepository $cache,
@@ -106,6 +115,7 @@ public function __construct(
106115
SoftwareVersionService $versionService
107116
) {
108117
$this->alert = $alert;
118+
$this->allocationDeletionService = $allocationDeletionService;
109119
$this->allocationRepository = $allocationRepository;
110120
$this->assignmentService = $assignmentService;
111121
$this->cache = $cache;
@@ -262,17 +272,14 @@ public function updateSettings(NodeFormRequest $request, Node $node)
262272
/**
263273
* Removes a single allocation from a node.
264274
*
265-
* @param int $node
266-
* @param int $allocation
267-
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
275+
* @param \Pterodactyl\Models\Allocation $allocation
276+
* @return \Illuminate\Http\Response
277+
*
278+
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
268279
*/
269-
public function allocationRemoveSingle($node, $allocation)
280+
public function allocationRemoveSingle(Allocation $allocation): Response
270281
{
271-
$this->allocationRepository->deleteWhere([
272-
['id', '=', $allocation],
273-
['node_id', '=', $node],
274-
['server_id', '=', null],
275-
]);
282+
$this->allocationDeletionService->handle($allocation);
276283

277284
return response('', 204);
278285
}

app/Models/Allocation.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ public function server()
105105
{
106106
return $this->belongsTo(Server::class);
107107
}
108+
109+
/**
110+
* Return the Node model associated with this allocation.
111+
*
112+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
113+
*/
114+
public function node()
115+
{
116+
return $this->belongsTo(Node::class);
117+
}
108118
}

0 commit comments

Comments
 (0)