misc_pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php

126 lines
4.6 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
2021-03-05 17:03:12 +00:00
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Services\Allocations\FindAssignableAllocationService;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
class NetworkAllocationController extends ClientApiController
{
2021-03-05 17:03:12 +00:00
private AllocationRepository $repository;
private ServerRepository $serverRepository;
private FindAssignableAllocationService $assignableAllocationService;
/**
* NetworkController constructor.
*/
public function __construct(
AllocationRepository $repository,
ServerRepository $serverRepository,
FindAssignableAllocationService $assignableAllocationService
) {
parent::__construct();
$this->repository = $repository;
$this->serverRepository = $serverRepository;
$this->assignableAllocationService = $assignableAllocationService;
}
/**
2021-03-05 17:03:12 +00:00
* Lists all of the allocations available to a server and whether or
* not they are currently assigned as the primary for this server.
2021-03-05 17:03:12 +00:00
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function index(GetNetworkRequest $request, Server $server): array
{
return $this->fractal->collection($server->allocations)
2021-08-07 21:32:40 +00:00
->transformWith(AllocationTransformer::class)
->toArray();
}
/**
* Set the primary allocation for a server.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function update(UpdateAllocationRequest $request, Server $server, Allocation $allocation): array
{
$allocation = $this->repository->update($allocation->id, [
'notes' => $request->input('notes'),
]);
return $this->fractal->item($allocation)
2021-08-07 21:32:40 +00:00
->transformWith(AllocationTransformer::class)
->toArray();
}
/**
* Set the primary allocation for a server.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array
{
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
return $this->fractal->item($allocation)
2021-08-07 21:32:40 +00:00
->transformWith(AllocationTransformer::class)
->toArray();
}
/**
* Set the notes for the allocation for a server.
*
* @throws \Pterodactyl\Exceptions\DisplayException
2021-03-05 17:03:12 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function store(NewAllocationRequest $request, Server $server): array
{
if ($server->allocations()->count() >= $server->allocation_limit) {
2021-01-23 20:33:34 +00:00
throw new DisplayException('Cannot assign additional allocations to this server: limit has been reached.');
}
$allocation = $this->assignableAllocationService->handle($server);
return $this->fractal->item($allocation)
2021-08-07 21:32:40 +00:00
->transformWith(AllocationTransformer::class)
->toArray();
}
/**
* Delete an allocation from a server.
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
2021-03-05 17:03:12 +00:00
public function delete(DeleteAllocationRequest $request, Server $server, Allocation $allocation): Response
{
if ($allocation->id === $server->allocation_id) {
2021-01-23 20:33:34 +00:00
throw new DisplayException('You cannot delete the primary allocation for this server.');
}
Allocation::query()->where('id', $allocation->id)->update([
'notes' => null,
'server_id' => null,
]);
2021-03-05 17:03:12 +00:00
return $this->returnNoContent();
}
}