allocation activity

This commit is contained in:
robert dennis
2026-10-12 05:18:56 -04:00
parent 84695d2f0a
commit bdfdab5d3d
9 changed files with 30 additions and 23 deletions
@@ -11,6 +11,7 @@ use Pterodactyl\Data\AllocationRange;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\Service\Allocation\AutoAllocationNotEnabledException;
use Pterodactyl\Exceptions\Service\Allocation\NoAutoAllocationSpaceAvailableException;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\JsonValueGuard;
@@ -44,6 +45,8 @@ final readonly class AssignAvailableAllocation implements AssignsAvailableAlloca
$allocation->update(['server_id' => $server->id]);
Activity::event('server:allocation.create')->subject($server, $allocation)->property('allocation', $allocation->toString())->log();
return $allocation;
}
}
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Allocations;
use Pterodactyl\Contracts\Allocations\SetsPrimaryAllocations;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Server;
@@ -14,6 +15,11 @@ final class SetPrimaryAllocation implements SetsPrimaryAllocations
{
$server->update(['allocation_id' => $allocation->id]);
Activity::event('server:allocation.primary')
->subject($server, $allocation)
->property('allocation', $allocation->toString())
->log();
// The allocation row is unchanged, but a loaded server relation still holds the old primary.
return $allocation->unsetRelation('server');
}
@@ -6,6 +6,7 @@ namespace Pterodactyl\Actions\Allocations;
use Pterodactyl\Contracts\Allocations\UnassignsAllocations;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Server;
@@ -24,6 +25,11 @@ final class UnassignAllocation implements UnassignsAllocations
'server_id' => null,
]);
Activity::event('server:allocation.delete')
->subject($server, $allocation)
->property('allocation', $allocation->toString())
->log();
return $allocation->refresh();
}
}
@@ -5,14 +5,23 @@ declare(strict_types=1);
namespace Pterodactyl\Actions\Allocations;
use Pterodactyl\Contracts\Allocations\UpdatesAllocationNotes;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Allocation;
final class UpdateAllocationNotes implements UpdatesAllocationNotes
{
public function update(Allocation $allocation, ?string $notes): Allocation
{
$original = $allocation->notes;
$allocation->forceFill(['notes' => $notes])->save();
if ($original !== $notes) {
Activity::event('server:allocation.notes')
->subject($allocation->server, $allocation)
->property(['allocation' => $allocation->toString(), 'old' => $original, 'new' => $notes])
->log();
}
return $allocation;
}
}
@@ -15,7 +15,6 @@ use Pterodactyl\Contracts\Allocations\UnassignsAllocations;
use Pterodactyl\Contracts\Allocations\UpdatesAllocationNotes;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
@@ -74,17 +73,8 @@ class NetworkAllocationController extends ClientApiController
#[ResponseFromTransformer(AllocationTransformer::class, Allocation::class, description: 'Allocation notes updated.', factoryStates: ['withServer'], resourceKey: 'allocation')]
public function update(UpdateAllocationRequest $request, UpdatesAllocationNotes $notes, Server $server, Allocation $allocation): array
{
$original = $allocation->notes;
$allocation = $notes->update($allocation, JsonValueGuard::nullableString($request->validated('notes')));
if ($original !== $allocation->notes) {
Activity::event('server:allocation.notes')
->subject($allocation)
->property(['allocation' => $allocation->toString(), 'old' => $original, 'new' => $allocation->notes])
->log();
}
return Fractal::item($allocation)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toResponseArray();
@@ -101,11 +91,6 @@ class NetworkAllocationController extends ClientApiController
{
$allocation = $allocations->setPrimary($server, $allocation);
Activity::event('server:allocation.primary')
->subject($allocation)
->property('allocation', $allocation->toString())
->log();
return Fractal::item($allocation)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toResponseArray();
@@ -123,8 +108,6 @@ class NetworkAllocationController extends ClientApiController
{
$allocation = $allocations->assign($server);
Activity::event('server:allocation.create')->subject($allocation)->property('allocation', $allocation->toString())->log();
return Fractal::item($allocation)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toResponseArray();
@@ -140,12 +123,7 @@ class NetworkAllocationController extends ClientApiController
#[ScribeResponse(self::DELETE_PRIMARY_ERROR, status: 400, description: 'The allocation cannot be removed because it is primary or the server has no allocation limit.')]
public function delete(DeleteAllocationRequest $request, UnassignsAllocations $allocations, Server $server, Allocation $allocation): JsonResponse
{
$allocation = $allocations->unassign($server, $allocation);
Activity::event('server:allocation.delete')
->subject($allocation)
->property('allocation', $allocation->toString())
->log();
$allocations->unassign($server, $allocation);
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
@@ -33,6 +33,7 @@ test('existing allocation is preferred', function () {
expect($response->node_id)->toBe($server->node_id);
expect($response->server_id)->toBe($server->id);
$this->assertNotSame($server->allocation_id, $response->id);
$this->assertActivityOn('server:allocation.create', $server, $response);
});
test('new allocation is created if one is not found', function () {
$server = $this->createServerModel();
@@ -37,6 +37,7 @@ test('new allocation can be assigned to server', function (array $permission): v
'notes' => null,
'is_default' => false,
]);
$this->assertActivityFor('server:allocation.create', $user, $server, $matched);
})->with('permissionDataProvider');
test('allocation cannot be created if user does not have permission', function (): void {
/** @var Server $server */
@@ -22,6 +22,7 @@ test('allocation can be deleted from server', function (array $permission) {
$allocation = Allocation::factory()->create(['server_id' => $server->id, 'node_id' => $server->node_id, 'notes' => 'hodor']);
$this->actingAs($user)->deleteJson($this->link($allocation))->assertStatus(Response::HTTP_NO_CONTENT);
$this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => null, 'notes' => null]);
$this->assertActivityFor('server:allocation.delete', $user, $server, $allocation);
})->with('permissionDataProvider');
test('error is returned if user does not have permission', function () {
/** @var Server $server */
@@ -49,6 +49,7 @@ test('allocation notes can be updated', function (array $permissions): void {
$this->actingAs($user)->postJson($this->link($allocation), ['notes' => null])->assertOk()->assertJsonPath('object', Allocation::RESOURCE_NAME)->assertJsonPath('attributes.notes', null);
$allocation = $allocation->refresh();
expect($allocation->notes)->toBeNull();
$this->assertActivityFor('server:allocation.notes', $user, $server, $allocation);
})->with('updatePermissionsDataProvider');
test('allocation notes cannot be updated by invalid users', function (): void {
[$user, $server] = $this->generateTestAccount();
@@ -68,6 +69,7 @@ test('primary allocation can be modified', function (array $permissions): void {
$this->actingAs($user)->postJson($this->link($allocation2, '/primary'))->assertOk();
$server = $server->refresh();
expect($server->allocation_id)->toBe($allocation2->id);
$this->assertActivityFor('server:allocation.primary', $user, $server, $allocation2);
})->with('updatePermissionsDataProvider');
test('primary allocation cannot be modified by invalid user', function (): void {
[$user, $server] = $this->generateTestAccount();