Skip to content

Commit 2278927

Browse files
committed
Update allocations to support ids; protect endpoints; support notes
1 parent 9c3b9a0 commit 2278927

File tree

19 files changed

+216
-61
lines changed

19 files changed

+216
-61
lines changed

app/Http/Controllers/Api/Client/Servers/NetworkController.php renamed to app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
44

55
use Pterodactyl\Models\Server;
6+
use Illuminate\Http\JsonResponse;
7+
use Pterodactyl\Models\Allocation;
68
use Pterodactyl\Exceptions\DisplayException;
79
use Pterodactyl\Repositories\Eloquent\ServerRepository;
8-
use Illuminate\Database\Eloquent\ModelNotFoundException;
910
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
1011
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
1112
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
1213
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
14+
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
15+
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
1316
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
1417

15-
class NetworkController extends ClientApiController
18+
class NetworkAllocationController extends ClientApiController
1619
{
1720
/**
1821
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
@@ -55,36 +58,70 @@ public function index(GetNetworkRequest $request, Server $server): array
5558
->toArray();
5659
}
5760

61+
/**
62+
* Set the primary allocation for a server.
63+
*
64+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest $request
65+
* @param \Pterodactyl\Models\Server $server
66+
* @param \Pterodactyl\Models\Allocation $allocation
67+
* @return array
68+
*
69+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
70+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
71+
*/
72+
public function update(UpdateAllocationRequest $request, Server $server, Allocation $allocation): array
73+
{
74+
$allocation = $this->repository->update($allocation->id, [
75+
'notes' => $request->input('notes'),
76+
]);
77+
78+
return $this->fractal->item($allocation)
79+
->transformWith($this->getTransformer(AllocationTransformer::class))
80+
->toArray();
81+
}
82+
5883
/**
5984
* Set the primary allocation for a server.
6085
*
6186
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest $request
6287
* @param \Pterodactyl\Models\Server $server
88+
* @param \Pterodactyl\Models\Allocation $allocation
6389
* @return array
6490
*
65-
* @throws \Pterodactyl\Exceptions\DisplayException
91+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
6692
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
93+
*/
94+
public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array
95+
{
96+
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
97+
98+
return $this->fractal->item($allocation)
99+
->transformWith($this->getTransformer(AllocationTransformer::class))
100+
->toArray();
101+
}
102+
103+
/**
104+
* Delete an allocation from a server.
105+
*
106+
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest $request
107+
* @param \Pterodactyl\Models\Server $server
108+
* @param \Pterodactyl\Models\Allocation $allocation
109+
* @return \Illuminate\Http\JsonResponse
110+
*
111+
* @throws \Pterodactyl\Exceptions\DisplayException
67112
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
113+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
68114
*/
69-
public function storePrimary(SetPrimaryAllocationRequest $request, Server $server): array
115+
public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation)
70116
{
71-
try {
72-
/** @var \Pterodactyl\Models\Allocation $allocation */
73-
$allocation = $this->repository->findFirstWhere([
74-
'server_id' => $server->id,
75-
'ip' => $request->input('ip'),
76-
'port' => $request->input('port'),
77-
]);
78-
} catch (ModelNotFoundException $exception) {
117+
if ($allocation->id === $server->allocation_id) {
79118
throw new DisplayException(
80-
'The IP and port you selected are not available for this server.'
119+
'Cannot delete the primary allocation for a server.'
81120
);
82121
}
83122

84-
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
123+
$this->repository->update($allocation->id, ['server_id' => null, 'notes' => null]);
85124

86-
return $this->fractal->item($allocation)
87-
->transformWith($this->getTransformer(AllocationTransformer::class))
88-
->toArray();
125+
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
89126
}
90127
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Middleware\Api\Client\Server;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
8+
9+
class AllocationBelongsToServer
10+
{
11+
/**
12+
* Ensure that the allocation found in the URL belongs to the server being queried.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
* @return mixed
17+
*
18+
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
19+
*/
20+
public function handle(Request $request, Closure $next)
21+
{
22+
/** @var \Pterodactyl\Models\Server $server */
23+
$server = $request->route()->parameter('server');
24+
/** @var \Pterodactyl\Models\Allocation|null $allocation */
25+
$allocation = $request->route()->parameter('allocation');
26+
27+
if ($allocation && $allocation->server_id !== $server->id) {
28+
throw new NotFoundHttpException;
29+
}
30+
31+
return $next($request);
32+
}
33+
}

app/Http/Middleware/Api/Client/SubstituteClientApiBindings.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Closure;
66
use Pterodactyl\Models\Backup;
7+
use Pterodactyl\Models\Database;
78
use Illuminate\Container\Container;
89
use Pterodactyl\Contracts\Extensions\HashidsInterface;
910
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
1011
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
1112
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
12-
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
1313

1414
class SubstituteClientApiBindings extends ApiSubstituteBindings
1515
{
@@ -43,17 +43,9 @@ public function handle($request, Closure $next)
4343
});
4444

