Compare commits

...

8 commits

Author SHA1 Message Date
Lance Pioch
e522813b07 Pass in the id 2022-10-20 21:43:09 -04:00
Lance Pioch
35d97f153e Fix not 2022-10-20 21:20:39 -04:00
Lance Pioch
4d7ea155b1 Remove ServerRepository and ServerRepositoryInterface 2022-10-20 21:17:03 -04:00
Lance Pioch
22d560de64 Remove all of these, none of them are used 2022-10-20 20:25:49 -04:00
Lance Pioch
ed75f5cbfb Replace simple paginator 2022-10-20 20:21:57 -04:00
Lance Pioch
679a25a763 Replace unique uuid single call 2022-10-20 20:20:37 -04:00
Lance Pioch
3e405b626b This was never used 2022-10-20 20:20:05 -04:00
Lance Pioch
f45b7b5996 Replace egg relations method 2022-10-20 20:19:18 -04:00
20 changed files with 74 additions and 353 deletions

View file

@ -1,73 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\Server;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
interface ServerRepositoryInterface extends RepositoryInterface
{
/**
* Load the egg relations onto the server model.
*/
public function loadEggRelations(Server $server, bool $refresh = false): Server;
/**
* Return a collection of servers with their associated data for rebuild operations.
*/
public function getDataForRebuild(int $server = null, int $node = null): Collection;
/**
* Return a collection of servers with their associated data for reinstall operations.
*/
public function getDataForReinstall(int $server = null, int $node = null): Collection;
/**
* Return a server model and all variables associated with the server.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function findWithVariables(int $id): Server;
/**
* Get the primary allocation for a given server. If a model is passed into
* the function, load the allocation relationship onto it. Otherwise, find and
* return the server from the database.
*/
public function getPrimaryAllocation(Server $server, bool $refresh = false): Server;
/**
* Return enough data to be used for the creation of a server via the daemon.
*/
public function getDataForCreation(Server $server, bool $refresh = false): Server;
/**
* Load associated databases onto the server model.
*/
public function loadDatabaseRelations(Server $server, bool $refresh = false): Server;
/**
* Get data for use when updating a server on the Daemon. Returns an array of
* the egg which is used for build and rebuild. Only loads relations
* if they are missing, or refresh is set to true.
*/
public function getDaemonServiceData(Server $server, bool $refresh = false): array;
/**
* Return a server by UUID.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getByUuid(string $uuid): Server;
/**
* Check if a given UUID and UUID-Short string are unique to a server.
*/
public function isUniqueUuidCombo(string $uuid, string $short): bool;
/**
* Returns all the servers that exist for a given node in a paginated response.
*/
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator;
}

View file

@ -10,7 +10,6 @@ use Pterodactyl\Models\Allocation;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\View\Factory as ViewFactory; use Illuminate\Contracts\View\Factory as ViewFactory;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Traits\Controllers\JavascriptInjection; use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Services\Helpers\SoftwareVersionService; use Pterodactyl\Services\Helpers\SoftwareVersionService;
use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository;
@ -27,7 +26,6 @@ class NodeViewController extends Controller
private AllocationRepository $allocationRepository, private AllocationRepository $allocationRepository,
private LocationRepository $locationRepository, private LocationRepository $locationRepository,
private NodeRepository $repository, private NodeRepository $repository,
private ServerRepository $serverRepository,
private SoftwareVersionService $versionService, private SoftwareVersionService $versionService,
private ViewFactory $view private ViewFactory $view
) { ) {
@ -96,7 +94,7 @@ class NodeViewController extends Controller
return $this->view->make('admin.nodes.view.servers', [ return $this->view->make('admin.nodes.view.servers', [
'node' => $node, 'node' => $node,
'servers' => $this->serverRepository->loadAllServersForNode($node->id, 25), 'servers' => $node->servers()->with(['user', 'nest', 'egg'])->paginate(25),
]); ]);
} }
} }

View file

