Path: blob/1.0-develop/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php
10280 views
<?php12namespace Pterodactyl\Http\Controllers\Api\Client\Servers;34use Pterodactyl\Models\Server;5use Illuminate\Http\JsonResponse;6use Pterodactyl\Facades\Activity;7use Pterodactyl\Models\Allocation;8use Illuminate\Database\ConnectionInterface;9use Pterodactyl\Exceptions\DisplayException;10use Pterodactyl\Repositories\Eloquent\ServerRepository;11use Pterodactyl\Transformers\Api\Client\AllocationTransformer;12use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;13use Pterodactyl\Services\Allocations\FindAssignableAllocationService;14use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;15use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;16use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;17use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;18use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;1920class NetworkAllocationController extends ClientApiController21{22/**23* NetworkAllocationController constructor.24*/25public function __construct(26protected readonly ConnectionInterface $connection,27private FindAssignableAllocationService $assignableAllocationService,28private ServerRepository $serverRepository,29) {30parent::__construct();31}3233/**34* Lists all the allocations available to a server and whether35* they are currently assigned as the primary for this server.36*/37public function index(GetNetworkRequest $request, Server $server): array38{39return $this->fractal->collection($server->allocations)40->transformWith($this->getTransformer(AllocationTransformer::class))41->toArray();42}4344/**45* Set the primary allocation for a server.46*47* @throws \Pterodactyl\Exceptions\Model\DataValidationException48* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException49*/50public function update(UpdateAllocationRequest $request, Server $server, Allocation $allocation): array51{52$original = $allocation->notes;5354$allocation->forceFill(['notes' => $request->input('notes')])->save();5556if ($original !== $allocation->notes) {57Activity::event('server:allocation.notes')58->subject($allocation)59->property(['allocation' => $allocation->toString(), 'old' => $original, 'new' => $allocation->notes])60->log();61}6263return $this->fractal->item($allocation)64->transformWith($this->getTransformer(AllocationTransformer::class))65->toArray();66}6768/**69* Set the primary allocation for a server.70*71* @throws \Pterodactyl\Exceptions\Model\DataValidationException72* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException73*/74public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array75{76$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);7778Activity::event('server:allocation.primary')79->subject($allocation)80->property('allocation', $allocation->toString())81->log();8283return $this->fractal->item($allocation)84->transformWith($this->getTransformer(AllocationTransformer::class))85->toArray();86}8788/**89* Set the notes for the allocation for a server.90*s.91*92* @throws DisplayException93*/94public function store(NewAllocationRequest $request, Server $server): array95{96$allocation = Activity::event('server:allocation.create')->transaction(function ($log) use ($server) {97if ($server->allocations()->lockForUpdate()->count() >= $server->allocation_limit) {98throw new DisplayException('Cannot assign additional allocations to this server: limit has been reached.');99}100101$allocation = $this->assignableAllocationService->handle($server);102103$log->subject($allocation)->property('allocation', $allocation->toString());104105return $allocation;106});107108return $this->fractal->item($allocation)109->transformWith($this->getTransformer(AllocationTransformer::class))110->toArray();111}112113/**114* Delete an allocation from a server.115*116* @throws DisplayException117*/118public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation): JsonResponse119{120// Don't allow the deletion of allocations if the server does not have an121// allocation limit set.122if (empty($server->allocation_limit)) {123throw new DisplayException('You cannot delete allocations for this server: no allocation limit is set.');124}125126if ($allocation->id === $server->allocation_id) {127throw new DisplayException('You cannot delete the primary allocation for this server.');128}129130Allocation::query()->where('id', $allocation->id)->update([131'notes' => null,132'server_id' => null,133]);134135Activity::event('server:allocation.delete')136->subject($allocation)137->property('allocation', $allocation->toString())138->log();139140return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);141}142}143144145