2017-10-19 03:32:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Server;
|
|
|
|
|
|
|
|
use Illuminate\View\View;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2018-03-02 03:27:37 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Pterodactyl\Traits\Controllers\JavascriptInjection;
|
|
|
|
use Pterodactyl\Services\Databases\DatabasePasswordService;
|
2018-03-02 03:27:37 +00:00
|
|
|
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
2018-03-02 03:27:37 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
|
|
|
|
use Pterodactyl\Http\Requests\Server\Database\StoreServerDatabaseRequest;
|
2017-10-19 03:32:19 +00:00
|
|
|
|
|
|
|
class DatabaseController extends Controller
|
|
|
|
{
|
|
|
|
use JavascriptInjection;
|
|
|
|
|
2018-03-02 03:27:37 +00:00
|
|
|
/**
|
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag
|
|
|
|
*/
|
|
|
|
private $alert;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DeployServerDatabaseService
|
|
|
|
*/
|
|
|
|
private $deployServerDatabaseService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $databaseHostRepository;
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
|
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
private $passwordService;
|
2017-10-19 03:32:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
private $repository;
|
2017-10-19 03:32:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*
|
2018-03-02 03:27:37 +00:00
|
|
|
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
|
|
|
* @param \Pterodactyl\Services\Databases\DeployServerDatabaseService $deployServerDatabaseService
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $databaseHostRepository
|
|
|
|
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $passwordService
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
2017-10-19 03:32:19 +00:00
|
|
|
*/
|
2018-03-02 03:27:37 +00:00
|
|
|
public function __construct(
|
|
|
|
AlertsMessageBag $alert,
|
|
|
|
DeployServerDatabaseService $deployServerDatabaseService,
|
|
|
|
DatabaseHostRepositoryInterface $databaseHostRepository,
|
|
|
|
DatabasePasswordService $passwordService,
|
|
|
|
DatabaseRepositoryInterface $repository
|
|
|
|
) {
|
|
|
|
$this->alert = $alert;
|
|
|
|
$this->databaseHostRepository = $databaseHostRepository;
|
|
|
|
$this->deployServerDatabaseService = $deployServerDatabaseService;
|
2017-10-19 03:32:19 +00:00
|
|
|
$this->passwordService = $passwordService;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the database listing for a server.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\View\View
|
2017-10-21 02:32:57 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2017-10-19 03:32:19 +00:00
|
|
|
*/
|
|
|
|
public function index(Request $request): View
|
|
|
|
{
|
|
|
|
$server = $request->attributes->get('server');
|
2017-10-21 02:32:57 +00:00
|
|
|
$this->authorize('view-databases', $server);
|
|
|
|
$this->setRequest($request)->injectJavascript();
|
2017-10-19 03:32:19 +00:00
|
|
|
|
2018-03-02 03:27:37 +00:00
|
|
|
$canCreateDatabase = config('pterodactyl.client_features.databases.enabled');
|
|
|
|
$allowRandom = config('pterodactyl.client_features.databases.allow_random');
|
|
|
|
|
|
|
|
if ($this->databaseHostRepository->findCountWhere([['node_id', '=', $server->node_id]]) === 0) {
|
|
|
|
if ($canCreateDatabase && ! $allowRandom) {
|
|
|
|
$canCreateDatabase = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$databases = $this->repository->getDatabasesForServer($server->id);
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
return view('server.databases.index', [
|
2018-03-02 03:27:37 +00:00
|
|
|
'allowCreation' => $canCreateDatabase,
|
|
|
|
'overLimit' => ! is_null($server->database_limit) && count($databases) >= $server->database_limit,
|
|
|
|
'databases' => $databases,
|
2017-10-19 03:32:19 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-03-02 03:27:37 +00:00
|
|
|
/**
|
|
|
|
* Handle a request from a user to create a new database for the server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Server\Database\StoreServerDatabaseRequest $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
|
|
|
|
*/
|
|
|
|
public function store(StoreServerDatabaseRequest $request): RedirectResponse
|
|
|
|
{
|
|
|
|
$this->deployServerDatabaseService->handle($request->getServer(), $request->validated());
|
|
|
|
|
|
|
|
$this->alert->success('Successfully created a new database.')->flash();
|
|
|
|
|
|
|
|
return redirect()->route('server.databases.index', $request->getServer()->uuidShort);
|
|
|
|
}
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
/**
|
|
|
|
* Handle a request to update the password for a specific database.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
2017-10-21 02:32:57 +00:00
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
2017-10-19 03:32:19 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
|
|
|
public function update(Request $request): JsonResponse
|
|
|
|
{
|
2017-10-21 02:32:57 +00:00
|
|
|
$this->authorize('reset-db-password', $request->attributes->get('server'));
|
|
|
|
|
2017-10-19 03:32:19 +00:00
|
|
|
$password = str_random(20);
|
|
|
|
$this->passwordService->handle($request->attributes->get('database'), $password);
|
|
|
|
|
|
|
|
return response()->json(['password' => $password]);
|
|
|
|
}
|
|
|
|
}
|