Replace DatabaseHost Repository

This commit is contained in:
Lance Pioch 2022-10-23 02:14:54 -04:00
parent 860b2d890b
commit 9001755e10
10 changed files with 29 additions and 74 deletions

View file

@ -1,14 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
use Illuminate\Support\Collection;
interface DatabaseHostRepositoryInterface extends RepositoryInterface
{
/**
* Return database hosts with a count of databases and the node
* information for which it is attached.
*/
public function getWithViewDetails(): Collection;
}

View file

@ -5,7 +5,6 @@ namespace Pterodactyl\Extensions;
use Pterodactyl\Models\DatabaseHost; use Pterodactyl\Models\DatabaseHost;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DynamicDatabaseConnection class DynamicDatabaseConnection
{ {
@ -18,20 +17,18 @@ class DynamicDatabaseConnection
*/ */
public function __construct( public function __construct(
protected ConfigRepository $config, protected ConfigRepository $config,
protected Encrypter $encrypter, protected Encrypter $encrypter
protected DatabaseHostRepositoryInterface $repository
) { ) {
} }
/** /**
* Adds a dynamic database connection entry to the runtime config. * Adds a dynamic database connection entry to the runtime config.
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void public function set(string $connection, DatabaseHost|int $host, string $database = 'mysql'): void
{ {
if (!$host instanceof DatabaseHost) { if (!$host instanceof DatabaseHost) {
$host = $this->repository->find($host); $host = DatabaseHost::query()->findOrFail($host);
} }
$this->config->set('database.connections.' . $connection, [ $this->config->set('database.connections.' . $connection, [

View file

@ -12,11 +12,11 @@ use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Databases\Hosts\HostUpdateService; use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest; use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Services\Databases\Hosts\HostCreationService; use Pterodactyl\Services\Databases\Hosts\HostCreationService;
use Pterodactyl\Services\Databases\Hosts\HostDeletionService; use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseController extends Controller class DatabaseController extends Controller
{ {
@ -25,7 +25,6 @@ class DatabaseController extends Controller
*/ */
public function __construct( public function __construct(
private AlertsMessageBag $alert, private AlertsMessageBag $alert,
private DatabaseHostRepositoryInterface $repository,
private DatabaseRepositoryInterface $databaseRepository, private DatabaseRepositoryInterface $databaseRepository,
private HostCreationService $creationService, private HostCreationService $creationService,
private HostDeletionService $deletionService, private HostDeletionService $deletionService,
@ -40,22 +39,25 @@ class DatabaseController extends Controller
*/ */
public function index(): View public function index(): View
{ {
$hosts = DatabaseHost::query()
->withCount('databases')
->with('node')
->get();
return $this->view->make('admin.databases.index', [ return $this->view->make('admin.databases.index', [
'locations' => $this->locationRepository->getAllWithNodes(), 'locations' => $this->locationRepository->getAllWithNodes(),
'hosts' => $this->repository->getWithViewDetails(), 'hosts' => $hosts,
]); ]);
} }
/** /**
* Display database host to user. * Display database host to user.
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function view(int $host): View public function view(int $host): View
{ {
return $this->view->make('admin.databases.view', [ return $this->view->make('admin.databases.view', [
'locations' => $this->locationRepository->getAllWithNodes(), 'locations' => $this->locationRepository->getAllWithNodes(),
'host' => $this->repository->find($host), 'host' => DatabaseHost::query()->findOrFail($host),
'databases' => $this->databaseRepository->getDatabasesForHost($host), 'databases' => $this->databaseRepository->getDatabasesForHost($host),
]); ]);
} }
@ -118,7 +120,7 @@ class DatabaseController extends Controller
/** /**
* Handle request to delete a database host. * Handle request to delete a database host.
* *
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException * @throws HasActiveServersException
*/ */
public function delete(int $host): RedirectResponse public function delete(int $host): RedirectResponse
{ {

View file

@ -7,6 +7,7 @@ use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\Nest; use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Servers\EnvironmentService; use Pterodactyl\Services\Servers\EnvironmentService;
@ -17,7 +18,6 @@ use Pterodactyl\Repositories\Eloquent\MountRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository; 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;
class ServerViewController extends Controller class ServerViewController extends Controller
{ {
@ -27,7 +27,6 @@ class ServerViewController extends Controller
* ServerViewController constructor. * ServerViewController constructor.
*/ */
public function __construct( public function __construct(
private DatabaseHostRepository $databaseHostRepository,
private LocationRepository $locationRepository, private LocationRepository $locationRepository,
private MountRepository $mountRepository, private MountRepository $mountRepository,
private NestRepository $nestRepository, private NestRepository $nestRepository,
@ -97,7 +96,7 @@ class ServerViewController extends Controller
public function database(Request $request, Server $server): View public function database(Request $request, Server $server): View
{ {
return $this->view->make('admin.servers.view.database', [ return $this->view->make('admin.servers.view.database', [
'hosts' => $this->databaseHostRepository->all(), 'hosts' => DatabaseHost::all(),
'server' => $server, 'server' => $server,
]); ]);
} }

View file

@ -25,7 +25,6 @@ use Pterodactyl\Services\Databases\DatabasePasswordService;
use Pterodactyl\Services\Servers\DetailsModificationService; use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Services\Servers\StartupModificationService; use Pterodactyl\Services\Servers\StartupModificationService;
use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
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\ServerRepositoryInterface;
@ -48,7 +47,6 @@ class ServersController extends Controller
protected DatabaseManagementService $databaseManagementService, protected DatabaseManagementService $databaseManagementService,
protected DatabasePasswordService $databasePasswordService, protected DatabasePasswordService $databasePasswordService,
protected DatabaseRepositoryInterface $databaseRepository, protected DatabaseRepositoryInterface $databaseRepository,
protected DatabaseHostRepository $databaseHostRepository,
protected ServerDeletionService $deletionService, protected ServerDeletionService $deletionService,
protected DetailsModificationService $detailsModificationService, protected DetailsModificationService $detailsModificationService,
protected ReinstallServerService $reinstallService, protected ReinstallServerService $reinstallService,

View file

@ -23,7 +23,6 @@ use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\TaskRepositoryInterface; use Pterodactyl\Contracts\Repository\TaskRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface; use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\ApiKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\ServerVariableRepository; use Pterodactyl\Repositories\Eloquent\ServerVariableRepository;
@ -35,7 +34,6 @@ use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
class RepositoryServiceProvider extends ServiceProvider class RepositoryServiceProvider extends ServiceProvider
@ -49,7 +47,6 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class); $this->app->bind(AllocationRepositoryInterface::class, AllocationRepository::class);
$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(EggRepositoryInterface::class, EggRepository::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);

View file

@ -1,27 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Illuminate\Support\Collection;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseHostRepository extends EloquentRepository implements DatabaseHostRepositoryInterface
{
/**
* Return the model backing this repository.
*/
public function model(): string
{
return DatabaseHost::class;
}
/**
* Return database hosts with a count of databases and the node
* information for which it is attached.
*/
public function getWithViewDetails(): Collection
{
return $this->getBuilder()->withCount('databases')->with('node')->get();
}
}

View file

@ -7,7 +7,6 @@ use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class HostCreationService class HostCreationService
{ {
@ -18,8 +17,7 @@ class HostCreationService
private ConnectionInterface $connection, private ConnectionInterface $connection,
private DatabaseManager $databaseManager, private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic, private DynamicDatabaseConnection $dynamic,
private Encrypter $encrypter, private Encrypter $encrypter
private DatabaseHostRepositoryInterface $repository
) { ) {
} }
@ -31,7 +29,8 @@ class HostCreationService
public function handle(array $data): DatabaseHost public function handle(array $data): DatabaseHost
{ {
return $this->connection->transaction(function () use ($data) { return $this->connection->transaction(function () use ($data) {
$host = $this->repository->create([ /** @var DatabaseHost $host */
$host = DatabaseHost::query()->create([
'password' => $this->encrypter->encrypt(array_get($data, 'password')), 'password' => $this->encrypter->encrypt(array_get($data, 'password')),
'name' => array_get($data, 'name'), 'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'), 'host' => array_get($data, 'host'),

View file

@ -2,9 +2,9 @@
namespace Pterodactyl\Services\Databases\Hosts; namespace Pterodactyl\Services\Databases\Hosts;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Exceptions\Service\HasActiveServersException; use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class HostDeletionService class HostDeletionService
{ {
@ -12,8 +12,7 @@ class HostDeletionService
* HostDeletionService constructor. * HostDeletionService constructor.
*/ */
public function __construct( public function __construct(
private DatabaseRepositoryInterface $databaseRepository, private DatabaseRepositoryInterface $databaseRepository
private DatabaseHostRepositoryInterface $repository
) { ) {
} }
@ -21,7 +20,7 @@ class HostDeletionService
* Delete a specified host from the Panel if no databases are * Delete a specified host from the Panel if no databases are
* attached to it. * attached to it.
* *
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException * @throws HasActiveServersException
*/ */
public function handle(int $host): int public function handle(int $host): int
{ {
@ -30,6 +29,11 @@ class HostDeletionService
throw new HasActiveServersException(trans('exceptions.databases.delete_has_databases')); throw new HasActiveServersException(trans('exceptions.databases.delete_has_databases'));
} }
return $this->repository->delete($host); $host = DatabaseHost::query()->find($host);
if ($host) {
return $host->delete();
}
return true;
} }
} }

View file

@ -7,7 +7,6 @@ use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class HostUpdateService class HostUpdateService
{ {
@ -18,8 +17,7 @@ class HostUpdateService
private ConnectionInterface $connection, private ConnectionInterface $connection,
private DatabaseManager $databaseManager, private DatabaseManager $databaseManager,
private DynamicDatabaseConnection $dynamic, private DynamicDatabaseConnection $dynamic,
private Encrypter $encrypter, private Encrypter $encrypter
private DatabaseHostRepositoryInterface $repository
) { ) {
} }
@ -37,7 +35,9 @@ class HostUpdateService
} }
return $this->connection->transaction(function () use ($data, $hostId) { return $this->connection->transaction(function () use ($data, $hostId) {
$host = $this->repository->update($hostId, $data); /** @var DatabaseHost $host */
$host = DatabaseHost::query()->findOrFail($hostId);
$host->update($data);
$this->dynamic->set('dynamic', $host); $this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual'); $this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');