Skip to content

Commit e88d24e

Browse files
committed
Don't allow allocations to be deleted by users if no limit is defined; closes pterodactyl#3703
1 parent c751ce7 commit e88d24e

File tree

7 files changed

+68
-19
lines changed

7 files changed

+68
-19
lines changed

app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public function store(NewAllocationRequest $request, Server $server): array
120120
*/
121121
public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation)
122122
{
123+
// Don't allow the deletion of allocations if the server does not have an
124+
// allocation limit set.
125+
if (empty($server->allocation_limit)) {
126+
throw new DisplayException('You cannot delete allocations for this server: no allocation limit is set.');
127+
}
128+
123129
if ($allocation->id === $server->allocation_id) {
124130
throw new DisplayException('You cannot delete the primary allocation for this server.');
125131
}

app/Models/Allocation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Pterodactyl\Models;
44

55
/**
6+
* Pterodactyl\Models\Allocation.
7+
*
68
* @property int $id
79
* @property int $node_id
810
* @property string $ip
@@ -16,6 +18,22 @@
1618
* @property bool $has_alias
1719
* @property \Pterodactyl\Models\Server|null $server
1820
* @property \Pterodactyl\Models\Node $node
21+
* @property string $hashid
22+
*
23+
* @method static \Database\Factories\AllocationFactory factory(...$parameters)
24+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation newModelQuery()
25+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation newQuery()
26+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation query()
27+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereCreatedAt($value)
28+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereId($value)
29+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereIp($value)
30+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereIpAlias($value)
31+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereNodeId($value)
32+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereNotes($value)
33+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation wherePort($value)
34+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereServerId($value)
35+
* @method static \Illuminate\Database\Eloquent\Builder|Allocation whereUpdatedAt($value)
36+
* @mixin \Eloquent
1937
*/
2038
class Allocation extends Model
2139
{

database/Factories/AllocationFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Database\Factories;
44

5+
use Pterodactyl\Models\Server;
56
use Pterodactyl\Models\Allocation;
67
use Illuminate\Database\Eloquent\Factories\Factory;
78

@@ -24,4 +25,12 @@ public function definition(): array
2425
'port' => $this->faker->unique()->randomNumber(5),
2526
];
2627
}
28+
29+
/**
30+
* Attaches the allocation to a specific server model.
31+
*/
32+
public function forServer(Server $server): self
33+
{
34+
return $this->for($server)->for($server->node);
35+
}
2736
}

resources/scripts/components/server/network/NetworkContainer.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,22 @@ const NetworkContainer = () => {
6666
/>
6767
))
6868
}
69-
<Can action={'allocation.create'}>
70-
<SpinnerOverlay visible={loading}/>
71-
<div css={tw`mt-6 sm:flex items-center justify-end`}>
72-
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
73-
You are currently using {data.length} of {allocationLimit} allowed allocations for this
74-
server.
75-
</p>
76-
{allocationLimit > data.length &&
77-
<Button css={tw`w-full sm:w-auto`} color={'primary'} onClick={onCreateAllocation}>
78-
Create Allocation
79-
</Button>
80-
}
81-
</div>
82-
</Can>
69+
{allocationLimit > 0 &&
70+
<Can action={'allocation.create'}>
71+
<SpinnerOverlay visible={loading}/>
72+
<div css={tw`mt-6 sm:flex items-center justify-end`}>
73+
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
74+
You are currently using {data.length} of {allocationLimit} allowed allocations for
75+
this server.
76+
</p>
77+
{allocationLimit > data.length &&
78+
<Button css={tw`w-full sm:w-auto`} color={'primary'} onClick={onCreateAllocation}>
79+
Create Allocation
80+
</Button>
81+
}
82+
</div>
83+
</Can>
84+
}
8385
</>
8486
}
8587
</ServerContentBlock>

tests/Integration/Api/Client/ClientApiIntegrationTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ protected function link($model, $append = null): string
8989
* is assumed that the user is actually a subuser of the server.
9090
*
9191
* @param string[] $permissions
92+
* @return array{\Pterodactyl\Models\User, \Pterodactyl\Models\Server}
9293
*/
9394
protected function generateTestAccount(array $permissions = []): array
9495
{

tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function testAllocationCanBeDeletedFromServer(array $permission)
1919
{
2020
/** @var \Pterodactyl\Models\Server $server */
2121
[$user, $server] = $this->generateTestAccount($permission);
22+
$server->update(['allocation_limit' => 2]);
2223

2324
/** @var \Pterodactyl\Models\Allocation $allocation */
2425
$allocation = Allocation::factory()->create([
@@ -60,13 +61,30 @@ public function testErrorIsReturnedIfAllocationIsPrimary()
6061
{
6162
/** @var \Pterodactyl\Models\Server $server */
6263
[$user, $server] = $this->generateTestAccount();
64+
$server->update(['allocation_limit' => 2]);
6365

6466
$this->actingAs($user)->deleteJson($this->link($server->allocation))
6567
->assertStatus(Response::HTTP_BAD_REQUEST)
6668
->assertJsonPath('errors.0.code', 'DisplayException')
6769
->assertJsonPath('errors.0.detail', 'You cannot delete the primary allocation for this server.');
6870
}
6971

72+
public function testAllocationCannotBeDeletedIfServerLimitIsNotDefined()
73+
{
74+
[$user, $server] = $this->generateTestAccount();
75+
76+
/** @var \Pterodactyl\Models\Allocation $allocation */
77+
$allocation = Allocation::factory()->forServer($server)->create(['notes' => 'Test notes']);
78+
79+
$this->actingAs($user)->deleteJson($this->link($allocation))
80+
->assertStatus(400)
81+
->assertJsonPath('errors.0.detail', 'You cannot delete allocations for this server: no allocation limit is set.');
82+
83+
$allocation->refresh();
84+
$this->assertNotNull($allocation->notes);
85+
$this->assertEquals($server->id, $allocation->server_id);
86+
}
87+
7088
/**
7189
* Test that an allocation cannot be deleted if it does not belong to the server instance.
7290
*/

tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,4 @@ public function updatePermissionsDataProvider()
137137
{
138138
return [[[]], [[Permission::ACTION_ALLOCATION_UPDATE]]];
139139
}
140-
141-
public function deletePermissionsDataProvider()
142-
{
143-
return [[[]], [[Permission::ACTION_ALLOCATION_DELETE]]];
144-
}
145140
}

0 commit comments

Comments
 (0)