2019-03-24 00:47:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-07-10 02:56:46 +00:00
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\AllocationRepository;
|
2019-03-24 00:47:20 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest;
|
2020-07-10 02:56:46 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest;
|
2019-03-24 00:47:20 +00:00
|
|
|
|
|
|
|
class NetworkController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
2019-09-06 04:41:20 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\AllocationRepository
|
2019-03-24 00:47:20 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $serverRepository;
|
|
|
|
|
2019-03-24 00:47:20 +00:00
|
|
|
/**
|
|
|
|
* NetworkController constructor.
|
|
|
|
*
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\AllocationRepository $repository
|
2020-07-10 02:56:46 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
|
2019-03-24 00:47:20 +00:00
|
|
|
*/
|
2020-07-10 02:56:46 +00:00
|
|
|
public function __construct(
|
|
|
|
AllocationRepository $repository,
|
|
|
|
ServerRepository $serverRepository
|
|
|
|
) {
|
2019-03-24 00:47:20 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
2020-07-10 02:56:46 +00:00
|
|
|
$this->serverRepository = $serverRepository;
|
2019-03-24 00:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists all of the allocations available to a server and wether or
|
|
|
|
* not they are currently assigned as the primary for this server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest $request
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2019-03-24 00:47:20 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function index(GetNetworkRequest $request, Server $server): array
|
2019-03-24 00:47:20 +00:00
|
|
|
{
|
2020-07-10 02:56:46 +00:00
|
|
|
return $this->fractal->collection($server->allocations)
|
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
2019-03-24 00:47:20 +00:00
|
|
|
|
2020-07-10 02:56:46 +00:00
|
|
|
return $this->fractal->item($allocation)
|
2019-03-24 00:47:20 +00:00
|
|
|
->transformWith($this->getTransformer(AllocationTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
}
|