4545
$this->router->bind('database', function ($value) use ($request) {
46-
try {
47-
$id = Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
48-
49-
return Container::getInstance()->make(DatabaseRepositoryInterface::class)->findFirstWhere([
50-
['id', '=', $id],
51-
]);
52-
} catch (RecordNotFoundException $exception) {
53-
$request->attributes->set('is_missing_model', true);
46+
$id = Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
5447

55-
return null;
56-
}
48+
return Database::query()->where('id', $id)->firstOrFail();
5749
});
5850

5951
$this->router->model('backup', Backup::class, function ($value) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
4+
5+
use Pterodactyl\Models\Permission;
6+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7+
8+
class DeleteAllocationRequest extends ClientApiRequest
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function permission(): string
14+
{
15+
return Permission::ACTION_ALLOCATION_DELETE;
16+
}
17+
}

app/Http/Requests/Api/Client/Servers/Network/SetPrimaryAllocationRequest.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,13 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
44

5-
use Pterodactyl\Models\Permission;
6-
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
7-
8-
class SetPrimaryAllocationRequest extends ClientApiRequest
5+
class SetPrimaryAllocationRequest extends UpdateAllocationRequest
96
{
10-
/**
11-
* @return string
12-
*/
13-
public function permission(): string
14-
{
15-
return Permission::ACTION_ALLOCIATION_UPDATE;
16-
}
17-
187
/**
198
* @return array
209
*/
2110
public function rules(): array
2211
{
23-
return [
24-
'ip' => 'required|string',
25-
'port' => 'required|numeric|min:1024|max:65535',
26-
];
12+
return [];
2713
}
2814
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
4+
5+
use Pterodactyl\Models\Allocation;
6+
use Pterodactyl\Models\Permission;
7+
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
8+
9+
class UpdateAllocationRequest extends ClientApiRequest
10+
{
11+
/**
12+
* @return string
13+
*/
14+
public function permission(): string
15+
{
16+
return Permission::ACTION_ALLOCATION_UPDATE;
17+
}
18+
19+
/**
20+
* @return array
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'notes' => Allocation::$validationRules['notes'],
26+
];
27+
}
28+
}

app/Models/Allocation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @property string|null $ip_alias
1010
* @property int $port
1111
* @property int|null $server_id
12+
* @property string|null $notes
1213
* @property \Carbon\Carbon|null $created_at
1314
* @property \Carbon\Carbon|null $updated_at
1415
*
@@ -60,6 +61,7 @@ class Allocation extends Model
6061
'port' => 'required|numeric|between:1024,65553',
6162
'ip_alias' => 'nullable|string',
6263
'server_id' => 'nullable|exists:servers,id',
64+
'notes' => 'nullable|string|max:256',
6365
];
6466

6567
/**

app/Models/Permission.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class Permission extends Model
4444
const ACTION_BACKUP_DOWNLOAD = 'backup.download';
4545

4646
const ACTION_ALLOCATION_READ = 'allocation.read';
47-
const ACTION_ALLOCIATION_UPDATE = 'allocation.update';
47+
const ACTION_ALLOCATION_CREATE = 'allocation.create';
48+
const ACTION_ALLOCATION_UPDATE = 'allocation.update';
49+
const ACTION_ALLOCATION_DELETE = 'allocation.delete';
4850

4951
const ACTION_FILE_READ = 'file.read';
5052
const ACTION_FILE_CREATE = 'file.create';
@@ -157,7 +159,9 @@ class Permission extends Model
157159
'description' => 'Permissions that control a user\'s ability to modify the port allocations for this server.',
158160
'keys' => [
159161
'read' => 'Allows a user to view the allocations assigned to this server.',
160-
'update' => 'Allows a user to modify the allocations assigned to this server.',
162+
'create' => 'Allows a user to assign additional allocations to the server.',
163+
'update' => 'Allows a user to change the primary server allocation and attach notes to each allocation.',
164+
'delete' => 'Allows a user to delete an allocation from the server.',
161165
],
162166
],
163167

app/Transformers/Api/Application/AllocationTransformer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function transform(Allocation $allocation)
3939
'ip' => $allocation->ip,
4040
'alias' => $allocation->ip_alias,
4141
'port' => $allocation->port,
42+
'notes' => $allocation->notes,
4243
'assigned' => ! is_null($allocation->server_id),
4344
];
4445
}

app/Transformers/Api/Client/AllocationTransformer.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ public function getResourceName(): string
2525
public function transform(Allocation $model)
2626
{
2727
return [
28+
'id' => $model->id,
2829
'ip' => $model->ip,
2930
'ip_alias' => $model->ip_alias,
3031
'port' => $model->port,
32+
'notes' => $model->notes,
3133
'is_default' => $model->server->allocation_id === $model->id,
3234
];
3335
}

0 commit comments

Comments
 (0)