2016-02-13 05:18:32 +00:00
|
|
|
<?php
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-02-13 05:18:32 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Admin;
|
|
|
|
|
2019-08-03 19:33:28 +00:00
|
|
|
use Exception;
|
2017-10-19 03:32:19 +00:00
|
|
|
use PDOException;
|
|
|
|
use Illuminate\View\View;
|
2018-02-18 20:10:12 +00:00
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2017-06-18 00:48:31 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2016-02-13 05:18:32 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
|
2017-06-18 00:48:31 +00:00
|
|
|
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
|
|
|
|
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
|
2018-03-03 23:52:35 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
2017-08-09 04:24:55 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
2016-02-13 05:18:32 +00:00
|
|
|
|
|
|
|
class DatabaseController extends Controller
|
|
|
|
{
|
2017-06-18 00:48:31 +00:00
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private AlertsMessageBag $alert,
|
|
|
|
private DatabaseHostRepositoryInterface $repository,
|
|
|
|
private DatabaseRepositoryInterface $databaseRepository,
|
|
|
|
private HostCreationService $creationService,
|
|
|
|
private HostDeletionService $deletionService,
|
|
|
|
private HostUpdateService $updateService,
|
2022-11-28 16:56:03 +00:00
|
|
|
private LocationRepositoryInterface $locationRepository
|
2017-06-18 00:48:31 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:18:32 +00:00
|
|
|
/**
|
2017-03-16 23:35:29 +00:00
|
|
|
* Display database host index.
|
2016-02-13 05:18:32 +00:00
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
public function index(): View
|
2016-02-13 05:18:32 +00:00
|
|
|
{
|
2022-11-28 16:56:03 +00:00
|
|
|
return view('admin.databases.index', [
|
2017-08-09 04:24:55 +00:00
|
|
|
'locations' => $this->locationRepository->getAllWithNodes(),
|
|
|
|
'hosts' => $this->repository->getWithViewDetails(),
|
2016-02-13 05:18:32 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
/**
|
|
|
|
* Display database host to user.
|
|
|
|
*
|
2017-08-16 04:16:00 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-03-16 23:35:29 +00:00
|
|
|
*/
|
2018-02-04 18:59:14 +00:00
|
|
|
public function view(int $host): View
|
2016-02-15 02:43:20 +00:00
|
|
|
{
|
2022-11-28 16:56:03 +00:00
|
|
|
return view('admin.databases.view', [
|
2017-08-09 04:24:55 +00:00
|
|
|
'locations' => $this->locationRepository->getAllWithNodes(),
|
2018-03-03 23:52:35 +00:00
|
|
|
'host' => $this->repository->find($host),
|
|
|
|
'databases' => $this->databaseRepository->getDatabasesForHost($host),
|
2016-02-15 02:43:20 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
/**
|
2017-06-18 00:48:31 +00:00
|
|
|
* Handle request to create a new database host.
|
2017-03-16 23:35:29 +00:00
|
|
|
*
|
2019-08-03 19:33:28 +00:00
|
|
|
* @throws \Throwable
|
2017-03-16 23:35:29 +00:00
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
public function create(DatabaseHostFormRequest $request): RedirectResponse
|
2016-02-15 02:43:20 +00:00
|
|
|
{
|
|
|
|
try {
|
2017-10-19 03:32:19 +00:00
|
|
|
$host = $this->creationService->handle($request->normalize());
|
2019-08-03 19:33:28 +00:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
if ($exception instanceof PDOException || $exception->getPrevious() instanceof PDOException) {
|
|
|
|
$this->alert->danger(
|
|
|
|
sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())
|
|
|
|
)->flash();
|
|
|
|
|
2019-08-10 20:19:52 +00:00
|
|
|
return redirect()->route('admin.databases')->withInput($request->validated());
|
2019-08-03 19:33:28 +00:00
|
|
|
} else {
|
|
|
|
throw $exception;
|
|
|
|
}
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
2017-03-16 23:35:29 +00:00
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->alert->success('Successfully created a new database host on the system.')->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.databases.view', $host->id);
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 23:35:29 +00:00
|
|
|
/**
|
2017-06-18 00:48:31 +00:00
|
|
|
* Handle updating database host.
|
2017-03-16 23:35:29 +00:00
|
|
|
*
|
2019-08-03 19:33:28 +00:00
|
|
|
* @throws \Throwable
|
2017-03-16 23:35:29 +00:00
|
|
|
*/
|
2018-02-18 20:10:12 +00:00
|
|
|
public function update(DatabaseHostFormRequest $request, DatabaseHost $host): RedirectResponse
|
2016-02-15 02:43:20 +00:00
|
|
|
{
|
2018-02-18 20:10:12 +00:00
|
|
|
$redirect = redirect()->route('admin.databases.view', $host->id);
|
|
|
|
|
2016-02-15 02:43:20 +00:00
|
|
|
try {
|
2018-02-18 20:10:12 +00:00
|
|
|
$this->updateService->handle($host->id, $request->normalize());
|
2017-06-18 00:48:31 +00:00
|
|
|
$this->alert->success('Database host was updated successfully.')->flash();
|
2019-08-03 19:33:28 +00:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
// Catch any SQL related exceptions and display them back to the user, otherwise just
|
|
|
|
// throw the exception like normal and move on with it.
|
|
|
|
if ($exception instanceof PDOException || $exception->getPrevious() instanceof PDOException) {
|
|
|
|
$this->alert->danger(
|
|
|
|
sprintf('There was an error while trying to connect to the host or while executing a query: "%s"', $exception->getMessage())
|
|
|
|
)->flash();
|
2020-04-11 20:07:40 +00:00
|
|
|
|
2019-11-11 17:01:38 +00:00
|
|
|
return $redirect->withInput($request->normalize());
|
2019-08-03 19:33:28 +00:00
|
|
|
} else {
|
|
|
|
throw $exception;
|
|
|
|
}
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
2017-03-16 23:35:29 +00:00
|
|
|
|
2018-02-18 20:10:12 +00:00
|
|
|
return $redirect;
|
2017-06-18 00:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle request to delete a database host.
|
|
|
|
*
|
2017-10-19 03:32:19 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
|
2017-06-18 00:48:31 +00:00
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
public function delete(int $host): RedirectResponse
|
2017-06-18 00:48:31 +00:00
|
|
|
{
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->deletionService->handle($host);
|
2017-06-18 00:48:31 +00:00
|
|
|
$this->alert->success('The requested database host has been deleted from the system.')->flash();
|
|
|
|
|
|
|
|
return redirect()->route('admin.databases');
|
2016-02-15 02:43:20 +00:00
|
|
|
}
|
2016-02-13 05:18:32 +00:00
|
|
|
}
|