Files
pterodactyl-panel/app/Http/Controllers/Api/Admin/Nodes/BulkDeleteAllocationsController.php
T
2026-10-12 05:18:55 -04:00

57 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Pterodactyl\Http\Controllers\Api\Admin\Nodes;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Allocations\DeletesAllocations;
use Pterodactyl\Facades\Activity;
use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController;
use Pterodactyl\Http\Requests\Api\Admin\Nodes\Allocations\BulkDeleteAllocationRequest;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Node;
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
#[Subgroup('Node Allocations', 'Create, update, retrieve, and delete node allocations.')]
class BulkDeleteAllocationsController extends AdminApiController
{
/**
* Delete selected unassigned allocations.
*/
#[Endpoint('Bulk delete node allocations', 'Deletes multiple unassigned allocations from a node by allocation ID.')]
#[ScribeResponse(status: 204, description: 'Allocations deleted.')]
#[ScribeResponse(AllocationController::SERVER_USING_ALLOCATION_ERROR, status: 400, description: 'One of the allocations is currently assigned to a server.')]
public function __invoke(BulkDeleteAllocationRequest $request, DeletesAllocations $deletions, Node $node): Response
{
// All or nothing: one allocation a server holds rejects the whole selection, rather
// than leaving the ones before it deleted.
$allocations = DB::transaction(function () use ($request, $deletions, $node): Collection {
$allocations = $node->allocations()
->whereIn('id', $request->validated('ids', []))
->lockForUpdate()
->get();
$allocations->each(fn (Allocation $allocation) => $deletions->delete($allocation));
return $allocations;
});
foreach ($allocations as $allocation) {
Activity::event('admin:node-allocation.delete')
->subject($allocation)
->property('address', $allocation->ip)
->property('port', $allocation->port)
->log();
}
return $this->returnNoContent();
}
}