Support modifying the primary allocation for a server

This commit is contained in:
Dane Everitt 2020-07-09 19:56:46 -07:00
parent bfb28f949d
commit fc9054312d
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
17 changed files with 230 additions and 87 deletions

View file

@ -3,10 +3,14 @@
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Pterodactyl\Models\Server;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
class NetworkController extends ClientApiController
{
@ -15,16 +19,25 @@ class NetworkController extends ClientApiController
*/
private $repository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $serverRepository;
/**
* NetworkController constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
*/
public function __construct(AllocationRepository $repository)
{
public function __construct(
AllocationRepository $repository,
ServerRepository $serverRepository
) {
parent::__construct();
$this->repository = $repository;
$this->serverRepository = $serverRepository;
}
/**
@ -37,11 +50,40 @@ class NetworkController extends ClientApiController
*/
public function index(GetNetworkRequest $request, Server $server): array
{
$allocations = $this->repository->findWhere([
['server_id', '=', $server->id],
]);
return $this->fractal->collection($server->allocations)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toArray();
}
return $this->fractal->collection($allocations)
/**
* Set the primary allocation for a server.
*
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest $request
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function storePrimary(SetPrimaryAllocationRequest $request, Server $server): array
{
try {
/** @var \Pterodactyl\Models\Allocation $allocation */
$allocation = $this->repository->findFirstWhere([
'server_id' => $server->id,
'ip' => $request->input('ip'),
'port' => $request->input('port'),
]);
} catch (ModelNotFoundException $exception) {
throw new DisplayException(
'The IP and port you selected are not available for this server.'
);
}
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]);
return $this->fractal->item($allocation)
->transformWith($this->getTransformer(AllocationTransformer::class))
->toArray();
}

View file

@ -0,0 +1,28 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Network;
use Pterodactyl\Models\Permission;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
class SetPrimaryAllocationRequest extends ClientApiRequest
{
/**
* @return string
*/
public function permission(): string
{
return Permission::ACTION_ALLOCIATION_UPDATE;
}
/**
* @return array
*/
public function rules(): array
{
return [
'ip' => 'required|string',
'port' => 'required|numeric|min:1024|max:65535',
];
}
}