diff --git a/app/Contracts/Repository/ServerRepositoryInterface.php b/app/Contracts/Repository/ServerRepositoryInterface.php deleted file mode 100644 index 20e36a17f..000000000 --- a/app/Contracts/Repository/ServerRepositoryInterface.php +++ /dev/null @@ -1,15 +0,0 @@ -repository->update($server->id, [ - 'status' => $server->isInstalled() ? Server::STATUS_INSTALLING : null, - ], true, true); + $server->status = $server->isInstalled() ? Server::STATUS_INSTALLING : null; + $server->save(); $this->alert->success(trans('admin/server.alerts.install_toggled'))->flash(); diff --git a/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php b/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php index 4e3c5f9bb..0fd6e8069 100644 --- a/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php +++ b/app/Http/Controllers/Api/Client/Servers/NetworkAllocationController.php @@ -7,7 +7,6 @@ use Illuminate\Http\JsonResponse; use Pterodactyl\Facades\Activity; use Pterodactyl\Models\Allocation; use Pterodactyl\Exceptions\DisplayException; -use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Transformers\Api\Client\AllocationTransformer; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Services\Allocations\FindAssignableAllocationService; @@ -22,10 +21,8 @@ class NetworkAllocationController extends ClientApiController /** * NetworkAllocationController constructor. */ - public function __construct( - private FindAssignableAllocationService $assignableAllocationService, - private ServerRepository $serverRepository - ) { + public function __construct(private FindAssignableAllocationService $assignableAllocationService) + { parent::__construct(); } @@ -72,7 +69,8 @@ class NetworkAllocationController extends ClientApiController */ 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') ->subject($allocation) diff --git a/app/Http/Controllers/Api/Client/Servers/SettingsController.php b/app/Http/Controllers/Api/Client/Servers/SettingsController.php index 7f1946a21..a1bcf33e7 100644 --- a/app/Http/Controllers/Api/Client/Servers/SettingsController.php +++ b/app/Http/Controllers/Api/Client/Servers/SettingsController.php @@ -6,7 +6,6 @@ use Illuminate\Http\Response; use Pterodactyl\Models\Server; use Illuminate\Http\JsonResponse; use Pterodactyl\Facades\Activity; -use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Services\Servers\ReinstallServerService; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -19,10 +18,8 @@ class SettingsController extends ClientApiController /** * SettingsController constructor. */ - public function __construct( - private ServerRepository $repository, - private ReinstallServerService $reinstallServerService - ) { + public function __construct(private ReinstallServerService $reinstallServerService) + { parent::__construct(); } @@ -34,15 +31,12 @@ class SettingsController extends ClientApiController */ public function rename(RenameServerRequest $request, Server $server): JsonResponse { - $this->repository->update($server->id, [ - 'name' => $request->input('name'), - ]); + $server->name = $request->input('name'); + $server->save(); - if ($server->name !== $request->input('name')) { - Activity::event('server:settings.rename') - ->property(['old' => $server->name, 'new' => $request->input('name')]) - ->log(); - } + Activity::event('server:settings.rename') + ->property(['old' => $server->name, 'new' => $request->input('name')]) + ->log(); return new JsonResponse([], Response::HTTP_NO_CONTENT); } diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php index 8ce88e89e..763b40d05 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerDetailsController.php @@ -9,7 +9,6 @@ use Pterodactyl\Facades\Activity; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Eggs\EggConfigurationService; -use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Http\Resources\Wings\ServerConfigurationCollection; use Pterodactyl\Services\Servers\ServerConfigurationStructureService; @@ -20,7 +19,6 @@ class ServerDetailsController extends Controller */ public function __construct( protected ConnectionInterface $connection, - private ServerRepository $repository, private ServerConfigurationStructureService $configurationStructureService, private EggConfigurationService $eggConfigurationService ) { @@ -30,11 +28,10 @@ class ServerDetailsController extends Controller * 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. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function __invoke(Request $request, string $uuid): JsonResponse { - $server = $this->repository->getByUuid($uuid); + $server = Server::findOrFailByUuid($uuid); return new JsonResponse([ 'settings' => $this->configurationStructureService->handle($server), diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php b/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php index e78843763..71ee06581 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerInstallController.php @@ -2,13 +2,13 @@ namespace Pterodactyl\Http\Controllers\Api\Remote\Servers; -use Carbon\CarbonImmutable; use Illuminate\Http\Request; use Illuminate\Http\Response; use Pterodactyl\Models\Server; use Illuminate\Http\JsonResponse; 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 Illuminate\Contracts\Events\Dispatcher as EventDispatcher; use Pterodactyl\Http\Requests\Api\Remote\InstallationDataRequest; @@ -18,18 +18,16 @@ class ServerInstallController extends Controller /** * ServerInstallController constructor. */ - public function __construct(private ServerRepository $repository, private EventDispatcher $eventDispatcher) + public function __construct(private EventDispatcher $eventDispatcher) { } /** * Returns installation information for a server. - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function index(Request $request, string $uuid): JsonResponse { - $server = $this->repository->getByUuid($uuid); + $server = Server::findOrFailByUuid($uuid); $egg = $server->egg; return new JsonResponse([ @@ -42,23 +40,27 @@ class ServerInstallController extends Controller /** * Updates the installation state of a server. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ 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; if ($server->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. // 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)) { $this->eventDispatcher->dispatch(new ServerInstalled($server)); } elseif (!$isInitialInstall && config()->get('pterodactyl.email.send_reinstall_notification', true)) { diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php b/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php index 6f49d66e0..3f17220b0 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php @@ -8,11 +8,11 @@ use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; use Pterodactyl\Models\Allocation; use Illuminate\Support\Facades\Log; +use Pterodactyl\Models\Server; use Pterodactyl\Models\ServerTransfer; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Services\Nodes\NodeJWTService; -use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Repositories\Wings\DaemonTransferRepository; use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; @@ -24,7 +24,6 @@ class ServerTransferController extends Controller */ public function __construct( private ConnectionInterface $connection, - private ServerRepository $repository, private DaemonServerRepository $daemonServerRepository, private DaemonTransferRepository $daemonTransferRepository, private NodeJWTService $jwtService @@ -39,7 +38,7 @@ class ServerTransferController extends Controller */ 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. if (!$request->input('successful')) { @@ -78,7 +77,7 @@ class ServerTransferController extends Controller */ public function failure(string $uuid): JsonResponse { - $server = $this->repository->getByUuid($uuid); + $server = Server::findOrFailByUuid($uuid); return $this->processFailedTransfer($server->transfer); } @@ -90,7 +89,7 @@ class ServerTransferController extends Controller */ public function success(string $uuid): JsonResponse { - $server = $this->repository->getByUuid($uuid); + $server = Server::findOrFailByUuid($uuid); $transfer = $server->transfer; /** @var \Pterodactyl\Models\Server $server */ diff --git a/app/Http/Controllers/Base/IndexController.php b/app/Http/Controllers/Base/IndexController.php index fecaa91a3..cf028ed1e 100644 --- a/app/Http/Controllers/Base/IndexController.php +++ b/app/Http/Controllers/Base/IndexController.php @@ -5,17 +5,14 @@ namespace Pterodactyl\Http\Controllers\Base; use Illuminate\View\View; use Illuminate\View\Factory as ViewFactory; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class IndexController extends Controller { /** * IndexController constructor. */ - public function __construct( - protected ServerRepositoryInterface $repository, - protected ViewFactory $view - ) { + public function __construct(protected ViewFactory $view) + { } /** diff --git a/app/Models/Server.php b/app/Models/Server.php index 5ad99151a..18a94886a 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -233,9 +233,9 @@ class Server extends Model /** * 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 * 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. */ public function validateTransferState() @@ -378,4 +378,15 @@ class Server extends Model 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; + } } diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..bc8b189e9 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -9,7 +9,6 @@ use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\UserRepository; use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; -use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository; @@ -25,7 +24,6 @@ use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Contracts\Repository\SessionRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; @@ -56,7 +54,6 @@ class RepositoryServiceProvider extends ServiceProvider $this->app->bind(NestRepositoryInterface::class, NestRepository::class); $this->app->bind(NodeRepositoryInterface::class, NodeRepository::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(SessionRepositoryInterface::class, SessionRepository::class); $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); diff --git a/app/Repositories/Eloquent/ServerRepository.php b/app/Repositories/Eloquent/ServerRepository.php deleted file mode 100644 index e87ec875e..000000000 --- a/app/Repositories/Eloquent/ServerRepository.php +++ /dev/null @@ -1,42 +0,0 @@ -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(); - } - } -} diff --git a/app/Services/Eggs/EggDeletionService.php b/app/Services/Eggs/EggDeletionService.php index 7e4013351..c642cfeb0 100644 --- a/app/Services/Eggs/EggDeletionService.php +++ b/app/Services/Eggs/EggDeletionService.php @@ -2,10 +2,10 @@ namespace Pterodactyl\Services\Eggs; +use Pterodactyl\Models\Server; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Exceptions\Service\Egg\HasChildrenException; use Pterodactyl\Exceptions\Service\HasActiveServersException; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class EggDeletionService { @@ -13,7 +13,6 @@ class EggDeletionService * EggDeletionService constructor. */ public function __construct( - protected ServerRepositoryInterface $serverRepository, protected EggRepositoryInterface $repository ) { } @@ -26,8 +25,7 @@ class EggDeletionService */ public function handle(int $egg): int { - $servers = $this->serverRepository->findCountWhere([['egg_id', '=', $egg]]); - if ($servers > 0) { + if (Server::query()->where('egg_id', $egg)->count()) { throw new HasActiveServersException(trans('exceptions.nest.egg.delete_has_servers')); } diff --git a/app/Services/Nests/NestDeletionService.php b/app/Services/Nests/NestDeletionService.php index 6babf45c3..d2f43eb60 100644 --- a/app/Services/Nests/NestDeletionService.php +++ b/app/Services/Nests/NestDeletionService.php @@ -2,9 +2,9 @@ namespace Pterodactyl\Services\Nests; +use Pterodactyl\Models\Server; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Exceptions\Service\HasActiveServersException; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class NestDeletionService { @@ -12,7 +12,6 @@ class NestDeletionService * NestDeletionService constructor. */ public function __construct( - protected ServerRepositoryInterface $serverRepository, protected NestRepositoryInterface $repository ) { } @@ -20,12 +19,11 @@ class NestDeletionService /** * 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 { - $count = $this->serverRepository->findCountWhere([['nest_id', '=', $nest]]); - if ($count > 0) { + if (Server::query()->where('nest_id', $nest)->count() > 0) { throw new HasActiveServersException(trans('exceptions.nest.delete_has_servers')); } diff --git a/app/Services/Nodes/NodeDeletionService.php b/app/Services/Nodes/NodeDeletionService.php index adb9a0628..bfe856bf9 100644 --- a/app/Services/Nodes/NodeDeletionService.php +++ b/app/Services/Nodes/NodeDeletionService.php @@ -6,7 +6,6 @@ use Pterodactyl\Models\Node; use Illuminate\Contracts\Translation\Translator; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Exceptions\Service\HasActiveServersException; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class NodeDeletionService { @@ -15,7 +14,6 @@ class NodeDeletionService */ public function __construct( protected NodeRepositoryInterface $repository, - protected ServerRepositoryInterface $serverRepository, protected Translator $translator ) { } @@ -23,16 +21,15 @@ class NodeDeletionService /** * 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 { - if ($node instanceof Node) { - $node = $node->id; + if (is_int($node)) { + $node = Node::query()->findOrFail($node); } - $servers = $this->serverRepository->setColumns('id')->findCountWhere([['node_id', '=', $node]]); - if ($servers > 0) { + if ($node->servers()->count() > 0) { throw new HasActiveServersException($this->translator->get('exceptions.node.servers_attached')); } diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index b959893e6..1380db7cc 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -12,7 +12,6 @@ use Illuminate\Support\Collection; use Pterodactyl\Models\Allocation; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Models\Objects\DeploymentObject; -use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Services\Deployment\FindViableNodesService; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; @@ -29,7 +28,6 @@ class ServerCreationService private ConnectionInterface $connection, private DaemonServerRepository $daemonServerRepository, private FindViableNodesService $findViableNodesService, - private ServerRepository $repository, private ServerDeletionService $serverDeletionService, private ServerVariableRepository $serverVariableRepository, private VariableValidatorService $validatorService @@ -82,7 +80,7 @@ class ServerCreationService // // If that connection fails out we will attempt to perform a cleanup by just // deleting the server itself from the system. - /** @var \Pterodactyl\Models\Server $server */ + /** @var Server $server */ $server = $this->connection->transaction(function () use ($data, $eggVariableData) { // Create the server and assign any additional allocations to it. $server = $this->createModel($data); @@ -136,8 +134,8 @@ class ServerCreationService { $uuid = $this->generateUniqueUuidCombo(); - /** @var \Pterodactyl\Models\Server $model */ - $model = $this->repository->create([ + /** @var Server $model */ + $model = Server::create([ 'external_id' => Arr::get($data, 'external_id'), 'uuid' => $uuid, 'uuidShort' => substr($uuid, 0, 8), diff --git a/app/Services/Users/UserDeletionService.php b/app/Services/Users/UserDeletionService.php index f7f060cee..257d6783d 100644 --- a/app/Services/Users/UserDeletionService.php +++ b/app/Services/Users/UserDeletionService.php @@ -6,7 +6,6 @@ use Pterodactyl\Models\User; use Pterodactyl\Exceptions\DisplayException; use Illuminate\Contracts\Translation\Translator; use Pterodactyl\Contracts\Repository\UserRepositoryInterface; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; class UserDeletionService { @@ -15,7 +14,6 @@ class UserDeletionService */ public function __construct( protected UserRepositoryInterface $repository, - protected ServerRepositoryInterface $serverRepository, protected Translator $translator ) { } @@ -23,16 +21,15 @@ class UserDeletionService /** * 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 { - if ($user instanceof User) { - $user = $user->id; + if (is_int($user)) { + $user = User::query()->findOrFail($user); } - $servers = $this->serverRepository->setColumns('id')->findCountWhere([['owner_id', '=', $user]]); - if ($servers > 0) { + if ($user->servers()->count() > 0) { throw new DisplayException($this->translator->get('admin/user.exceptions.user_has_servers')); }