@ -19,7 +19,6 @@ use Pterodactyl\Services\Allocations\AssignmentService;
use Pterodactyl\Services\Helpers\SoftwareVersionService; use Pterodactyl\Services\Helpers\SoftwareVersionService;
use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest; use Pterodactyl\Http\Requests\Admin\Node\NodeFormRequest;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest; use Pterodactyl\Http\Requests\Admin\Node\AllocationFormRequest;
use Pterodactyl\Services\Allocations\AllocationDeletionService; use Pterodactyl\Services\Allocations\AllocationDeletionService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
@ -41,7 +40,6 @@ class NodesController extends Controller
protected NodeDeletionService $deletionService, protected NodeDeletionService $deletionService,
protected LocationRepositoryInterface $locationRepository, protected LocationRepositoryInterface $locationRepository,
protected NodeRepositoryInterface $repository, protected NodeRepositoryInterface $repository,
protected ServerRepositoryInterface $serverRepository,
protected NodeUpdateService $updateService, protected NodeUpdateService $updateService,
protected SoftwareVersionService $versionService, protected SoftwareVersionService $versionService,
protected ViewFactory $view protected ViewFactory $view

View file

@ -14,7 +14,6 @@ use Illuminate\Contracts\View\Factory as ViewFactory;
use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\MountRepository; use Pterodactyl\Repositories\Eloquent\MountRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Traits\Controllers\JavascriptInjection; use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
@ -32,7 +31,6 @@ class ServerViewController extends Controller
private MountRepository $mountRepository, private MountRepository $mountRepository,
private NestRepository $nestRepository, private NestRepository $nestRepository,
private NodeRepository $nodeRepository, private NodeRepository $nodeRepository,
private ServerRepository $repository,
private EnvironmentService $environmentService, private EnvironmentService $environmentService,
private ViewFactory $view private ViewFactory $view
) { ) {

View file

@ -28,7 +28,6 @@ use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Services\Databases\DatabaseManagementService; use Pterodactyl\Services\Databases\DatabaseManagementService;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService; use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
@ -52,7 +51,6 @@ class ServersController extends Controller
protected ServerDeletionService $deletionService, protected ServerDeletionService $deletionService,
protected DetailsModificationService $detailsModificationService, protected DetailsModificationService $detailsModificationService,
protected ReinstallServerService $reinstallService, protected ReinstallServerService $reinstallService,
protected ServerRepositoryInterface $repository,
protected MountRepository $mountRepository, protected MountRepository $mountRepository,
protected NestRepositoryInterface $nestRepository, protected NestRepositoryInterface $nestRepository,
protected ServerConfigurationStructureService $serverConfigurationStructureService, protected ServerConfigurationStructureService $serverConfigurationStructureService,
@ -91,9 +89,8 @@ class ServersController extends Controller
throw new DisplayException(trans('admin/server.exceptions.marked_as_failed')); throw new DisplayException(trans('admin/server.exceptions.marked_as_failed'));
} }
$this->repository->update($server->id, [ $server->status = $server->isInstalled() ? Server::STATUS_INSTALLING : null;
'status' => $server->isInstalled() ? Server::STATUS_INSTALLING : null, $server->save();
], true, true);
$this->alert->success(trans('admin/server.alerts.install_toggled'))->flash(); $this->alert->success(trans('admin/server.alerts.install_toggled'))->flash();

View file

