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
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
private $alert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService
|
|
|
|
*/
|
|
|
|
private $creationService;
|
|
|
|
|
2018-03-03 23:52:35 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $databaseRepository;
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService
|
|
|
|
*/
|
|
|
|
private $deletionService;
|
2017-06-18 00:48:31 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-09 04:24:55 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
|
2017-06-18 00:48:31 +00:00
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
private $locationRepository;
|
2017-06-18 00:48:31 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-09 04:24:55 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
|
2017-06-18 00:48:31 +00:00
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
private $repository;
|
2017-06-18 00:48:31 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-19 03:32:19 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService
|
2017-06-18 00:48:31 +00:00
|
|
|
*/
|
2017-10-19 03:32:19 +00:00
|
|
|
private $updateService;
|
2017-06-18 00:48:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*
|
2017-08-09 04:24:55 +00:00
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
|
2018-03-03 23:52:35 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
|
2017-10-19 03:32:19 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\Hosts\HostCreationService $creationService
|
|
|
|
* @param \Pterodactyl\Services\Databases\Hosts\HostDeletionService $deletionService
|
|
|
|
* @param \Pterodactyl\Services\Databases\Hosts\HostUpdateService $updateService
|
2017-08-09 04:24:55 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
|
2017-06-18 00:48:31 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
2017-08-09 04:24:55 +00:00
|
|
|
DatabaseHostRepositoryInterface $repository,
|
2018-03-03 23:52:35 +00:00
|
|
|
DatabaseRepositoryInterface $databaseRepository,
|
2017-10-19 03:32:19 +00:00
|
|
|
HostCreationService $creationService,
|
|
|
|
HostDeletionService $deletionService,
|
|
|
|
HostUpdateService $updateService,
|
2017-08-09 04:24:55 +00:00
|
|
|
LocationRepositoryInterface $locationRepository
|
2017-06-18 00:48:31 +00:00
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->creationService = $creationService;
|
2018-03-03 23:52:35 +00:00
|
|
|
$this->databaseRepository = $databaseRepository;
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->deletionService = $deletionService;
|
2017-08-09 04:24:55 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
$this->locationRepository = $locationRepository;
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->updateService = $updateService;
|
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.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
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
|
|
|
{
|
|
|
|
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-22 03:10:48 +00:00
|
|
|
* @param int $host
|
2017-03-16 23:35:29 +00:00
|
|
|
* @return \Illuminate\View\View
|
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
|
|
|
{
|
2017-03-16 23:35:29 +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
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2017-06-18 00:48:31 +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-11-11 17:01:38 +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
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
|
2018-02-18 20:10:12 +00:00
|
|
|
* @param \Pterodactyl\Models\DatabaseHost $host
|
2017-03-19 23:36:50 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2017-06-18 00:48:31 +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();
|
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
|
|
|
* @param int $host
|
2017-06-18 00:48:31 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*
|
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
|
|
|
}
|