Compare commits

...

1 commit

Author SHA1 Message Date
Lance Pioch
6e4f7f16c1 Replace egg repository 2022-10-23 17:27:18 -04:00
11 changed files with 54 additions and 206 deletions

View file

@ -1,38 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Pterodactyl\Models\Egg;
use Illuminate\Database\Eloquent\Collection;
interface EggRepositoryInterface extends RepositoryInterface
{
/**
* Return an egg with the variables relation attached.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithVariables(int $id): Egg;
/**
* Return all eggs and their relations to be used in the daemon API.
*/
public function getAllWithCopyAttributes(): Collection;
/**
* Return an egg with the scriptFrom and configFrom relations loaded onto the model.
*/
public function getWithCopyAttributes(int|string $value, string $column = 'id'): Egg;
/**
* Return all the data needed to export a service.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithExportAttributes(int $id): Egg;
/**
* Confirm a copy script belongs to the same nest as the item trying to use it.
*/
public function isCopyableScript(int $copyFromId, int $service): bool;
}

View file

@ -13,7 +13,6 @@ use Pterodactyl\Services\Eggs\EggUpdateService;
use Pterodactyl\Services\Eggs\EggCreationService; use Pterodactyl\Services\Eggs\EggCreationService;
use Pterodactyl\Services\Eggs\EggDeletionService; use Pterodactyl\Services\Eggs\EggDeletionService;
use Pterodactyl\Http\Requests\Admin\Egg\EggFormRequest; use Pterodactyl\Http\Requests\Admin\Egg\EggFormRequest;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
class EggController extends Controller class EggController extends Controller
@ -25,7 +24,6 @@ class EggController extends Controller
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected EggCreationService $creationService, protected EggCreationService $creationService,
protected EggDeletionService $deletionService, protected EggDeletionService $deletionService,
protected EggRepositoryInterface $repository,
protected EggUpdateService $updateService, protected EggUpdateService $updateService,
protected NestRepositoryInterface $nestRepository, protected NestRepositoryInterface $nestRepository,
protected ViewFactory $view protected ViewFactory $view

View file