@ -7,7 +7,6 @@ use Illuminate\Http\JsonResponse;
use Pterodactyl\Facades\Activity; use Pterodactyl\Facades\Activity;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Transformers\Api\Client\AllocationTransformer; use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Services\Allocations\FindAssignableAllocationService; use Pterodactyl\Services\Allocations\FindAssignableAllocationService;
@ -22,10 +21,8 @@ class NetworkAllocationController extends ClientApiController
/** /**
* NetworkAllocationController constructor. * NetworkAllocationController constructor.
*/ */
public function __construct( public function __construct(private FindAssignableAllocationService $assignableAllocationService)
private FindAssignableAllocationService $assignableAllocationService, {
private ServerRepository $serverRepository
) {
parent::__construct(); parent::__construct();
} }
@ -72,7 +69,8 @@ class NetworkAllocationController extends ClientApiController
*/ */
public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array public function setPrimary(SetPrimaryAllocationRequest $request, Server $server, Allocation $allocation): array
{ {
$this->serverRepository->update($server->id, ['allocation_id' => $allocation->id]); $server->allocation()->associate($allocation);
$server->save();
Activity::event('server:allocation.primary') Activity::event('server:allocation.primary')
->subject($allocation) ->subject($allocation)

View file

@ -6,7 +6,6 @@ use Illuminate\Http\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pterodactyl\Facades\Activity; use Pterodactyl\Facades\Activity;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\ReinstallServerService; use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
@ -19,10 +18,8 @@ class SettingsController extends ClientApiController
/** /**
* SettingsController constructor. * SettingsController constructor.
*/ */
public function __construct( public function __construct(private ReinstallServerService $reinstallServerService)
private ServerRepository $repository, {
private ReinstallServerService $reinstallServerService
) {
parent::__construct(); parent::__construct();
} }
@ -34,15 +31,12 @@ class SettingsController extends ClientApiController
*/ */
public function rename(RenameServerRequest $request, Server $server): JsonResponse public function rename(RenameServerRequest $request, Server $server): JsonResponse
{ {
$this->repository->update($server->id, [ $server->name = $request->input('name');
'name' => $request->input('name'), $server->save();
]);
if ($server->name !== $request->input('name')) { Activity::event('server:settings.rename')
Activity::event('server:settings.rename') ->property(['old' => $server->name, 'new' => $request->input('name')])
->property(['old' => $server->name, 'new' => $request->input('name')]) ->log();
->log();
}
return new JsonResponse([], Response::HTTP_NO_CONTENT); return new JsonResponse([], Response::HTTP_NO_CONTENT);
} }

View file

@ -5,15 +5,15 @@ namespace Pterodactyl\Http\Controllers\Api\Remote;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Servers\EnvironmentService; use Pterodactyl\Services\Servers\EnvironmentService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class EggInstallController extends Controller class EggInstallController extends Controller
{ {
/** /**
* EggInstallController constructor. * EggInstallController constructor.
*/ */
public function __construct(private EnvironmentService $environment, private ServerRepositoryInterface $repository) public function __construct(private EnvironmentService $environment)
{ {
} }
@ -21,19 +21,17 @@ class EggInstallController extends Controller
* Handle request to get script and installation information for a server * Handle request to get script and installation information for a server
* that is being created on the node. * that is being created on the node.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function index(Request $request, string $uuid): JsonResponse public function index(Request $request, string $uuid): JsonResponse
{ {
$node = $request->attributes->get('node'); $node = $request->attributes->get('node');
/** @var \Pterodactyl\Models\Server $server */ /** @var Server $server */
$server = $this->repository->findFirstWhere([ $server = Server::with('egg.scriptFrom')
['uuid', '=', $uuid], ->where('uuid', $uuid)
['node_id', '=', $node->id], ->where('node_id', $node->id)
]); ->firstOrFail();
$this->repository->loadEggRelations($server);
$egg = $server->getRelation('egg'); $egg = $server->getRelation('egg');
return response()->json([ return response()->json([

View file

@ -9,7 +9,6 @@ use Pterodactyl\Facades\Activity;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Eggs\EggConfigurationService; use Pterodactyl\Services\Eggs\EggConfigurationService;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection; use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection;
use Pterodactyl\Services\Servers\ServerConfigurationStructureService; use Pterodactyl\Services\Servers\ServerConfigurationStructureService;
@ -20,7 +19,6 @@ class ServerDetailsController extends Controller
*/ */
public function __construct( public function __construct(
protected ConnectionInterface $connection, protected ConnectionInterface $connection,
private ServerRepository $repository,
private ServerConfigurationStructureService $configurationStructureService, private ServerConfigurationStructureService $configurationStructureService,
private EggConfigurationService $eggConfigurationService private EggConfigurationService $eggConfigurationService
) { ) {
@ -30,11 +28,10 @@ class ServerDetailsController extends Controller
* Returns details about the server that allows Wings to self-recover and ensure * Returns details about the server that allows Wings to self-recover and ensure
* that the state of the server matches the Panel at all times. * that the state of the server matches the Panel at all times.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function __invoke(Request $request, string $uuid): JsonResponse public function __invoke(Request $request, string $uuid): JsonResponse
{ {
$server = $this->repository->getByUuid($uuid); $server = Server::findOrFailByUuid($uuid);
return new JsonResponse([ return new JsonResponse([
'settings' => $this->configurationStructureService->handle($server), 'settings' => $this->configurationStructureService->handle($server),

View file

@ -2,13 +2,13 @@
namespace Pterodactyl\Http\Controllers\Api\Remote\Servers; namespace Pterodactyl\Http\Controllers\Api\Remote\Servers;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\ServerRepository; use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerExceptionInterface;
use Pterodactyl\Events\Server\Installed as ServerInstalled; use Pterodactyl\Events\Server\Installed as ServerInstalled;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest; use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest;
@ -18,18 +18,16 @@ class ServerInstallController extends Controller
/** /**
* ServerInstallController constructor. * ServerInstallController constructor.
*/ */
public function __construct(private ServerRepository $repository, private EventDispatcher $eventDispatcher) public function __construct(private EventDispatcher $eventDispatcher)
{ {
} }
/** /**
* Returns installation information for a server. * Returns installation information for a server.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function index(Request $request, string $uuid): JsonResponse public function index(Request $request, string $uuid): JsonResponse
{ {
$server = $this->repository->getByUuid($uuid); $server = Server::findOrFailByUuid($uuid);
$egg = $server->egg; $egg = $server->egg;
return new JsonResponse([ return new JsonResponse([
@ -42,23 +40,27 @@ class ServerInstallController extends Controller
/** /**
* Updates the installation state of a server. * Updates the installation state of a server.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws ContainerExceptionInterface
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws NotFoundExceptionInterface
*/ */
public function store(InstallationDataRequest $request, string $uuid): JsonResponse public function store(InstallationDataRequest $request, string $uuid): JsonResponse
{ {
$server = $this->repository->getByUuid($uuid); $server = Server::findOrFailByUuid($uuid);
$status = $request->boolean('successful') ? null : Server::STATUS_INSTALL_FAILED; $status = $request->boolean('successful') ? null : Server::STATUS_INSTALL_FAILED;
if ($server->status === Server::STATUS_SUSPENDED) { if ($server->status === Server::STATUS_SUSPENDED) {
$status = Server::STATUS_SUSPENDED; $status = Server::STATUS_SUSPENDED;
} }
$this->repository->update($server->id, ['status' => $status, 'installed_at' => CarbonImmutable::now()], true, true); $previouslyInstalledAt = $server->installed_at;
$server->status = $status;
$server->installed_at = now();
$server->save();
// If the server successfully installed, fire installed event. // If the server successfully installed, fire installed event.
// This logic allows individually disabling install and reinstall notifications separately. // This logic allows individually disabling install and reinstall notifications separately.
$isInitialInstall = is_null($server->installed_at); $isInitialInstall = is_null($previouslyInstalledAt);
if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) { if ($isInitialInstall && config()->get('pterodactyl.email.send_install_notification', true)) {
$this->eventDispatcher->dispatch(new ServerInstalled($server)); $this->eventDispatcher->dispatch(new ServerInstalled($server));
} elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) { } elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) {

View file

@ -8,11 +8,11 @@ use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\ServerTransfer; use Pterodactyl\Models\ServerTransfer;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Nodes\NodeJWTService; use Pterodactyl\Services\Nodes\NodeJWTService;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Repositories\Wings\DaemonTransferRepository; use Pterodactyl\Repositories\Wings\DaemonTransferRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
@ -24,7 +24,6 @@ class ServerTransferController extends Controller
*/ */
public function __construct( public function __construct(
private ConnectionInterface $connection, private ConnectionInterface $connection,
private ServerRepository $repository,
private DaemonServerRepository $daemonServerRepository, private DaemonServerRepository $daemonServerRepository,
private DaemonTransferRepository $daemonTransferRepository, private DaemonTransferRepository $daemonTransferRepository,
private NodeJWTService $jwtService private NodeJWTService $jwtService
@ -39,7 +38,7 @@ class ServerTransferController extends Controller
*/ */
public function archive(Request $request, string $uuid): JsonResponse public function archive(Request $request, string $uuid): JsonResponse
{ {
$server = $this->repository->getByUuid($uuid); $server = Server::findOrFailByUuid($uuid);
// Unsuspend the server and don't continue the transfer. // Unsuspend the server and don't continue the transfer.
if (!$request->input('successful')) { if (!$request->input('successful')) {
@ -78,7 +77,7 @@ class ServerTransferController extends Controller
*/ */
public function failure(string $uuid): JsonResponse public function failure(string $uuid): JsonResponse
{ {
$server = $this->repository->getByUuid($uuid); $server = Server::findOrFailByUuid($uuid);
return $this->processFailedTransfer($server->transfer); return $this->processFailedTransfer($server->transfer);
} }
@ -90,7 +89,7 @@ class ServerTransferController extends Controller
*/ */
public function success(string $uuid): JsonResponse public function success(string $uuid): JsonResponse
{ {
$server = $this->repository->getByUuid($uuid); $server = Server::findOrFailByUuid($uuid);
$transfer = $server->transfer; $transfer = $server->transfer;
/** @var \Pterodactyl\Models\Server $server */ /** @var \Pterodactyl\Models\Server $server */

View file

@ -5,17 +5,14 @@ namespace Pterodactyl\Http\Controllers\Base;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class IndexController extends Controller class IndexController extends Controller
{ {
/** /**
* IndexController constructor. * IndexController constructor.
*/ */
public function __construct( public function __construct(protected ViewFactory $view)
protected ServerRepositoryInterface $repository, {
protected ViewFactory $view
) {
} }
/** /**

View file

@ -233,9 +233,9 @@ class Server extends Model
/** /**
* Gets the default allocation for a server. * Gets the default allocation for a server.
*/ */
public function allocation(): HasOne public function allocation(): BelongsTo
{ {
return $this->hasOne(Allocation::class, 'id', 'allocation_id'); return $this->belongsTo(Allocation::class);
} }
/** /**
@ -365,7 +365,7 @@ class Server extends Model
/** /**
* Checks if the server is currently in a transferable state. If not, an * Checks if the server is currently in a transferable state. If not, an
* exception is raised. This should be called whenever something needs to make * exception is raised. This should be called whenever something needs to make
* sure the server is able to be transferred and is not currently being transferred * sure the server can be transferred and is not currently being transferred
* or installed. * or installed.
*/ */
public function validateTransferState() public function validateTransferState()
@ -378,4 +378,15 @@ class Server extends Model
throw new ServerStateConflictException($this); throw new ServerStateConflictException($this);
} }
} }
public static function findOrFailByUuid(string $uuid): Server
{
/** @var Server $server */
$server = Server::with(['nest', 'node'])
->where('uuidShort', $uuid)
->orWhere('uuid', $uuid)
->firstOrFail();
return $server;
}
} }

View file

@ -9,7 +9,6 @@ use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository;
use Pterodactyl\Repositories\Eloquent\UserRepository; use Pterodactyl\Repositories\Eloquent\UserRepository;
use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; use Pterodactyl\Repositories\Eloquent\ApiKeyRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository;
use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
@ -25,7 +24,6 @@ use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
@ -56,7 +54,6 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(NestRepositoryInterface::class, NestRepository::class); $this->app->bind(NestRepositoryInterface::class, NestRepository::class);
$this->app->bind(NodeRepositoryInterface::class, NodeRepository::class); $this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
$this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class); $this->app->bind(ScheduleRepositoryInterface::class, ScheduleRepository::class);
$this->app->bind(ServerRepositoryInterface::class, ServerRepository::class);
$this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class); $this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class);
$this->app->bind(SessionRepositoryInterface::class, SessionRepository::class); $this->app->bind(SessionRepositoryInterface::class, SessionRepository::class);
$this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class);

View file

@ -1,179 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Server;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class ServerRepository extends EloquentRepository implements ServerRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Server::class;
}
/**
* Load the egg relations onto the server model.
*/
public function loadEggRelations(Server $server, bool $refresh = false): Server
{
if (!$server->relationLoaded('egg') || $refresh) {
$server->load('egg.scriptFrom');
}
return $server;
}
/**
* Return a collection of servers with their associated data for rebuild operations.
*/
public function getDataForRebuild(int $server = null, int $node = null): Collection
{
$instance = $this->getBuilder()->with(['allocation', 'allocations', 'egg', 'node']);
if (!is_null($server) && is_null($node)) {
$instance = $instance->where('id', '=', $server);
} elseif (is_null($server) && !is_null($node)) {
$instance = $instance->where('node_id', '=', $node);
}
return $instance->get($this->getColumns());
}
/**
* Return a collection of servers with their associated data for reinstall operations.
*/
public function getDataForReinstall(int $server = null, int $node = null): Collection
{
$instance = $this->getBuilder()->with(['allocation', 'allocations', 'egg', 'node']);
if (!is_null($server) && is_null($node)) {
$instance = $instance->where('id', '=', $server);
} elseif (is_null($server) && !is_null($node)) {
$instance = $instance->where('node_id', '=', $node);
}
return $instance->get($this->getColumns());
}
/**
* Return a server model and all variables associated with the server.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function findWithVariables(int $id): Server
{
try {
return $this->getBuilder()->with('egg.variables', 'variables')
->where($this->getModel()->getKeyName(), '=', $id)
->firstOrFail($this->getColumns());
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Get the primary allocation for a given server. If a model is passed into
* the function, load the allocation relationship onto it. Otherwise, find and
* return the server from the database.
*/
public function getPrimaryAllocation(Server $server, bool $refresh = false): Server
{
if (!$server->relationLoaded('allocation') || $refresh) {
$server->load('allocation');
}
return $server;
}
/**
* Return enough data to be used for the creation of a server via the daemon.
*/
public function getDataForCreation(Server $server, bool $refresh = false): Server
{
foreach (['allocation', 'allocations', 'egg'] as $relation) {
if (!$server->relationLoaded($relation) || $refresh) {
$server->load($relation);
}
}
return $server;
}
/**
* Load associated databases onto the server model.
*/
public function loadDatabaseRelations(Server $server, bool $refresh = false): Server
{
if (!$server->relationLoaded('databases') || $refresh) {
$server->load('databases.host');
}
return $server;
}
/**
* Get data for use when updating a server on the Daemon. Returns an array of
* the egg which is used for build and rebuild. Only loads relations
* if they are missing, or refresh is set to true.
*/
public function getDaemonServiceData(Server $server, bool $refresh = false): array
{
if (!$server->relationLoaded('egg') || $refresh) {
$server->load('egg');
}
return [
'egg' => $server->getRelation('egg')->uuid,
];
}
/**
* Return a server by UUID.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getByUuid(string $uuid): Server
{
try {
/** @var \Pterodactyl\Models\Server $model */
$model = $this->getBuilder()
->with('nest', 'node')
->where(function (Builder $query) use ($uuid) {
$query->where('uuidShort', $uuid)->orWhere('uuid', $uuid);
})
->firstOrFail($this->getColumns());
return $model;
} catch (ModelNotFoundException) {
throw new RecordNotFoundException();
}
}
/**
* Check if a given UUID and UUID-Short string are unique to a server.
*/
public function isUniqueUuidCombo(string $uuid, string $short): bool
{
return !$this->getBuilder()->where('uuid', '=', $uuid)->orWhere('uuidShort', '=', $short)->exists();
}
/**
* Returns all the servers that exist for a given node in a paginated response.
*/
public function loadAllServersForNode(int $node, int $limit): LengthAwarePaginator
{
return $this->getBuilder()
->with(['user', 'nest', 'egg'])
->where('node_id', '=', $node)
->paginate($limit);
}
}

View file

@ -2,10 +2,10 @@
namespace Pterodactyl\Services\Eggs; namespace Pterodactyl\Services\Eggs;
use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\HasChildrenException; use Pterodactyl\Exceptions\Service\Egg\HasChildrenException;
use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class EggDeletionService class EggDeletionService
{ {
@ -13,7 +13,6 @@ class EggDeletionService
* EggDeletionService constructor. * EggDeletionService constructor.
*/ */
public function __construct( public function __construct(
protected ServerRepositoryInterface $serverRepository,
protected EggRepositoryInterface $repository protected EggRepositoryInterface $repository
) { ) {
} }
@ -26,8 +25,7 @@ class EggDeletionService
*/ */
public function handle(int $egg): int public function handle(int $egg): int
{ {
$servers = $this->serverRepository->findCountWhere([['egg_id', '=', $egg]]); if (Server::query()->where('egg_id', $egg)->count()) {
if ($servers > 0) {
throw new HasActiveServersException(trans('exceptions.nest.egg.delete_has_servers')); throw new HasActiveServersException(trans('exceptions.nest.egg.delete_has_servers'));
} }

View file

@ -2,9 +2,9 @@
namespace Pterodactyl\Services\Nests; namespace Pterodactyl\Services\Nests;
use Pterodactyl\Models\Server;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class NestDeletionService class NestDeletionService
{ {
@ -12,7 +12,6 @@ class NestDeletionService
* NestDeletionService constructor. * NestDeletionService constructor.
*/ */
public function __construct( public function __construct(
protected ServerRepositoryInterface $serverRepository,
protected NestRepositoryInterface $repository protected NestRepositoryInterface $repository
) { ) {
} }
@ -20,12 +19,11 @@ class NestDeletionService
/** /**
* Delete a nest from the system only if there are no servers attached to it. * Delete a nest from the system only if there are no servers attached to it.
* *
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException * @throws HasActiveServersException
*/ */
public function handle(int $nest): int public function handle(int $nest): int
{ {
$count = $this->serverRepository->findCountWhere([['nest_id', '=', $nest]]); if (Server::query()->where('nest_id', $nest)->count() > 0) {
if ($count > 0) {
throw new HasActiveServersException(trans('exceptions.nest.delete_has_servers')); throw new HasActiveServersException(trans('exceptions.nest.delete_has_servers'));
} }

View file

@ -6,7 +6,6 @@ use Pterodactyl\Models\Node;
use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class NodeDeletionService class NodeDeletionService
{ {
@ -15,7 +14,6 @@ class NodeDeletionService
*/ */
public function __construct( public function __construct(
protected NodeRepositoryInterface $repository, protected NodeRepositoryInterface $repository,
protected ServerRepositoryInterface $serverRepository,
protected Translator $translator protected Translator $translator
) { ) {
} }
@ -23,16 +21,15 @@ class NodeDeletionService
/** /**
* Delete a node from the panel if no servers are attached to it. * Delete a node from the panel if no servers are attached to it.
* *
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException * @throws HasActiveServersException
*/ */
public function handle(int|Node $node): int public function handle(int|Node $node): int
{ {
if ($node instanceof Node) { if (is_int($node)) {
$node = $node->id; $node = Node::query()->findOrFail($node);
} }
$servers = $this->serverRepository->setColumns('id')->findCountWhere([['node_id', '=', $node]]); if ($node->servers()->count() > 0) {
if ($servers > 0) {
throw new HasActiveServersException($this->translator->get('exceptions.node.servers_attached')); throw new HasActiveServersException($this->translator->get('exceptions.node.servers_attached'));
} }

View file

@ -12,7 +12,6 @@ use Illuminate\Support\Collection;
use Pterodactyl\Models\Allocation; use Pterodactyl\Models\Allocation;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Models\Objects\DeploymentObject; use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository;
use Pterodactyl\Services\Deployment\FindViableNodesService; use Pterodactyl\Services\Deployment\FindViableNodesService;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
@ -29,7 +28,6 @@ class ServerCreationService
private ConnectionInterface $connection, private ConnectionInterface $connection,
private DaemonServerRepository $daemonServerRepository, private DaemonServerRepository $daemonServerRepository,
private FindViableNodesService $findViableNodesService, private FindViableNodesService $findViableNodesService,
private ServerRepository $repository,
private ServerDeletionService $serverDeletionService, private ServerDeletionService $serverDeletionService,
private ServerVariableRepository $serverVariableRepository, private ServerVariableRepository $serverVariableRepository,
private VariableValidatorService $validatorService private VariableValidatorService $validatorService
@ -82,7 +80,7 @@ class ServerCreationService
// //
// If that connection fails out we will attempt to perform a cleanup by just // If that connection fails out we will attempt to perform a cleanup by just
// deleting the server itself from the system. // deleting the server itself from the system.
/** @var \Pterodactyl\Models\Server $server */ /** @var Server $server */
$server = $this->connection->transaction(function () use ($data, $eggVariableData) { $server = $this->connection->transaction(function () use ($data, $eggVariableData) {
// Create the server and assign any additional allocations to it. // Create the server and assign any additional allocations to it.
$server = $this->createModel($data); $server = $this->createModel($data);
@ -136,8 +134,8 @@ class ServerCreationService
{ {
$uuid = $this->generateUniqueUuidCombo(); $uuid = $this->generateUniqueUuidCombo();
/** @var \Pterodactyl\Models\Server $model */ /** @var Server $model */
$model = $this->repository->create([ $model = Server::create([
'external_id' => Arr::get($data, 'external_id'), 'external_id' => Arr::get($data, 'external_id'),
'uuid' => $uuid, 'uuid' => $uuid,
'uuidShort' => substr($uuid, 0, 8), 'uuidShort' => substr($uuid, 0, 8),
@ -206,8 +204,9 @@ class ServerCreationService
private function generateUniqueUuidCombo(): string private function generateUniqueUuidCombo(): string
{ {
$uuid = Uuid::uuid4()->toString(); $uuid = Uuid::uuid4()->toString();
$shortUuid = substr($uuid, 0, 8);
if (!$this->repository->isUniqueUuidCombo($uuid, substr($uuid, 0, 8))) { if (Server::query()->where('uuid', $uuid)->orWhere('uuidShort', $shortUuid)->exists()) {
return $this->generateUniqueUuidCombo(); return $this->generateUniqueUuidCombo();
} }

View file

@ -6,7 +6,6 @@ use Pterodactyl\Models\User;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\Translation\Translator;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
class UserDeletionService class UserDeletionService
{ {
@ -15,7 +14,6 @@ class UserDeletionService
*/ */
public function __construct( public function __construct(
protected UserRepositoryInterface $repository, protected UserRepositoryInterface $repository,
protected ServerRepositoryInterface $serverRepository,
protected Translator $translator protected Translator $translator
) { ) {
} }
@ -23,19 +21,18 @@ class UserDeletionService
/** /**
* Delete a user from the panel only if they have no servers attached to their account. * Delete a user from the panel only if they have no servers attached to their account.
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws DisplayException
*/ */
public function handle(int|User $user): ?bool public function handle(int|User $user): ?bool
{ {
if ($user instanceof User) { if (is_int($user)) {
$user = $user->id; $user = User::query()->findOrFail($user);
} }
$servers = $this->serverRepository->setColumns('id')->findCountWhere([['owner_id', '=', $user]]); if ($user->servers()->count() > 0) {
if ($servers > 0) {
throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers')); throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers'));
} }
return $this->repository->delete($user); return $this->repository->delete($user->id);
} }
} }