diff --git a/app/Actions/Allocations/AssignAvailableAllocation.php b/app/Actions/Allocations/AssignAvailableAllocation.php index 5f03dceb9..f7ba4a8a5 100644 --- a/app/Actions/Allocations/AssignAvailableAllocation.php +++ b/app/Actions/Allocations/AssignAvailableAllocation.php @@ -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; } } diff --git a/app/Actions/Allocations/SetPrimaryAllocation.php b/app/Actions/Allocations/SetPrimaryAllocation.php index 7e817ff4a..003f1d11f 100644 --- a/app/Actions/Allocations/SetPrimaryAllocation.php +++ b/app/Actions/Allocations/SetPrimaryAllocation.php @@ -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'); } diff --git a/app/Actions/Allocations/UnassignAllocation.php b/app/Actions/Allocations/UnassignAllocation.php index b0941967c..8799a8413 100644 --- a/app/Actions/Allocations/UnassignAllocation.php +++ b/app/Actions/Allocations/UnassignAllocation.php @@ -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(); } } diff --git a/app/Actions/Allocations/UpdateAllocationNotes.php b/app/Actions/Allocations/UpdateAllocationNotes.php index 6cd630af6..56b9aa869 100644 --- a/app/Actions/Allocations/UpdateAllocationNotes.php +++ b/app/Actions/Allocations/UpdateAllocationNotes.php @@ -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; } } diff --git a/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php b/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php index ad7e966cc..04f53c8fb 100644 --- a/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php +++ b/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php @@ -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); } diff --git a/tests/Integration/Actions/Allocations/AssignAvailableAllocationTest.php b/tests/Integration/Actions/Allocations/AssignAvailableAllocationTest.php index 6ec7ca40f..500ac4510 100644 --- a/tests/Integration/Actions/Allocations/AssignAvailableAllocationTest.php +++ b/tests/Integration/Actions/Allocations/AssignAvailableAllocationTest.php @@ -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(); diff --git a/tests/Integration/Api/Client/Server/Allocation/CreateNewAllocationTest.php b/tests/Integration/Api/Client/Server/Allocation/CreateNewAllocationTest.php index b296a92cc..bb2bec8b2 100644 --- a/tests/Integration/Api/Client/Server/Allocation/CreateNewAllocationTest.php +++ b/tests/Integration/Api/Client/Server/Allocation/CreateNewAllocationTest.php @@ -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 */ diff --git a/tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.php b/tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.php index 03b020b62..a19ef517d 100644 --- a/tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.php +++ b/tests/Integration/Api/Client/Server/Allocation/DeleteAllocationTest.php @@ -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 */ diff --git a/tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php b/tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php index 0934a4a2f..0b642339a 100644 --- a/tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php +++ b/tests/Integration/Api/Client/Server/NetworkAllocationControllerTest.php @@ -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();