@ -9,7 +9,6 @@ use Prologue\Alerts\AlertsMessageBag;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService; use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest; use Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest;
class EggScriptController extends Controller class EggScriptController extends Controller
@ -19,7 +18,6 @@ class EggScriptController extends Controller
*/ */
public function __construct( public function __construct(
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected EggRepositoryInterface $repository,
protected InstallScriptService $installScriptService, protected InstallScriptService $installScriptService,
protected ViewFactory $view protected ViewFactory $view
) { ) {
@ -30,16 +28,17 @@ class EggScriptController extends Controller
*/ */
public function index(int $egg): View public function index(int $egg): View
{ {
$egg = $this->repository->getWithCopyAttributes($egg); $egg = Egg::with('scriptFrom', 'configFrom')
$copy = $this->repository->findWhere([ ->where('id', $egg)
['copy_script_from', '=', null], ->firstOrFail();
['nest_id', '=', $egg->nest_id],
['id', '!=', $egg],
]);
$rely = $this->repository->findWhere([ $copy = Egg::query()
['copy_script_from', '=', $egg->id], ->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', [ return $this->view->make('admin.eggs.scripts', [
'copyFromOptions' => $copy, 'copyFromOptions' => $copy,

View file

@ -9,7 +9,6 @@ use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
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\EggRepositoryInterface;
use Pterodactyl\Services\Eggs\Variables\VariableUpdateService; use Pterodactyl\Services\Eggs\Variables\VariableUpdateService;
use Pterodactyl\Http\Requests\Admin\Egg\EggVariableFormRequest; use Pterodactyl\Http\Requests\Admin\Egg\EggVariableFormRequest;
use Pterodactyl\Services\Eggs\Variables\VariableCreationService; use Pterodactyl\Services\Eggs\Variables\VariableCreationService;
@ -24,7 +23,6 @@ class EggVariableController extends Controller
protected AlertsMessageBag $alert, protected AlertsMessageBag $alert,
protected VariableCreationService $creationService, protected VariableCreationService $creationService,
protected VariableUpdateService $updateService, protected VariableUpdateService $updateService,
protected EggRepositoryInterface $repository,
protected EggVariableRepositoryInterface $variableRepository, protected EggVariableRepositoryInterface $variableRepository,
protected ViewFactory $view protected ViewFactory $view
) { ) {
@ -33,11 +31,10 @@ class EggVariableController extends Controller
/** /**
* Handle request to view the variables attached to an Egg. * Handle request to view the variables attached to an Egg.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function view(int $egg): View 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]); return $this->view->make('admin.eggs.variables', ['egg' => $egg]);
} }

View file

@ -3,7 +3,6 @@
namespace Pterodactyl\Providers; namespace Pterodactyl\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Pterodactyl\Repositories\Eloquent\EggRepository;
use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository;
use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository;
@ -17,7 +16,6 @@ use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\SettingsRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\EggVariableRepository; use Pterodactyl\Repositories\Eloquent\EggVariableRepository;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
@ -50,7 +48,6 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class); $this->app->bind(ApiKeyRepositoryInterface::class, ApiKeyRepository::class);
$this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class); $this->app->bind(DatabaseRepositoryInterface::class, DatabaseRepository::class);
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::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(EggVariableRepositoryInterface::class, EggVariableRepository::class);
$this->app->bind(LocationRepositoryInterface::class, LocationRepository::class); $this->app->bind(LocationRepositoryInterface::class, LocationRepository::class);
$this->app->bind(NestRepositoryInterface::class, NestRepository::class); $this->app->bind(NestRepositoryInterface::class, NestRepository::class);

View file

@ -1,86 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Egg;
use Webmozart\Assert\Assert;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
class EggRepository extends EloquentRepository implements EggRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Egg::class;
}
/**
* Return an egg with the variables relation attached.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getWithVariables(int $id): Egg
{
try {
return $this->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();
}
}

View file

@ -4,7 +4,6 @@ namespace Pterodactyl\Services\Eggs;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException; use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException;
@ -14,33 +13,35 @@ class EggCreationService
/** /**
* EggCreationService constructor. * 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. * Create a new service option and assign it to the given service.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws NoParentConfigurationFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException
*/ */
public function handle(array $data): Egg public function handle(array $data): Egg
{ {
$data['config_from'] = array_get($data, 'config_from'); $data['config_from'] = array_get($data, 'config_from');
if (!is_null($data['config_from'])) { if (!is_null($data['config_from'])) {
$results = $this->repository->findCountWhere([ $results = Egg::query()
['nest_id', '=', array_get($data, 'nest_id')], ->where('nest_id', array_get($data, 'nest_id'))
['id', '=', array_get($data, 'config_from')], ->where('id', array_get($data, 'config_from'))
]); ->count();
if ($results !== 1) { if ($results !== 1) {
throw new NoParentConfigurationFoundException(trans('exceptions.nest.egg.must_be_child')); 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(), 'uuid' => Uuid::uuid4()->toString(),
'author' => $this->config->get('pterodactyl.service.author'), 'author' => $this->config->get('pterodactyl.service.author'),
]), true, true); ]));
return $egg;
} }
} }

View file

@ -2,27 +2,25 @@
namespace Pterodactyl\Services\Eggs; namespace Pterodactyl\Services\Eggs;
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; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Models\Egg;
class EggDeletionService class EggDeletionService
{ {
/** /**
* EggDeletionService constructor. * EggDeletionService constructor.
*/ */
public function __construct( public function __construct(protected ServerRepositoryInterface $serverRepository)
protected ServerRepositoryInterface $serverRepository, {
protected EggRepositoryInterface $repository
) {
} }
/** /**
* Delete an Egg from the database if it has no active servers attached to it. * Delete an Egg from the database if it has no active servers attached to it.
* *
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException * @throws HasActiveServersException
* @throws \Pterodactyl\Exceptions\Service\Egg\HasChildrenException * @throws HasChildrenException
*/ */
public function handle(int $egg): int public function handle(int $egg): int
{ {
@ -31,11 +29,13 @@ class EggDeletionService
throw new HasActiveServersException(trans('exceptions.nest.egg.delete_has_servers')); 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) { if ($children > 0) {
throw new HasChildrenException(trans('exceptions.nest.egg.has_children')); throw new HasChildrenException(trans('exceptions.nest.egg.has_children'));
} }
return $this->repository->delete($egg); $egg = Egg::query()->findOrFail($egg);
return $egg->delete();
} }
} }

View file

@ -3,42 +3,33 @@
namespace Pterodactyl\Services\Eggs; namespace Pterodactyl\Services\Eggs;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException; use Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException;
class EggUpdateService class EggUpdateService
{ {
/**
* EggUpdateService constructor.
*/
public function __construct(protected EggRepositoryInterface $repository)
{
}
/** /**
* Update a service option. * Update a service option.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws NoParentConfigurationFoundException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\NoParentConfigurationFoundException
*/ */
public function handle(Egg $egg, array $data): void public function handle(Egg $egg, array $data): void
{ {
if (!is_null(array_get($data, 'config_from'))) { $eggId = array_get($data, 'config_from');
$results = $this->repository->findCountWhere([ if (!is_null($eggId)) {
['nest_id', '=', $egg->nest_id], $results = Egg::query()
['id', '=', array_get($data, 'config_from')], ->where('nest_id', $egg->nest_id)
]); ->where('id', $eggId)
->count();
if ($results !== 1) { if ($results !== 1) {
throw new NoParentConfigurationFoundException(trans('exceptions.nest.egg.must_be_child')); 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. // in said UI, remove this so that you can actually update the denylist.
unset($data['file_denylist']); unset($data['file_denylist']);
$this->repository->withoutFreshModel()->update($egg->id, $data); $egg->update($data);
} }
} }

View file

@ -3,34 +3,30 @@
namespace Pterodactyl\Services\Eggs\Scripts; namespace Pterodactyl\Services\Eggs\Scripts;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException; use Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException;
class InstallScriptService class InstallScriptService
{ {
/** /**
* InstallScriptService constructor. * Modify the installation script for a given Egg.
*/
public function __construct(protected EggRepositoryInterface $repository)
{
}
/**
* Modify the install script for a given Egg.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws InvalidCopyFromException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Service\Egg\InvalidCopyFromException
*/ */
public function handle(Egg $egg, array $data): void public function handle(Egg $egg, array $data): void
{ {
if (!is_null(array_get($data, 'copy_script_from'))) { $copyFromEggId = array_get($data, 'copy_script_from');
if (!$this->repository->isCopyableScript(array_get($data, 'copy_script_from'), $egg->nest_id)) { 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')); 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_install' => array_get($data, 'script_install'),
'script_is_privileged' => array_get($data, 'script_is_privileged', 1), 'script_is_privileged' => array_get($data, 'script_is_privileged', 1),
'script_entry' => array_get($data, 'script_entry'), 'script_entry' => array_get($data, 'script_entry'),

View file

@ -6,25 +6,18 @@ use Carbon\Carbon;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Pterodactyl\Models\EggVariable; use Pterodactyl\Models\EggVariable;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
class EggExporterService class EggExporterService
{ {
/**
* EggExporterService constructor.
*/
public function __construct(protected EggRepositoryInterface $repository)
{
}
/** /**
* Return a JSON representation of an egg and its variables. * Return a JSON representation of an egg and its variables.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws RecordNotFoundException
*/ */
public function handle(int $egg): string public function handle(int $egg): string
{ {
$egg = $this->repository->getWithExportAttributes($egg); $egg = Egg::with(['scriptFrom', 'configFrom', 'variables'])->findOrFail($egg);
$struct = [ $struct = [
'_comment' => 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PTERODACTYL PANEL - PTERODACTYL.IO', '_comment' => 'DO NOT EDIT: FILE GENERATED AUTOMATICALLY BY PTERODACTYL PANEL - PTERODACTYL.IO',