Replace egg repository
This commit is contained in:
parent
860b2d890b
commit
6e4f7f16c1
11 changed files with 54 additions and 206 deletions
|
@ -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;
|
||||
}
|
|
@ -13,7 +13,6 @@ use Pterodactyl\Services\Eggs\EggUpdateService;
|
|||
use Pterodactyl\Services\Eggs\EggCreationService;
|
||||
use Pterodactyl\Services\Eggs\EggDeletionService;
|
||||
use Pterodactyl\Http\Requests\Admin\Egg\EggFormRequest;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
|
||||
|
||||
class EggController extends Controller
|
||||
|
@ -25,7 +24,6 @@ class EggController extends Controller
|
|||
protected AlertsMessageBag $alert,
|
||||
protected EggCreationService $creationService,
|
||||
protected EggDeletionService $deletionService,
|
||||
protected EggRepositoryInterface $repository,
|
||||
protected EggUpdateService $updateService,
|
||||
protected NestRepositoryInterface $nestRepository,
|
||||
protected ViewFactory $view
|
||||
|
|
|
@ -9,7 +9,6 @@ use Prologue\Alerts\AlertsMessageBag;
|
|||
use Illuminate\View\Factory as ViewFactory;
|
||||
use Pterodactyl\Http\Controllers\Controller;
|
||||
use Pterodactyl\Services\Eggs\Scripts\InstallScriptService;
|
||||
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
|
||||
use Pterodactyl\Http\Requests\Admin\Egg\EggScriptFormRequest;
|
||||
|
||||
class EggScriptController extends Controller
|
||||
|
@ -19,7 +18,6 @@ class EggScriptController extends Controller
|
|||
*/
|
||||
public function __construct(
|
||||
protected AlertsMessageBag $alert,
|
||||
protected EggRepositoryInterface $repository,
|
||||
protected InstallScriptService $installScriptService,
|
||||
protected ViewFactory $view
|
||||
) {
|
||||
|
@ -30,16 +28,17 @@ class EggScriptController extends Controller
|
|||
*/
|
||||
public function index(int $egg): View
|
||||
{
|
||||
$egg = $this->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,
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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'),
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in a new issue