More API updates, better support for node config edits
This commit is contained in:
parent
800e2df6b2
commit
cf21fd5a4b
21 changed files with 449 additions and 125 deletions
|
@ -3,6 +3,7 @@
|
|||
namespace Pterodactyl\Contracts\Repository;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface AllocationRepositoryInterface extends RepositoryInterface
|
||||
{
|
||||
|
@ -23,6 +24,15 @@ interface AllocationRepositoryInterface extends RepositoryInterface
|
|||
*/
|
||||
public function getAllocationsForNode(int $node): Collection;
|
||||
|
||||
/**
|
||||
* Return all of the allocations for a node in a paginated format.
|
||||
*
|
||||
* @param int $node
|
||||
* @param int $perPage
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Return all of the unique IPs that exist for a given node.
|
||||
*
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Pterodactyl\Contracts\Repository;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
|
||||
interface RepositoryInterface
|
||||
{
|
||||
|
@ -175,6 +176,14 @@ interface RepositoryInterface
|
|||
*/
|
||||
public function all(): Collection;
|
||||
|
||||
/**
|
||||
* Return a paginated result set using a search term if set on the repository.
|
||||
*
|
||||
* @param int $perPage
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public function paginated(int $perPage): LengthAwarePaginator;
|
||||
|
||||
/**
|
||||
* Insert a single or multiple records into the database at once skipping
|
||||
* validation and mass assignment checking.
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Exceptions\Service\Allocation;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
||||
class ServerUsingAllocationException extends DisplayException
|
||||
{
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Exceptions\Service\Node;
|
||||
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
|
||||
class ConfigurationNotPersistedException extends DisplayException
|
||||
{
|
||||
}
|
|
@ -74,7 +74,7 @@ class LocationController extends Controller
|
|||
*/
|
||||
public function index(Request $request): array
|
||||
{
|
||||
$locations = $this->repository->all(50);
|
||||
$locations = $this->repository->paginated(100);
|
||||
|
||||
return $this->fractal->collection($locations)
|
||||
->transformWith(new LocationTransformer($request))
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\API\Admin\Nodes;
|
||||
|
||||
use Spatie\Fractal\Fractal;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||
use Pterodactyl\Transformers\Api\Admin\AllocationTransformer;
|
||||
use Pterodactyl\Services\Allocations\AllocationDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
|
||||
class AllocationController extends Controller
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Allocations\AllocationDeletionService
|
||||
*/
|
||||
private $deletionService;
|
||||
|
||||
/**
|
||||
* @var \Spatie\Fractal\Fractal
|
||||
*/
|
||||
private $fractal;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* AllocationController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $deletionService
|
||||
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
|
||||
* @param \Spatie\Fractal\Fractal $fractal
|
||||
*/
|
||||
public function __construct(AllocationDeletionService $deletionService, AllocationRepositoryInterface $repository, Fractal $fractal)
|
||||
{
|
||||
$this->deletionService = $deletionService;
|
||||
$this->fractal = $fractal;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the allocations that exist for a given node.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $node
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request, int $node): array
|
||||
{
|
||||
$allocations = $this->repository->getPaginatedAllocationsForNode($node, 100);
|
||||
|
||||
return $this->fractal->collection($allocations)
|
||||
->transformWith(new AllocationTransformer($request))
|
||||
->withResourceName('allocation')
|
||||
->paginateWith(new IlluminatePaginatorAdapter($allocations))
|
||||
->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a specific allocation from the Panel.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Allocation $allocation
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
|
||||
*/
|
||||
public function delete(Request $request, int $node, Allocation $allocation): Response
|
||||
{
|
||||
$this->deletionService->handle($allocation);
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
}
|
|
@ -74,7 +74,7 @@ class NodeController extends Controller
|
|||
*/
|
||||
public function index(Request $request): array
|
||||
{
|
||||
$nodes = $this->repository->all(config('pterodactyl.paginate.api.nodes'));
|
||||
$nodes = $this->repository->paginated(100);
|
||||
|
||||
$fractal = $this->fractal->collection($nodes)
|
||||
->transformWith(new NodeTransformer($request))
|
||||
|
|
|
@ -76,7 +76,7 @@ class UserController extends Controller
|
|||
*/
|
||||
public function index(Request $request): array
|
||||
{
|
||||
$users = $this->repository->all(config('pterodactyl.paginate.api.users'));
|
||||
$users = $this->repository->paginated(100);
|
||||
|
||||
return $this->fractal->collection($users)
|
||||
->transformWith(new UserTransformer($request))
|
||||
|
@ -113,7 +113,6 @@ class UserController extends Controller
|
|||
* @param \Pterodactyl\Models\User $user
|
||||
* @return array
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
|
|
|
@ -12,6 +12,8 @@ namespace Pterodactyl\Http\Controllers\Admin;
|
|||
use Javascript;
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Illuminate\Http\Response;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Services\Nodes\NodeUpdateService;
|
||||
|
@ -23,6 +25,7 @@ use Pterodactyl\Services\Helpers\SoftwareVersionService;
|
|||
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
|
||||
use Pterodactyl\Services\Allocations\AllocationDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Http\Requests\Admin\Node\AllocationAliasFormRequest;
|
||||
|
@ -78,11 +81,16 @@ class NodesController extends Controller
|
|||
* @var \Pterodactyl\Services\Helpers\SoftwareVersionService
|
||||
*/
|
||||
protected $versionService;
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Allocations\AllocationDeletionService
|
||||
*/
|
||||
private $allocationDeletionService;
|
||||
|
||||
/**
|
||||
* NodesController constructor.
|
||||
*
|
||||
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
||||
* @param \Pterodactyl\Services\Allocations\AllocationDeletionService $allocationDeletionService
|
||||
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
|
||||
* @param \Pterodactyl\Services\Allocations\AssignmentService $assignmentService
|
||||
* @param \Illuminate\Cache\Repository $cache
|
||||
|
@ -95,6 +103,7 @@ class NodesController extends Controller
|
|||
*/
|
||||
public function __construct(
|
||||
AlertsMessageBag $alert,
|
||||
AllocationDeletionService $allocationDeletionService,
|
||||
AllocationRepositoryInterface $allocationRepository,
|
||||
AssignmentService $assignmentService,
|
||||
CacheRepository $cache,
|
||||
|
@ -106,6 +115,7 @@ class NodesController extends Controller
|
|||
SoftwareVersionService $versionService
|
||||
) {
|
||||
$this->alert = $alert;
|
||||
$this->allocationDeletionService = $allocationDeletionService;
|
||||
$this->allocationRepository = $allocationRepository;
|
||||
$this->assignmentService = $assignmentService;
|
||||
$this->cache = $cache;
|
||||
|
@ -262,17 +272,14 @@ class NodesController extends Controller
|
|||
/**
|
||||
* Removes a single allocation from a node.
|
||||
*
|
||||
* @param int $node
|
||||
* @param int $allocation
|
||||
* @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse
|
||||
* @param \Pterodactyl\Models\Allocation $allocation
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
|
||||
*/
|
||||
public function allocationRemoveSingle($node, $allocation)
|
||||
public function allocationRemoveSingle(Allocation $allocation): Response
|
||||
{
|
||||
$this->allocationRepository->deleteWhere([
|
||||
['id', '=', $allocation],
|
||||
['node_id', '=', $node],
|
||||
['server_id', '=', null],
|
||||
]);
|
||||
$this->allocationDeletionService->handle($allocation);
|
||||
|
||||
return response('', 204);
|
||||
}
|
||||
|
|
|
@ -105,4 +105,14 @@ class Allocation extends Model implements CleansAttributes, ValidableContract
|
|||
{
|
||||
return $this->belongsTo(Server::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the Node model associated with this allocation.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function node()
|
||||
{
|
||||
return $this->belongsTo(Node::class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
namespace Pterodactyl\Repositories\Eloquent;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
|
||||
class AllocationRepository extends EloquentRepository implements AllocationRepositoryInterface
|
||||
|
@ -41,6 +43,18 @@ class AllocationRepository extends EloquentRepository implements AllocationRepos
|
|||
return $this->getBuilder()->where('node_id', $node)->get($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the allocations for a node in a paginated format.
|
||||
*
|
||||
* @param int $node
|
||||
* @param int $perPage
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public function getPaginatedAllocationsForNode(int $node, int $perPage = 100): LengthAwarePaginator
|
||||
{
|
||||
return $this->getBuilder()->where('node_id', $node)->paginate($perPage, $this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all of the unique IPs that exist for a given node.
|
||||
*
|
||||
|
|
|
@ -7,6 +7,7 @@ use Illuminate\Support\Collection;
|
|||
use Pterodactyl\Repositories\Repository;
|
||||
use Illuminate\Database\Query\Expression;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Pterodactyl\Contracts\Repository\RepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Model\DataValidationException;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
|
@ -234,6 +235,22 @@ abstract class EloquentRepository extends Repository implements RepositoryInterf
|
|||
return $instance->get($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a paginated result set using a search term if set on the repository.
|
||||
*
|
||||
* @param int $perPage
|
||||
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
||||
*/
|
||||
public function paginated(int $perPage): LengthAwarePaginator
|
||||
{
|
||||
$instance = $this->getBuilder();
|
||||
if (is_subclass_of(get_called_class(), SearchableInterface::class) && $this->hasSearchTerm()) {
|
||||
$instance = $instance->search($this->getSearchTerm());
|
||||
}
|
||||
|
||||
return $instance->paginate($perPage, $this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a single or multiple records into the database at once skipping
|
||||
* validation and mass assignment checking.
|
||||
|
|
43
app/Services/Allocations/AllocationDeletionService.php
Normal file
43
app/Services/Allocations/AllocationDeletionService.php
Normal file
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Services\Allocations;
|
||||
|
||||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException;
|
||||
|
||||
class AllocationDeletionService
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* AllocationDeletionService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(AllocationRepositoryInterface $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an allocation from the database only if it does not have a server
|
||||
* that is actively attached to it.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Allocation $allocation
|
||||
* @return int
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
|
||||
*/
|
||||
public function handle(Allocation $allocation)
|
||||
{
|
||||
if (! is_null($allocation->server_id)) {
|
||||
throw new ServerUsingAllocationException(trans('exceptions.allocations.server_using'));
|
||||
}
|
||||
|
||||
return $this->repository->delete($allocation->id);
|
||||
}
|
||||
}
|
|
@ -10,16 +10,24 @@
|
|||
namespace Pterodactyl\Services\Nodes;
|
||||
|
||||
use Pterodactyl\Models\Node;
|
||||
use GuzzleHttp\Exception\ConnectException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Traits\Services\ReturnsUpdatedModels;
|
||||
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
|
||||
|
||||
class NodeUpdateService
|
||||
{
|
||||
use ReturnsUpdatedModels;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface
|
||||
*/
|
||||
|
@ -33,13 +41,16 @@ class NodeUpdateService
|
|||
/**
|
||||
* UpdateService constructor.
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionInterface $connection
|
||||
* @param \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface $configurationRepository
|
||||
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(
|
||||
ConnectionInterface $connection,
|
||||
ConfigurationRepositoryInterface $configurationRepository,
|
||||
NodeRepositoryInterface $repository
|
||||
) {
|
||||
$this->connection = $connection;
|
||||
$this->configRepository = $configurationRepository;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
@ -62,6 +73,7 @@ class NodeUpdateService
|
|||
unset($data['reset_secret']);
|
||||
}
|
||||
|
||||
$this->connection->beginTransaction();
|
||||
if ($this->getUpdatedModel()) {
|
||||
$response = $this->repository->update($node->id, $data);
|
||||
} else {
|
||||
|
@ -70,7 +82,16 @@ class NodeUpdateService
|
|||
|
||||
try {
|
||||
$this->configRepository->setNode($node)->update();
|
||||
$this->connection->commit();
|
||||
} catch (RequestException $exception) {
|
||||
// Failed to connect to the Daemon. Let's go ahead and save the configuration
|
||||
// and let the user know they'll need to manually update.
|
||||
if ($exception instanceof ConnectException) {
|
||||
$this->connection->commit();
|
||||
|
||||
throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));
|
||||
}
|
||||
|
||||
throw new DaemonConnectionException($exception);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,16 @@ use Pterodactyl\Transformers\Api\ApiTransformer;
|
|||
|
||||
class AllocationTransformer extends ApiTransformer
|
||||
{
|
||||
/**
|
||||
* Relationships that can be loaded onto allocation transformations.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $availableIncludes = [
|
||||
'node',
|
||||
'server',
|
||||
];
|
||||
|
||||
/**
|
||||
* Return a generic transformed allocation array.
|
||||
*
|
||||
|
@ -15,17 +25,50 @@ class AllocationTransformer extends ApiTransformer
|
|||
*/
|
||||
public function transform(Allocation $allocation)
|
||||
{
|
||||
return $this->transformWithFilter($allocation);
|
||||
return [
|
||||
'id' => $allocation->id,
|
||||
'ip' => $allocation->ip,
|
||||
'alias' => $allocation->ip_alias,
|
||||
'port' => $allocation->port,
|
||||
'assigned' => ! is_null($allocation->server_id),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine which transformer filter to apply.
|
||||
* Load the node relationship onto a given transformation.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Allocation $allocation
|
||||
* @return array
|
||||
* @return bool|\League\Fractal\Resource\Item
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\PterodactylException
|
||||
*/
|
||||
protected function transformWithFilter(Allocation $allocation)
|
||||
public function includeNode(Allocation $allocation)
|
||||
{
|
||||
return $allocation->toArray();
|
||||
if (! $this->authorize('node-view')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$allocation->loadMissing('node');
|
||||
|
||||
return $this->item($allocation->getRelation('node'), new NodeTransformer($this->getRequest()), 'node');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the server relationship onto a given transformation.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Allocation $allocation
|
||||
* @return bool|\League\Fractal\Resource\Item
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\PterodactylException
|
||||
*/
|
||||
public function includeServer(Allocation $allocation)
|
||||
{
|
||||
if (! $this->authorize('server-view')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$allocation->loadMissing('server');
|
||||
|
||||
return $this->item($allocation->getRelation('server'), new ServerTransformer($this->getRequest()), 'server');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,13 +35,11 @@ class UserTransformer extends ApiTransformer
|
|||
*/
|
||||
public function includeServers(User $user)
|
||||
{
|
||||
if ($this->authorize('server-list')) {
|
||||
if (! $this->authorize('server-list')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $user->relationLoaded('servers')) {
|
||||
$user->load('servers');
|
||||
}
|
||||
$user->loadMissing('servers');
|
||||
|
||||
return $this->collection($user->getRelation('servers'), new ServerTransformer($this->getRequest()), 'server');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue