From 6e4f7f16c12ec2380153a5437d47fd72e7c2ccb3 Mon Sep 17 00:00:00 2001 From: Lance Pioch Date: Sun, 23 Oct 2022 17:27:18 -0400 Subject: [PATCH] Replace egg repository --- .../Repository/EggRepositoryInterface.php | 38 -------- .../Controllers/Admin/Nests/EggController.php | 2 - .../Admin/Nests/EggScriptController.php | 21 +++-- .../Admin/Nests/EggVariableController.php | 5 +- app/Providers/RepositoryServiceProvider.php | 3 - app/Repositories/Eloquent/EggRepository.php | 86 ------------------- app/Services/Eggs/EggCreationService.php | 21 ++--- app/Services/Eggs/EggDeletionService.php | 18 ++-- app/Services/Eggs/EggUpdateService.php | 27 ++---- .../Eggs/Scripts/InstallScriptService.php | 26 +++--- .../Eggs/Sharing/EggExporterService.php | 13 +-- 11 files changed, 54 insertions(+), 206 deletions(-) delete mode 100644 app/Contracts/Repository/EggRepositoryInterface.php delete mode 100644 app/Repositories/Eloquent/EggRepository.php diff --git a/app/Contracts/Repository/EggRepositoryInterface.php b/app/Contracts/Repository/EggRepositoryInterface.php deleted file mode 100644 index 5f2162c4c..000000000 --- a/app/Contracts/Repository/EggRepositoryInterface.php +++ /dev/null @@ -1,38 +0,0 @@ -repository->getWithCopyAttributes($egg); - $copy = $this->repository->findWhere([ - ['copy_script_from', '=', null], - ['nest_id', '=', $egg->nest_id], - ['id', '!=', $egg], - ]); + $egg = Egg::with('scriptFrom', 'configFrom') + ->where('id', $egg) + ->firstOrFail(); - $rely = $this->repository->findWhere([ - ['copy_script_from', '=', $egg->id], - ]); + $copy = Egg::query() + ->whereNull('copy_script_from') + ->where('nest_id', $egg->nest_id) + ->whereNot('id', $egg->id) + ->firstOrFail(); + + $rely = Egg::query()->where('copy_script_from', $egg->id)->firstOrFail(); return $this->view->make('admin.eggs.scripts', [ 'copyFromOptions' => $copy, diff --git a/app/Http/Controllers/Admin/Nests/EggVariableController.php b/app/Http/Controllers/Admin/Nests/EggVariableController.php index 40274b323..80dc56aed 100644 --- a/app/Http/Controllers/Admin/Nests/EggVariableController.php +++ b/app/Http/Controllers/Admin/Nests/EggVariableController.php @@ -9,7 +9,6 @@ use Illuminate\Http\RedirectResponse; use Prologue\Alerts\AlertsMessageBag; use Illuminate\View\Factory as ViewFactory; use Pterodactyl\Http\Controllers\Controller; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Services\Eggs\Variables\VariableUpdateService; use Pterodactyl\Http\Requests\Admin\Egg\EggVariableFormRequest; use Pterodactyl\Services\Eggs\Variables\VariableCreationService; @@ -24,7 +23,6 @@ class EggVariableController extends Controller protected AlertsMessageBag $alert, protected VariableCreationService $creationService, protected VariableUpdateService $updateService, - protected EggRepositoryInterface $repository, protected EggVariableRepositoryInterface $variableRepository, protected ViewFactory $view ) { @@ -33,11 +31,10 @@ class EggVariableController extends Controller /** * Handle request to view the variables attached to an Egg. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function view(int $egg): View { - $egg = $this->repository->getWithVariables($egg); + $egg = Egg::with('variables')->findOrFail($egg); return $this->view->make('admin.eggs.variables', ['egg' => $egg]); } diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..97b22f7af 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -3,7 +3,6 @@ namespace Pterodactyl\Providers; use Illuminate\Support\ServiceProvider; -use Pterodactyl\Repositories\Eloquent\EggRepository; use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository; @@ -17,7 +16,6 @@ use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Repositories\Eloquent\EggVariableRepository; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; @@ -50,7 +48,6 @@ class RepositoryServiceProvider extends ServiceProvider $this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class); - $this->app->bind(EggRepositoryInterface::class, EggRepository::class); $this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class); $this->app->bind(LocationRepositoryInterface::class, LocationRepository::class); $this->app->bind(NestRepositoryInterface::class, NestRepository::class); diff --git a/app/Repositories/Eloquent/EggRepository.php b/app/Repositories/Eloquent/EggRepository.php deleted file mode 100644 index 8c4a01a27..000000000 --- a/app/Repositories/Eloquent/EggRepository.php +++ /dev/null @@ -1,86 +0,0 @@ -getBuilder()->with('variables')->findOrFail($id, $this->getColumns()); - } catch (ModelNotFoundException) { - throw new RecordNotFoundException(); - } - } - - /** - * Return all eggs and their relations to be used in the daemon API. - */ - public function getAllWithCopyAttributes(): Collection - { - return $this->getBuilder()->with('scriptFrom', 'configFrom')->get($this->getColumns()); - } - - /** - * Return an egg with the scriptFrom and configFrom relations loaded onto the model. - * - * @param int|string $value - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getWithCopyAttributes($value, string $column = 'id'): Egg - { - Assert::true(is_digit($value) || is_string($value), 'First argument passed to getWithCopyAttributes must be an integer or string, received %s.'); - - try { - return $this->getBuilder()->with('scriptFrom', 'configFrom')->where($column, '=', $value)->firstOrFail($this->getColumns()); - } catch (ModelNotFoundException) { - throw new RecordNotFoundException(); - } - } - - /** - * Return all the data needed to export a service. - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - */ - public function getWithExportAttributes(int $id): Egg - { - try { - return $this->getBuilder()->with('scriptFrom', 'configFrom', 'variables')->findOrFail($id, $this->getColumns()); - } catch (ModelNotFoundException) { - throw new RecordNotFoundException(); - } - } - - /** - * Confirm a copy script belongs to the same nest as the item trying to use it. - */ - public function isCopyableScript(int $copyFromId, int $service): bool - { - return $this->getBuilder()->whereNull('copy_script_from') - ->where('id', '=', $copyFromId) - ->where('nest_id', '=', $service) - ->exists(); - } -} diff --git a/app/Services/Eggs/EggCreationService.php b/app/Services/Eggs/EggCreationService.php index b084b0cca..60dadfb7b 100644 --- a/app/Services/Eggs/EggCreationService.php +++ b/app/Services/Eggs/EggCreationService.php @@ -4,7 +4,6 @@ namespace Pterodactyl\Services\Eggs; use Ramsey\Uuid\Uuid; use Pterodactyl\Models\Egg; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Illuminate\Contracts\Config\Repository as ConfigRepository; use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException; @@ -14,33 +13,35 @@ class EggCreationService /** * EggCreationService constructor. */ - public function __construct(private ConfigRepository $config, private EggRepositoryInterface $repository) + public function __construct(private ConfigRepository $config) { } /** * Create a new service option and assign it to the given service. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException + * @throws NoParentConfigurationFoundException */ public function handle(array $data): Egg { $data['config_from'] = array_get($data, 'config_from'); if (!is_null($data['config_from'])) { - $results = $this->repository->findCountWhere([ - ['nest_id', '=', array_get($data, 'nest_id')], - ['id', '=', array_get($data, 'config_from')], - ]); + $results = Egg::query() + ->where('nest_id', array_get($data, 'nest_id')) + ->where('id', array_get($data, 'config_from')) + ->count(); if ($results !== 1) { throw new NoParentConfigurationFoundException(trans('exceptions.nest.egg.must_be_child')); } } - return $this->repository->create(array_merge($data, [ + /** @var Egg $egg */ + $egg = Egg::query()->create(array_merge($data, [ 'uuid' => Uuid::uuid4()->toString(), 'author' => $this->config->get('pterodactyl.service.author'), - ]), true, true); + ])); + + return $egg; } } diff --git a/app/Services/Eggs/EggDeletionService.php b/app/Services/Eggs/EggDeletionService.php index 7e4013351..b3bc317f0 100644 --- a/app/Services/Eggs/EggDeletionService.php +++ b/app/Services/Eggs/EggDeletionService.php @@ -2,27 +2,25 @@ namespace Pterodactyl\Services\Eggs; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Exceptions\Service\Egg\HasChildrenException; use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Models\Egg; class EggDeletionService { /** * EggDeletionService constructor. */ - public function __construct( - protected ServerRepositoryInterface $serverRepository, - protected EggRepositoryInterface $repository - ) { + public function __construct(protected ServerRepositoryInterface $serverRepository) + { } /** * Delete an Egg from the database if it has no active servers attached to it. * - * @throws \Pterodactyl\Exceptions\Service\HasActiveServersException - * @throws \Pterodactyl\Exceptions\Service\Egg\HasChildrenException + * @throws HasActiveServersException + * @throws HasChildrenException */ public function handle(int $egg): int { @@ -31,11 +29,13 @@ class EggDeletionService throw new HasActiveServersException(trans('exceptions.nest.egg.delete_has_servers')); } - $children = $this->repository->findCountWhere([['config_from', '=', $egg]]); + $children = Egg::query()->where('config_from', $egg)->count(); if ($children > 0) { throw new HasChildrenException(trans('exceptions.nest.egg.has_children')); } - return $this->repository->delete($egg); + $egg = Egg::query()->findOrFail($egg); + + return $egg->delete(); } } diff --git a/app/Services/Eggs/EggUpdateService.php b/app/Services/Eggs/EggUpdateService.php index 7ca442ba3..3b15d6485 100644 --- a/app/Services/Eggs/EggUpdateService.php +++ b/app/Services/Eggs/EggUpdateService.php @@ -3,42 +3,33 @@ namespace Pterodactyl\Services\Eggs; use Pterodactyl\Models\Egg; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException; class EggUpdateService { - /** - * EggUpdateService constructor. - */ - public function __construct(protected EggRepositoryInterface $repository) - { - } - /** * Update a service option. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - * @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException + * @throws NoParentConfigurationFoundException */ public function handle(Egg $egg, array $data): void { - if (!is_null(array_get($data, 'config_from'))) { - $results = $this->repository->findCountWhere([ - ['nest_id', '=', $egg->nest_id], - ['id', '=', array_get($data, 'config_from')], - ]); + $eggId = array_get($data, 'config_from'); + if (!is_null($eggId)) { + $results = Egg::query() + ->where('nest_id', $egg->nest_id) + ->where('id', $eggId) + ->count(); if ($results !== 1) { throw new NoParentConfigurationFoundException(trans('exceptions.nest.egg.must_be_child')); } } - // TODO(dane): Once the admin UI is done being reworked and this is exposed + // TODO: (Dane) Once the admin UI is done being reworked and this is exposed // in said UI, remove this so that you can actually update the denylist. unset($data['file_denylist']); - $this->repository->withoutFreshModel()->update($egg->id, $data); + $egg->update($data); } } diff --git a/app/Services/Eggs/Scripts/InstallScriptService.php b/app/Services/Eggs/Scripts/InstallScriptService.php index 334157236..7efae278f 100644 --- a/app/Services/Eggs/Scripts/InstallScriptService.php +++ b/app/Services/Eggs/Scripts/InstallScriptService.php @@ -3,34 +3,30 @@ namespace Pterodactyl\Services\Eggs\Scripts; use Pterodactyl\Models\Egg; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException; class InstallScriptService { /** - * InstallScriptService constructor. - */ - public function __construct(protected EggRepositoryInterface $repository) - { - } - - /** - * Modify the install script for a given Egg. + * Modify the installation script for a given Egg. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - * @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException + * @throws InvalidCopyFromException */ public function handle(Egg $egg, array $data): void { - if (!is_null(array_get($data, 'copy_script_from'))) { - if (!$this->repository->isCopyableScript(array_get($data, 'copy_script_from'), $egg->nest_id)) { + $copyFromEggId = array_get($data, 'copy_script_from'); + if (!is_null($copyFromEggId)) { + $isCopyableScript = $egg->nest->eggs() + ->where('id', $copyFromEggId) + ->whereNull('copy_script_from') + ->exists(); + + if (!$isCopyableScript) { throw new InvalidCopyFromException(trans('exceptions.nest.egg.invalid_copy_id')); } } - $this->repository->withoutFreshModel()->update($egg->id, [ + $egg->update([ 'script_install' => array_get($data, 'script_install'), 'script_is_privileged' => array_get($data, 'script_is_privileged', 1), 'script_entry' => array_get($data, 'script_entry'), diff --git a/app/Services/Eggs/Sharing/EggExporterService.php b/app/Services/Eggs/Sharing/EggExporterService.php index 706297b3d..99b8d6b18 100644 --- a/app/Services/Eggs/Sharing/EggExporterService.php +++ b/app/Services/Eggs/Sharing/EggExporterService.php @@ -6,25 +6,18 @@ use Carbon\Carbon; use Pterodactyl\Models\Egg; use Illuminate\Support\Collection; use Pterodactyl\Models\EggVariable; -use Pterodactyl\Contracts\Repository\EggRepositoryInterface; +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; class EggExporterService { - /** - * EggExporterService constructor. - */ - public function __construct(protected EggRepositoryInterface $repository) - { - } - /** * Return a JSON representation of an egg and its variables. * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws RecordNotFoundException */ public function handle(int $egg): string { - $egg = $this->repository->getWithExportAttributes($egg); + $egg = Egg::with(['scriptFrom', 'configFrom', 'variables'])->findOrFail($egg); $struct = [ '_comment' => 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PTERODACTYL PANEL - PTERODACTYL.IO',