Add database management back to front-end and begin some refactoring

Here we go again boys...
This commit is contained in:
Dane Everitt 2017-10-18 22:32:19 -05:00
parent 2b80de03df
commit 97dc0519d6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
32 changed files with 774 additions and 407 deletions

View file

@ -9,8 +9,35 @@
namespace Pterodactyl\Contracts\Repository; namespace Pterodactyl\Contracts\Repository;
use Illuminate\Support\Collection;
interface DatabaseRepositoryInterface extends RepositoryInterface interface DatabaseRepositoryInterface extends RepositoryInterface
{ {
const DEFAULT_CONNECTION_NAME = 'dynamic';
/**
* Set the connection name to execute statements against.
*
* @param string $connection
* @return $this
*/
public function setConnection(string $connection);
/**
* Return the connection to execute statements aganist.
*
* @return string
*/
public function getConnection(): string;
/**
* Return all of the databases belonging to a server.
*
* @param int $server
* @return \Illuminate\Support\Collection
*/
public function getDatabasesForServer(int $server): Collection;
/** /**
* Create a new database if it does not already exist on the host with * Create a new database if it does not already exist on the host with
* the provided details. * the provided details.
@ -27,10 +54,9 @@ interface DatabaseRepositoryInterface extends RepositoryInterface
* Create a new database on a given connection. * Create a new database on a given connection.
* *
* @param string $database * @param string $database
* @param null|string $connection
* @return bool * @return bool
*/ */
public function createDatabase($database, $connection = null); public function createDatabase($database);
/** /**
* Create a new database user on a given connection. * Create a new database user on a given connection.
@ -38,10 +64,9 @@ interface DatabaseRepositoryInterface extends RepositoryInterface
* @param string $username * @param string $username
* @param string $remote * @param string $remote
* @param string $password * @param string $password
* @param null|string $connection
* @return bool * @return bool
*/ */
public function createUser($username, $remote, $password, $connection = null); public function createUser($username, $remote, $password);
/** /**
* Give a specific user access to a given database. * Give a specific user access to a given database.
@ -49,35 +74,31 @@ interface DatabaseRepositoryInterface extends RepositoryInterface
* @param string $database * @param string $database
* @param string $username * @param string $username
* @param string $remote * @param string $remote
* @param null|string $connection
* @return bool * @return bool
*/ */
public function assignUserToDatabase($database, $username, $remote, $connection = null); public function assignUserToDatabase($database, $username, $remote);
/** /**
* Flush the privileges for a given connection. * Flush the privileges for a given connection.
* *
* @param null|string $connection
* @return mixed * @return mixed
*/ */
public function flush($connection = null); public function flush();
/** /**
* Drop a given database on a specific connection. * Drop a given database on a specific connection.
* *
* @param string $database * @param string $database
* @param null|string $connection
* @return bool * @return bool
*/ */
public function dropDatabase($database, $connection = null); public function dropDatabase($database);
/** /**
* Drop a given user on a specific connection. * Drop a given user on a specific connection.
* *
* @param string $username * @param string $username
* @param string $remote * @param string $remote
* @param null|string $connection
* @return mixed * @return mixed
*/ */
public function dropUser($username, $remote, $connection = null); public function dropUser($username, $remote);
} }

View file

@ -9,11 +9,15 @@
namespace Pterodactyl\Http\Controllers\Admin; namespace Pterodactyl\Http\Controllers\Admin;
use Pterodactyl\Models\DatabaseHost; use PDOException;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Services\Database\DatabaseHostService; use Pterodactyl\Services\Databases\Hosts\HostUpdateService;
use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest; use Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest;
use Pterodactyl\Services\Databases\Hosts\HostCreationService;
use Pterodactyl\Services\Databases\Hosts\HostDeletionService;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -22,41 +26,57 @@ class DatabaseController extends Controller
/** /**
* @var \Prologue\Alerts\AlertsMessageBag * @var \Prologue\Alerts\AlertsMessageBag
*/ */
protected $alert; private $alert;
/**
* @var \Pterodactyl\Services\Databases\Hosts\HostCreationService
*/
private $creationService;
/**
* @var \Pterodactyl\Services\Databases\Hosts\HostDeletionService
*/
private $deletionService;
/** /**
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface * @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
*/ */
protected $locationRepository; private $locationRepository;
/** /**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface * @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/ */
protected $repository; private $repository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseHostService * @var \Pterodactyl\Services\Databases\Hosts\HostUpdateService
*/ */
protected $service; private $updateService;
/** /**
* DatabaseController constructor. * DatabaseController constructor.
* *
* @param \Prologue\Alerts\AlertsMessageBag $alert * @param \Prologue\Alerts\AlertsMessageBag $alert
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Pterodactyl\Services\Database\DatabaseHostService $service * @param \Pterodactyl\Services\Databases\Hosts\HostCreationService $creationService
* @param \Pterodactyl\Services\Databases\Hosts\HostDeletionService $deletionService
* @param \Pterodactyl\Services\Databases\Hosts\HostUpdateService $updateService
* @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository * @param \Pterodactyl\Contracts\Repository\LocationRepositoryInterface $locationRepository
*/ */
public function __construct( public function __construct(
AlertsMessageBag $alert, AlertsMessageBag $alert,
DatabaseHostRepositoryInterface $repository, DatabaseHostRepositoryInterface $repository,
DatabaseHostService $service, HostCreationService $creationService,
HostDeletionService $deletionService,
HostUpdateService $updateService,
LocationRepositoryInterface $locationRepository LocationRepositoryInterface $locationRepository
) { ) {
$this->alert = $alert; $this->alert = $alert;
$this->creationService = $creationService;
$this->deletionService = $deletionService;
$this->repository = $repository; $this->repository = $repository;
$this->service = $service;
$this->locationRepository = $locationRepository; $this->locationRepository = $locationRepository;
$this->updateService = $updateService;
} }
/** /**
@ -64,7 +84,7 @@ class DatabaseController extends Controller
* *
* @return \Illuminate\View\View * @return \Illuminate\View\View
*/ */
public function index() public function index(): View
{ {
return view('admin.databases.index', [ return view('admin.databases.index', [
'locations' => $this->locationRepository->getAllWithNodes(), 'locations' => $this->locationRepository->getAllWithNodes(),
@ -80,7 +100,7 @@ class DatabaseController extends Controller
* *
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function view($host) public function view($host): View
{ {
return view('admin.databases.view', [ return view('admin.databases.view', [
'locations' => $this->locationRepository->getAllWithNodes(), 'locations' => $this->locationRepository->getAllWithNodes(),
@ -94,42 +114,41 @@ class DatabaseController extends Controller
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request * @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* *
* @throws \Throwable * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function create(DatabaseHostFormRequest $request) public function create(DatabaseHostFormRequest $request): RedirectResponse
{ {
try { try {
$host = $this->service->create($request->normalize()); $host = $this->creationService->handle($request->normalize());
} catch (PDOException $ex) {
$this->alert->danger($ex->getMessage())->flash();
return redirect()->route('admin.databases');
}
$this->alert->success('Successfully created a new database host on the system.')->flash(); $this->alert->success('Successfully created a new database host on the system.')->flash();
return redirect()->route('admin.databases.view', $host->id); return redirect()->route('admin.databases.view', $host->id);
} catch (\PDOException $ex) {
$this->alert->danger($ex->getMessage())->flash();
}
return redirect()->route('admin.databases');
} }
/** /**
* Handle updating database host. * Handle updating database host.
* *
* @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request * @param \Pterodactyl\Http\Requests\Admin\DatabaseHostFormRequest $request
* @param \Pterodactyl\Models\DatabaseHost $host * @param int $host
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function update(DatabaseHostFormRequest $request, DatabaseHost $host) public function update(DatabaseHostFormRequest $request, int $host): RedirectResponse
{ {
if ($request->input('action') === 'delete') {
return $this->delete($host);
}
try { try {
$host = $this->service->update($host->id, $request->normalize()); $host = $this->updateService->handle($host, $request->normalize());
$this->alert->success('Database host was updated successfully.')->flash(); $this->alert->success('Database host was updated successfully.')->flash();
} catch (\PDOException $ex) { } catch (PDOException $ex) {
$this->alert->danger($ex->getMessage())->flash(); $this->alert->danger($ex->getMessage())->flash();
} }
@ -139,14 +158,14 @@ class DatabaseController extends Controller
/** /**
* Handle request to delete a database host. * Handle request to delete a database host.
* *
* @param \Pterodactyl\Models\DatabaseHost $host * @param int $host
* @return \Illuminate\Http\RedirectResponse * @return \Illuminate\Http\RedirectResponse
* *
* @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
*/ */
public function delete(DatabaseHost $host) public function delete(int $host): RedirectResponse
{ {
$this->service->delete($host->id); $this->deletionService->handle($host);
$this->alert->success('The requested database host has been deleted from the system.')->flash(); $this->alert->success('The requested database host has been deleted from the system.')->flash();
return redirect()->route('admin.databases'); return redirect()->route('admin.databases');

View file

@ -22,12 +22,13 @@ use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Servers\ReinstallServerService; use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Services\Servers\ContainerRebuildService; use Pterodactyl\Services\Servers\ContainerRebuildService;
use Pterodactyl\Services\Servers\BuildModificationService; use Pterodactyl\Services\Servers\BuildModificationService;
use Pterodactyl\Services\Database\DatabaseManagementService; 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\Contracts\Repository\NodeRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository; use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
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;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@ -67,10 +68,15 @@ class ServersController extends Controller
protected $databaseRepository; protected $databaseRepository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseManagementService * @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/ */
protected $databaseManagementService; protected $databaseManagementService;
/**
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
*/
protected $databasePasswordService;
/** /**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface * @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/ */
@ -135,7 +141,8 @@ class ServersController extends Controller
* @param \Illuminate\Contracts\Config\Repository $config * @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService * @param \Pterodactyl\Services\Servers\ContainerRebuildService $containerRebuildService
* @param \Pterodactyl\Services\Servers\ServerCreationService $service * @param \Pterodactyl\Services\Servers\ServerCreationService $service
* @param \Pterodactyl\Services\Database\DatabaseManagementService $databaseManagementService * @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
* @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository * @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository
* @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService * @param \Pterodactyl\Services\Servers\ServerDeletionService $deletionService
@ -156,6 +163,7 @@ class ServersController extends Controller
ContainerRebuildService $containerRebuildService, ContainerRebuildService $containerRebuildService,
ServerCreationService $service, ServerCreationService $service,
DatabaseManagementService $databaseManagementService, DatabaseManagementService $databaseManagementService,
DatabasePasswordService $databasePasswordService,
DatabaseRepositoryInterface $databaseRepository, DatabaseRepositoryInterface $databaseRepository,
DatabaseHostRepository $databaseHostRepository, DatabaseHostRepository $databaseHostRepository,
ServerDeletionService $deletionService, ServerDeletionService $deletionService,
@ -173,9 +181,10 @@ class ServersController extends Controller
$this->buildModificationService = $buildModificationService; $this->buildModificationService = $buildModificationService;
$this->config = $config; $this->config = $config;
$this->containerRebuildService = $containerRebuildService; $this->containerRebuildService = $containerRebuildService;
$this->databaseManagementService = $databaseManagementService;
$this->databaseRepository = $databaseRepository;
$this->databaseHostRepository = $databaseHostRepository; $this->databaseHostRepository = $databaseHostRepository;
$this->databaseManagementService = $databaseManagementService;
$this->databasePasswordService = $databasePasswordService;
$this->databaseRepository = $databaseRepository;
$this->detailsModificationService = $detailsModificationService; $this->detailsModificationService = $detailsModificationService;
$this->deletionService = $deletionService; $this->deletionService = $deletionService;
$this->locationRepository = $locationRepository; $this->locationRepository = $locationRepository;
@ -609,7 +618,7 @@ class ServersController extends Controller
['id', '=', $request->input('database')], ['id', '=', $request->input('database')],
]); ]);
$this->databaseManagementService->changePassword($database->id, str_random(20)); $this->databasePasswordService->handle($database, str_random(20));
return response('', 204); return response('', 204);
} }

View file

@ -0,0 +1,71 @@
<?php
namespace Pterodactyl\Http\Controllers\Server;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Services\Databases\DatabasePasswordService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabaseController extends Controller
{
use JavascriptInjection;
/**
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
*/
protected $passwordService;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
protected $repository;
/**
* DatabaseController constructor.
*
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $passwordService
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
*/
public function __construct(DatabasePasswordService $passwordService, DatabaseRepositoryInterface $repository)
{
$this->passwordService = $passwordService;
$this->repository = $repository;
}
/**
* Render the database listing for a server.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function index(Request $request): View
{
$server = $request->attributes->get('server');
$this->injectJavascript();
return view('server.databases.index', [
'databases' => $this->repository->getDatabasesForServer($server->id),
]);
}
/**
* Handle a request to update the password for a specific database.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function update(Request $request): JsonResponse
{
$password = str_random(20);
$this->passwordService->handle($request->attributes->get('database'), $password);
return response()->json(['password' => $password]);
}
}

View file

@ -5,6 +5,9 @@ namespace Pterodactyl\Http;
use Pterodactyl\Http\Middleware\DaemonAuthenticate; use Pterodactyl\Http\Middleware\DaemonAuthenticate;
use Illuminate\Foundation\Http\Kernel as HttpKernel; use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Middleware\SubstituteBindings; use Illuminate\Routing\Middleware\SubstituteBindings;
use Pterodactyl\Http\Middleware\Server\SubuserBelongsToServer;
use Pterodactyl\Http\Middleware\Server\DatabaseBelongsToServer;
use Pterodactyl\Http\Middleware\Server\ScheduleBelongsToServer;
class Kernel extends HttpKernel class Kernel extends HttpKernel
{ {
@ -63,7 +66,6 @@ class Kernel extends HttpKernel
'guest' => \Pterodactyl\Http\Middleware\RedirectIfAuthenticated::class, 'guest' => \Pterodactyl\Http\Middleware\RedirectIfAuthenticated::class,
'server' => \Pterodactyl\Http\Middleware\ServerAuthenticate::class, 'server' => \Pterodactyl\Http\Middleware\ServerAuthenticate::class,
'subuser.auth' => \Pterodactyl\Http\Middleware\SubuserAccessAuthenticate::class, 'subuser.auth' => \Pterodactyl\Http\Middleware\SubuserAccessAuthenticate::class,
'subuser' => \Pterodactyl\Http\Middleware\Server\SubuserAccess::class,
'admin' => \Pterodactyl\Http\Middleware\AdminAuthenticate::class, 'admin' => \Pterodactyl\Http\Middleware\AdminAuthenticate::class,
'daemon-old' => DaemonAuthenticate::class, 'daemon-old' => DaemonAuthenticate::class,
'csrf' => \Pterodactyl\Http\Middleware\VerifyCsrfToken::class, 'csrf' => \Pterodactyl\Http\Middleware\VerifyCsrfToken::class,
@ -71,6 +73,13 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'recaptcha' => \Pterodactyl\Http\Middleware\VerifyReCaptcha::class, 'recaptcha' => \Pterodactyl\Http\Middleware\VerifyReCaptcha::class,
'schedule' => \Pterodactyl\Http\Middleware\Server\ScheduleAccess::class,
// Server specific middleware (used for authenticating access to resources)
//
// These are only used for individual server authentication, and not gloabl
// actions from other resources. They are defined in the route files.
'server..database' => DatabaseBelongsToServer::class,
'server..subuser' => SubuserBelongsToServer::class,
'server..schedule' => ScheduleBelongsToServer::class,
]; ];
} }

View file

@ -0,0 +1,51 @@
<?php
namespace Pterodactyl\Http\Middleware\Server;
use Closure;
use Illuminate\Http\Request;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class DatabaseBelongsToServer
{
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
protected $repository;
/**
* DatabaseAccess constructor.
*
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
*/
public function __construct(DatabaseRepositoryInterface $repository)
{
$this->repository = $repository;
}
/**
* Check if a database being requested belongs to the currently loaded server.
* If it does not, throw a 404 error, otherwise continue on with the request
* and set an attribute with the database.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle(Request $request, Closure $next)
{
$server = $request->attributes->get('server');
$database = $this->repository->find($request->input('database'));
if ($database->server_id !== $server->id) {
throw new NotFoundHttpException;
}
$request->attributes->set('database', $database);
return $next($request);
}
}

View file

@ -14,7 +14,7 @@ use Illuminate\Contracts\Session\Session;
use Pterodactyl\Contracts\Extensions\HashidsInterface; use Pterodactyl\Contracts\Extensions\HashidsInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
class ScheduleAccess class ScheduleBelongsToServer
{ {
/** /**
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface * @var \Pterodactyl\Contracts\Extensions\HashidsInterface

View file

@ -15,7 +15,7 @@ use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SubuserAccess class SubuserBelongsToServer
{ {
/** /**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface

View file

@ -105,8 +105,13 @@ class ServerAuthenticate
} }
// Store the server in the session. // Store the server in the session.
// @todo remove from session. use request attributes.
$this->session->now('server_data.model', $server); $this->session->now('server_data.model', $server);
// Add server to the request attributes. This will replace sessions
// as files are updated.
$request->attributes->set('server', $server);
return $next($request); return $next($request);
} }
} }

View file

@ -60,6 +60,7 @@ class SubuserAccessAuthenticate
try { try {
$token = $this->keyProviderService->handle($server->id, $request->user()->id); $token = $this->keyProviderService->handle($server->id, $request->user()->id);
$this->session->now('server_data.token', $token); $this->session->now('server_data.token', $token);
$request->attributes->set('server_token', $token);
} catch (RecordNotFoundException $exception) { } catch (RecordNotFoundException $exception) {
throw new AuthenticationException('This account does not have permission to access this server.'); throw new AuthenticationException('This account does not have permission to access this server.');
} }

View file

@ -1,32 +1,25 @@
<?php <?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Http\ViewComposers\Server; namespace Pterodactyl\Http\ViewComposers\Server;
use Illuminate\View\View; use Illuminate\View\View;
use Illuminate\Contracts\Session\Session; use Illuminate\Http\Request;
class ServerDataComposer class ServerDataComposer
{ {
/** /**
* @var \Illuminate\Contracts\Session\Session * @var \Illuminate\Http\Request
*/ */
protected $session; protected $request;
/** /**
* ServerDataComposer constructor. * ServerDataComposer constructor.
* *
* @param \Illuminate\Contracts\Session\Session $session * @param \Illuminate\Http\Request $request
*/ */
public function __construct(Session $session) public function __construct(Request $request)
{ {
$this->session = $session; $this->request = $request;
} }
/** /**
@ -36,10 +29,10 @@ class ServerDataComposer
*/ */
public function compose(View $view) public function compose(View $view)
{ {
$data = $this->session->get('server_data'); $server = $this->request->get('server');
$view->with('server', array_get($data, 'model')); $view->with('server', $server);
$view->with('node', object_get($data['model'], 'node')); $view->with('node', object_get($server, 'node'));
$view->with('daemon_token', array_get($data, 'token')); $view->with('daemon_token', $this->request->get('server_token'));
} }
} }

View file

@ -10,6 +10,7 @@
namespace Pterodactyl\Repositories\Eloquent; namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Database; use Pterodactyl\Models\Database;
use Illuminate\Support\Collection;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Illuminate\Database\DatabaseManager; use Illuminate\Database\DatabaseManager;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
@ -17,6 +18,11 @@ use Pterodactyl\Exceptions\Repository\DuplicateDatabaseNameException;
class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface class DatabaseRepository extends EloquentRepository implements DatabaseRepositoryInterface
{ {
/**
* @var string
*/
protected $connection = self::DEFAULT_CONNECTION_NAME;
/** /**
* @var \Illuminate\Database\DatabaseManager * @var \Illuminate\Database\DatabaseManager
*/ */
@ -45,6 +51,40 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
return Database::class; return Database::class;
} }
/**
* Set the connection name to execute statements against.
*
* @param string $connection
* @return $this
*/
public function setConnection(string $connection)
{
$this->connection = $connection;
return $this;
}
/**
* Return the connection to execute statements aganist.
*
* @return string
*/
public function getConnection(): string
{
return $this->connection;
}
/**
* Return all of the databases belonging to a server.
*
* @param int $server
* @return \Illuminate\Support\Collection
*/
public function getDatabasesForServer(int $server): Collection
{
return $this->getBuilder()->where('server_id', $server)->get($this->getColumns());
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* @return bool|\Illuminate\Database\Eloquent\Model * @return bool|\Illuminate\Database\Eloquent\Model
@ -67,80 +107,64 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createDatabase($database, $connection = null) public function createDatabase($database)
{ {
return $this->runStatement( return $this->runStatement(sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database));
sprintf('CREATE DATABASE IF NOT EXISTS `%s`', $database),
$connection
);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function createUser($username, $remote, $password, $connection = null) public function createUser($username, $remote, $password)
{ {
return $this->runStatement( return $this->runStatement(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password),
$connection
);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function assignUserToDatabase($database, $username, $remote, $connection = null) public function assignUserToDatabase($database, $username, $remote)
{ {
return $this->runStatement( return $this->runStatement(sprintf(
sprintf(
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, EXECUTE ON `%s`.* TO `%s`@`%s`', 'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX, EXECUTE ON `%s`.* TO `%s`@`%s`',
$database, $database,
$username, $username,
$remote $remote
), ));
$connection
);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function flush($connection = null) public function flush()
{ {
return $this->runStatement('FLUSH PRIVILEGES', $connection); return $this->runStatement('FLUSH PRIVILEGES');
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function dropDatabase($database, $connection = null) public function dropDatabase($database)
{ {
return $this->runStatement( return $this->runStatement(sprintf('DROP DATABASE IF EXISTS `%s`', $database));
sprintf('DROP DATABASE IF EXISTS `%s`', $database),
$connection
);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function dropUser($username, $remote, $connection = null) public function dropUser($username, $remote)
{ {
return $this->runStatement( return $this->runStatement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote));
sprintf('DROP USER IF EXISTS `%s`@`%s`', $username, $remote),
$connection
);
} }
/** /**
* Run the provided statement against the database on a given connection. * Run the provided statement against the database on a given connection.
* *
* @param string $statement * @param string $statement
* @param null|string $connection
* @return bool * @return bool
*/ */
protected function runStatement($statement, $connection = null) protected function runStatement($statement)
{ {
return $this->database->connection($connection)->statement($statement); return $this->database->connection($this->getConnection())->statement($statement);
} }
} }

View file

@ -1,148 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Database;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class DatabaseHostService
{
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
protected $databaseRepository;
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
protected $dynamic;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
protected $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
protected $repository;
/**
* DatabaseHostService constructor.
*
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
DatabaseManager $database,
DatabaseRepositoryInterface $databaseRepository,
DatabaseHostRepositoryInterface $repository,
DynamicDatabaseConnection $dynamic,
Encrypter $encrypter
) {
$this->database = $database;
$this->databaseRepository = $databaseRepository;
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
$this->repository = $repository;
}
/**
* Create a new database host and persist it to the database.
*
* @param array $data
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Throwable
* @throws \PDOException
*/
public function create(array $data)
{
$this->database->beginTransaction();
$host = $this->repository->create([
'password' => $this->encrypter->encrypt(array_get($data, 'password')),
'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'),
'port' => array_get($data, 'port'),
'username' => array_get($data, 'username'),
'max_databases' => null,
'node_id' => array_get($data, 'node_id'),
]);
// Check Access
$this->dynamic->set('dynamic', $host);
$this->database->connection('dynamic')->select('SELECT 1 FROM dual');
$this->database->commit();
return $host;
}
/**
* Update a database host and persist to the database.
*
* @param int $id
* @param array $data
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function update($id, array $data)
{
$this->database->beginTransaction();
if (! empty(array_get($data, 'password'))) {
$data['password'] = $this->encrypter->encrypt($data['password']);
} else {
unset($data['password']);
}
$host = $this->repository->update($id, $data);
$this->dynamic->set('dynamic', $host);
$this->database->connection('dynamic')->select('SELECT 1 FROM dual');
$this->database->commit();
return $host;
}
/**
* Delete a database host if it has no active databases attached to it.
*
* @param int $id
* @return bool|null
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function delete($id)
{
$count = $this->databaseRepository->findCountWhere([['database_host_id', '=', $id]]);
if ($count > 0) {
throw new DisplayException(trans('exceptions.databases.delete_has_databases'));
}
return $this->repository->delete($id);
}
}

View file

@ -7,7 +7,7 @@
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
*/ */
namespace Pterodactyl\Services\Database; namespace Pterodactyl\Services\Databases;
use Illuminate\Database\DatabaseManager; use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
@ -79,28 +79,26 @@ class DatabaseManagementService
$database = $this->repository->createIfNotExists($data); $database = $this->repository->createIfNotExists($data);
$this->dynamic->set('dynamic', $data['database_host_id']); $this->dynamic->set('dynamic', $data['database_host_id']);
$this->repository->createDatabase($database->database, 'dynamic'); $this->repository->createDatabase($database->database);
$this->repository->createUser( $this->repository->createUser(
$database->username, $database->username,
$database->remote, $database->remote,
$this->encrypter->decrypt($database->password), $this->encrypter->decrypt($database->password)
'dynamic'
); );
$this->repository->assignUserToDatabase( $this->repository->assignUserToDatabase(
$database->database, $database->database,
$database->username, $database->username,
$database->remote, $database->remote
'dynamic'
); );
$this->repository->flush('dynamic'); $this->repository->flush();
$this->database->commit(); $this->database->commit();
} catch (\Exception $ex) { } catch (\Exception $ex) {
try { try {
if (isset($database)) { if (isset($database)) {
$this->repository->dropDatabase($database->database, 'dynamic'); $this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote, 'dynamic'); $this->repository->dropUser($database->username, $database->remote);
$this->repository->flush('dynamic'); $this->repository->flush();
} }
} catch (\Exception $exTwo) { } catch (\Exception $exTwo) {
// ignore an exception // ignore an exception
@ -113,62 +111,22 @@ class DatabaseManagementService
return $database; return $database;
} }
/**
* Change the password for a specific user and database combination.
*
* @param int $id
* @param string $password
* @return bool
*
* @throws \Exception
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function changePassword($id, $password)
{
$database = $this->repository->find($id);
$this->dynamic->set('dynamic', $database->database_host_id);
$this->database->beginTransaction();
try {
$updated = $this->repository->withoutFresh()->update($id, [
'password' => $this->encrypter->encrypt($password),
]);
$this->repository->dropUser($database->username, $database->remote, 'dynamic');
$this->repository->createUser($database->username, $database->remote, $password, 'dynamic');
$this->repository->assignUserToDatabase(
$database->database,
$database->username,
$database->remote,
'dynamic'
);
$this->repository->flush('dynamic');
$this->database->commit();
} catch (\Exception $ex) {
$this->database->rollBack();
throw $ex;
}
return $updated;
}
/** /**
* Delete a database from the given host server. * Delete a database from the given host server.
* *
* @param int $id * @param int $id
* @return bool|null * @return bool|null
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/ */
public function delete($id) public function delete($id)
{ {
$database = $this->repository->find($id); $database = $this->repository->find($id);
$this->dynamic->set('dynamic', $database->database_host_id); $this->dynamic->set('dynamic', $database->database_host_id);
$this->repository->dropDatabase($database->database, 'dynamic'); $this->repository->dropDatabase($database->database);
$this->repository->dropUser($database->username, $database->remote, 'dynamic'); $this->repository->dropUser($database->username, $database->remote);
$this->repository->flush('dynamic'); $this->repository->flush();
return $this->repository->delete($id); return $this->repository->delete($id);
} }

View file

@ -0,0 +1,86 @@
<?php
namespace Pterodactyl\Services\Databases;
use Pterodactyl\Models\Database;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabasePasswordService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
private $dynamic;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
private $repository;
/**
* DatabasePasswordService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
ConnectionInterface $connection,
DatabaseRepositoryInterface $repository,
DynamicDatabaseConnection $dynamic,
Encrypter $encrypter
) {
$this->connection = $connection;
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
$this->repository = $repository;
}
/**
* Updates a password for a given database.
*
* @param \Pterodactyl\Models\Database|int $database
* @param string $password
* @return bool
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle($database, string $password): bool
{
if (! $database instanceof Database) {
$database = $this->repository->find($database);
}
$this->dynamic->set('dynamic', $database->database_host_id);
$this->connection->beginTransaction();
$updated = $this->repository->withoutFresh()->update($database->id, [
'password' => $this->encrypter->encrypt($password),
]);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->createUser($database->username, $database->remote, $password);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
unset($password);
$this->connection->commit();
return $updated;
}
}

View file

@ -0,0 +1,92 @@
<?php
namespace Pterodactyl\Services\Databases\Hosts;
use Pterodactyl\Models\DatabaseHost;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class HostCreationService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Illuminate\Database\DatabaseManager
*/
private $databaseManager;
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
private $dynamic;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
private $repository;
/**
* HostCreationService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Database\DatabaseManager $databaseManager
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
ConnectionInterface $connection,
DatabaseManager $databaseManager,
DatabaseHostRepositoryInterface $repository,
DynamicDatabaseConnection $dynamic,
Encrypter $encrypter
) {
$this->connection = $connection;
$this->databaseManager = $databaseManager;
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
$this->repository = $repository;
}
/**
* Create a new database host on the Panel.
*
* @param array $data
* @return \Pterodactyl\Models\DatabaseHost
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle(array $data): DatabaseHost
{
$this->connection->beginTransaction();
$host = $this->repository->create([
'password' => $this->encrypter->encrypt(array_get($data, 'password')),
'name' => array_get($data, 'name'),
'host' => array_get($data, 'host'),
'port' => array_get($data, 'port'),
'username' => array_get($data, 'username'),
'max_databases' => null,
'node_id' => array_get($data, 'node_id'),
]);
// Confirm access using the provided credentials before saving data.
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
$this->connection->commit();
return $host;
}
}

View file

@ -0,0 +1,53 @@
<?php
namespace Pterodactyl\Services\Databases\Hosts;
use Pterodactyl\Exceptions\Service\HasActiveServersException;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class HostDeletionService
{
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
*/
private $databaseRepository;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
private $repository;
/**
* HostDeletionService constructor.
*
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
*/
public function __construct(
DatabaseRepositoryInterface $databaseRepository,
DatabaseHostRepositoryInterface $repository
) {
$this->databaseRepository = $databaseRepository;
$this->repository = $repository;
}
/**
* Delete a specified host from the Panel if no databases are
* attached to it.
*
* @param int $host
* @return int
*
* @throws \Pterodactyl\Exceptions\Service\HasActiveServersException
*/
public function handle(int $host): int
{
$count = $this->databaseRepository->findCountWhere([['database_host_id', '=', $host]]);
if ($count > 0) {
throw new HasActiveServersException(trans('exceptions.databases.delete_has_databases'));
}
return $this->repository->delete($host);
}
}

View file

@ -0,0 +1,96 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Databases\Hosts;
use Pterodactyl\Models\DatabaseHost;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
class HostUpdateService
{
/**
* @var \Illuminate\Database\ConnectionInterface
*/
private $connection;
/**
* @var \Illuminate\Database\DatabaseManager
*/
private $databaseManager;
/**
* @var \Pterodactyl\Extensions\DynamicDatabaseConnection
*/
private $dynamic;
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
/**
* @var \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface
*/
private $repository;
/**
* DatabaseHostService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param \Illuminate\Database\DatabaseManager $databaseManager
* @param \Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface $repository
* @param \Pterodactyl\Extensions\DynamicDatabaseConnection $dynamic
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
*/
public function __construct(
ConnectionInterface $connection,
DatabaseManager $databaseManager,
DatabaseHostRepositoryInterface $repository,
DynamicDatabaseConnection $dynamic,
Encrypter $encrypter
) {
$this->connection = $connection;
$this->databaseManager = $databaseManager;
$this->dynamic = $dynamic;
$this->encrypter = $encrypter;
$this->repository = $repository;
}
/**
* Update a database host and persist to the database.
*
* @param int $hostId
* @param array $data
* @return mixed
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function handle(int $hostId, array $data): DatabaseHost
{
if (! empty(array_get($data, 'password'))) {
$data['password'] = $this->encrypter->encrypt($data['password']);
} else {
unset($data['password']);
}
$this->connection->beginTransaction();
$host = $this->repository->update($hostId, $data);
$this->dynamic->set('dynamic', $host);
$this->databaseManager->connection('dynamic')->select('SELECT 1 FROM dual');
$this->connection->commit();
return $host;
}
}

View file

@ -14,7 +14,7 @@ use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Database\DatabaseManagementService; use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -32,7 +32,7 @@ class ServerDeletionService
protected $daemonServerRepository; protected $daemonServerRepository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseManagementService * @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/ */
protected $databaseManagementService; protected $databaseManagementService;
@ -62,7 +62,7 @@ class ServerDeletionService
* @param \Illuminate\Database\ConnectionInterface $connection * @param \Illuminate\Database\ConnectionInterface $connection
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository * @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $databaseRepository
* @param \Pterodactyl\Services\Database\DatabaseManagementService $databaseManagementService * @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Illuminate\Log\Writer $writer * @param \Illuminate\Log\Writer $writer
*/ */

View file

@ -10,25 +10,23 @@
namespace Pterodactyl\Traits\Controllers; namespace Pterodactyl\Traits\Controllers;
use Javascript; use Javascript;
use Illuminate\Http\Request;
trait JavascriptInjection trait JavascriptInjection
{ {
/**
* @var \Illuminate\Contracts\Session\Session
*/
protected $session;
/** /**
* Injects server javascript into the page to be used by other services. * Injects server javascript into the page to be used by other services.
* *
* @param array $args * @param array $args
* @param bool $overwrite * @param bool $overwrite
* @return mixed * @param \Illuminate\Http\Request|null $request
* @return array
*/ */
public function injectJavascript($args = [], $overwrite = false) public function injectJavascript($args = [], $overwrite = false, Request $request = null)
{ {
$server = $this->session->get('server_data.model'); $request = $request ?? app()->make(Request::class);
$token = $this->session->get('server_data.token'); $server = $request->attributes->get('server');
$token = $request->attributes->get('server_token');
$response = array_merge([ $response = array_merge([
'server' => [ 'server' => [

File diff suppressed because one or more lines are too long

View file

@ -302,7 +302,7 @@ return [
'database' => [ 'database' => [
'header' => 'Databases', 'header' => 'Databases',
'header_sub' => 'All databases available for this server.', 'header_sub' => 'All databases available for this server.',
'your_dbs' => 'Your Databases', 'your_dbs' => 'Configured Databases',
'host' => 'MySQL Host', 'host' => 'MySQL Host',
'reset_password' => 'Reset Password', 'reset_password' => 'Reset Password',
'no_dbs' => 'There are no databases listed for this server.', 'no_dbs' => 'There are no databases listed for this server.',

View file

@ -79,9 +79,8 @@
</div> </div>
<div class="box-footer"> <div class="box-footer">
{!! csrf_field() !!} {!! csrf_field() !!}
{!! method_field('PATCH') !!} <button name="_method" value="DELETE" class="btn btn-sm btn-danger pull-left muted muted-hover"><i class="fa fa-trash-o"></i></button>
<button name="action" value="delete" class="btn btn-sm btn-danger pull-left muted muted-hover"><i class="fa fa-trash-o"></i></button> <button name="_method" value="PATCH" class="btn btn-sm btn-primary pull-right">Save</button>
<button name="action" value="edit" class="btn btn-sm btn-primary pull-right">Save</button>
</div> </div>
</div> </div>
</div> </div>

View file

@ -40,6 +40,9 @@
</div> </div>
<div class="row"> <div class="row">
<div class="col-sm-7"> <div class="col-sm-7">
<div class="alert alert-info">
Database passwords can be viewed when <a href="{{ route('server.databases.index', ['server' => $server->uuidShort]) }}">visiting this server</a> on the front-end.
</div>
<div class="box box-primary"> <div class="box box-primary">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">Active Databases</h3> <h3 class="box-title">Active Databases</h3>
@ -128,7 +131,7 @@
}, function () { }, function () {
$.ajax({ $.ajax({
method: 'DELETE', method: 'DELETE',
url: Router.route('admin.servers.view.database.delete', { id: '{{ $server->id }}', database: self.data('id') }), url: Router.route('admin.servers.view.database.delete', { server: '{{ $server->id }}', database: self.data('id') }),
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }, headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },
}).done(function () { }).done(function () {
self.parent().parent().slideUp(); self.parent().parent().slideUp();
@ -149,7 +152,7 @@
$(this).addClass('disabled').find('i').addClass('fa-spin'); $(this).addClass('disabled').find('i').addClass('fa-spin');
$.ajax({ $.ajax({
type: 'PATCH', type: 'PATCH',
url: Router.route('admin.servers.view.database', { id: '{{ $server->id }}' }), url: Router.route('admin.servers.view.database', { server: '{{ $server->id }}' }),
headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') }, headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },
data: { database: $(this).data('id') }, data: { database: $(this).data('id') },
}).done(function (data) { }).done(function (data) {

View file

@ -155,6 +155,17 @@
</a> </a>
</li> </li>
@endcan @endcan
@can('view-databases', $server)
<li
@if(starts_with(Route::currentRouteName(), 'server.databases'))
class="active"
@endif
>
<a href="{{ route('server.databases.index', $server->uuidShort)}}">
<i class="fa fa-database"></i> <span>@lang('navigation.server.databases')</span>
</a>
</li>
@endcan
@if(Gate::allows('view-startup', $server) || Gate::allows('view-sftp', $server) || Gate::allows('view-databases', $server) || Gate::allows('view-allocation', $server)) @if(Gate::allows('view-startup', $server) || Gate::allows('view-sftp', $server) || Gate::allows('view-databases', $server) || Gate::allows('view-allocation', $server))
<li class="treeview <li class="treeview
@if(starts_with(Route::currentRouteName(), 'server.settings')) @if(starts_with(Route::currentRouteName(), 'server.settings'))
@ -178,9 +189,6 @@
@can('view-startup', $server) @can('view-startup', $server)
<li class="{{ Route::currentRouteName() !== 'server.settings.startup' ?: 'active' }}"><a href="{{ route('server.settings.startup', $server->uuidShort) }}"><i class="fa fa-angle-right"></i> @lang('navigation.server.startup_parameters')</a></li> <li class="{{ Route::currentRouteName() !== 'server.settings.startup' ?: 'active' }}"><a href="{{ route('server.settings.startup', $server->uuidShort) }}"><i class="fa fa-angle-right"></i> @lang('navigation.server.startup_parameters')</a></li>
@endcan @endcan
@can('view-databases', $server)
<li class="{{ Route::currentRouteName() !== 'server.settings.databases' ?: 'active' }}"><a href="{{ route('server.settings.databases', $server->uuidShort) }}"><i class="fa fa-angle-right"></i> @lang('navigation.server.databases')</a></li>
@endcan
</ul> </ul>
</li> </li>
@endif @endif

View file

@ -25,6 +25,11 @@
<div class="box"> <div class="box">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">@lang('server.config.database.your_dbs')</h3> <h3 class="box-title">@lang('server.config.database.your_dbs')</h3>
@if(auth()->user()->root_admin)
<div class="box-tools">
<a href="{{ route('admin.servers.view.database', ['server' => $server->id]) }}" target="_blank" class="btn btn-sm btn-success">Create New</a>
</div>
@endif
</div> </div>
@if(count($databases) > 0) @if(count($databases) > 0)
<div class="box-body table-responsive no-padding"> <div class="box-body table-responsive no-padding">
@ -41,7 +46,14 @@
<tr> <tr>
<td class="middle">{{ $database->database }}</td> <td class="middle">{{ $database->database }}</td>
<td class="middle">{{ $database->username }}</td> <td class="middle">{{ $database->username }}</td>
<td class="middle"><code data-attr="set-password">{{ Crypt::decrypt($database->password) }}</code></td> <td class="middle">
<code class="toggle-display" style="cursor:pointer" data-toggle="tooltip" data-placement="right" title="Click to Reveal">
<i class="fa fa-key"></i> &bull;&bull;&bull;&bull;&bull;&bull;&bull;&bull;
</code>
<code class="hidden" data-attr="set-password">
{{ Crypt::decrypt($database->password) }}
</code>
</td>
<td class="middle"><code>{{ $database->host->host }}:{{ $database->host->port }}</code></td> <td class="middle"><code>{{ $database->host->host }}:{{ $database->host->port }}</code></td>
@can('reset-db-password', $server) @can('reset-db-password', $server)
<td> <td>
@ -55,7 +67,7 @@
</div> </div>
@else @else
<div class="box-body"> <div class="box-body">
<div class="callout callout-info callout-nomargin"> <div class="alert alert-info no-margin-bottom">
@lang('server.config.database.no_dbs') @lang('server.config.database.no_dbs')
@if(Auth::user()->root_admin === 1) @if(Auth::user()->root_admin === 1)
<a href="{{ route('admin.servers.view', [ <a href="{{ route('admin.servers.view', [
@ -75,14 +87,21 @@
@parent @parent
{!! Theme::js('js/frontend/server.socket.js') !!} {!! Theme::js('js/frontend/server.socket.js') !!}
<script> <script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
});
$('.toggle-display').on('click', function () {
$(this).parent().find('code[data-attr="set-password"]').removeClass('hidden');
$(this).hide();
});
@can('reset-db-password', $server) @can('reset-db-password', $server)
$('[data-action="reset-password"]').click(function (e) { $('[data-action="reset-password"]').click(function (e) {
e.preventDefault(); e.preventDefault();
var block = $(this); var block = $(this);
$(this).addClass('disabled').find('i').addClass('fa-spin'); $(this).addClass('disabled').find('i').addClass('fa-spin');
$.ajax({ $.ajax({
type: 'POST', type: 'PATCH',
url: Router.route('server.ajax.reset-database-password', { server: Pterodactyl.server.uuidShort }), url: Router.route('server.databases.password', { server: Pterodactyl.server.uuidShort }),
headers: { headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'), 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
}, },
@ -90,8 +109,8 @@
database: $(this).data('id') database: $(this).data('id')
} }
}).done(function (data) { }).done(function (data) {
block.parent().parent().find('[data-attr="set-password"]').html(data); block.parent().parent().find('[data-attr="set-password"]').html(data.password);
}).fail(function(jqXHR, textStatus, errorThrown) { }).fail(function(jqXHR) {
console.error(jqXHR); console.error(jqXHR);
var error = 'An error occured while trying to process this request.'; var error = 'An error occured while trying to process this request.';
if (typeof jqXHR.responseJSON !== 'undefined' && typeof jqXHR.responseJSON.error !== 'undefined') { if (typeof jqXHR.responseJSON !== 'undefined' && typeof jqXHR.responseJSON.error !== 'undefined') {

View file

@ -38,6 +38,7 @@ Route::group(['prefix' => 'databases'], function () {
Route::post('/', 'DatabaseController@create'); Route::post('/', 'DatabaseController@create');
Route::patch('/view/{host}', 'DatabaseController@update'); Route::patch('/view/{host}', 'DatabaseController@update');
Route::delete('/view/{host}', 'DatabaseController@delete');
}); });
/* /*

View file

@ -18,7 +18,6 @@ Route::get('/console', 'ConsoleController@console')->name('server.console');
| |
*/ */
Route::group(['prefix' => 'settings'], function () { Route::group(['prefix' => 'settings'], function () {
Route::get('/databases', 'ServerController@getDatabases')->name('server.settings.databases');
Route::get('/sftp', 'ServerController@getSFTP')->name('server.settings.sftp'); Route::get('/sftp', 'ServerController@getSFTP')->name('server.settings.sftp');
Route::get('/startup', 'ServerController@getStartup')->name('server.settings.startup'); Route::get('/startup', 'ServerController@getStartup')->name('server.settings.startup');
Route::get('/allocation', 'ServerController@getAllocation')->name('server.settings.allocation'); Route::get('/allocation', 'ServerController@getAllocation')->name('server.settings.allocation');
@ -27,6 +26,20 @@ Route::group(['prefix' => 'settings'], function () {
Route::post('/startup', 'ServerController@postSettingsStartup'); Route::post('/startup', 'ServerController@postSettingsStartup');
}); });
/*
|--------------------------------------------------------------------------
| Server Database Controller Routes
|--------------------------------------------------------------------------
|
| Endpoint: /server/{server}/databases
|
*/
Route::group(['prefix' => 'databases'], function () {
Route::get('/', 'DatabaseController@index')->name('server.databases.index');
Route::patch('/password', 'DatabaseController@update')->middleware('server..database')->name('server.databases.password');
});
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Server File Manager Controller Routes | Server File Manager Controller Routes
@ -56,13 +69,13 @@ Route::group(['prefix' => 'files'], function () {
Route::group(['prefix' => 'users'], function () { Route::group(['prefix' => 'users'], function () {
Route::get('/', 'SubuserController@index')->name('server.subusers'); Route::get('/', 'SubuserController@index')->name('server.subusers');
Route::get('/new', 'SubuserController@create')->name('server.subusers.new'); Route::get('/new', 'SubuserController@create')->name('server.subusers.new');
Route::get('/view/{subuser}', 'SubuserController@view')->middleware('subuser')->name('server.subusers.view'); Route::get('/view/{subuser}', 'SubuserController@view')->middleware('server..subuser')->name('server.subusers.view');
Route::post('/new', 'SubuserController@store'); Route::post('/new', 'SubuserController@store');
Route::patch('/view/{subuser}', 'SubuserController@update')->middleware('subuser'); Route::patch('/view/{subuser}', 'SubuserController@update')->middleware('server..subuser');
Route::delete('/view/{subuser}/delete', 'SubuserController@delete')->middleware('subuser')->name('server.subusers.delete'); Route::delete('/view/{subuser}/delete', 'SubuserController@delete')->middleware('server..subuser')->name('server.subusers.delete');
}); });
/* /*
@ -76,24 +89,12 @@ Route::group(['prefix' => 'users'], function () {
Route::group(['prefix' => 'schedules'], function () { Route::group(['prefix' => 'schedules'], function () {
Route::get('/', 'Tasks\TaskManagementController@index')->name('server.schedules'); Route::get('/', 'Tasks\TaskManagementController@index')->name('server.schedules');
Route::get('/new', 'Tasks\TaskManagementController@create')->name('server.schedules.new'); Route::get('/new', 'Tasks\TaskManagementController@create')->name('server.schedules.new');
Route::get('/view/{schedule}', 'Tasks\TaskManagementController@view')->middleware('schedule')->name('server.schedules.view'); Route::get('/view/{schedule}', 'Tasks\TaskManagementController@view')->middleware('server..schedule')->name('server.schedules.view');
Route::post('/new', 'Tasks\TaskManagementController@store'); Route::post('/new', 'Tasks\TaskManagementController@store');
Route::patch('/view/{schedule}', 'Tasks\TaskManagementController@update')->middleware('schedule'); Route::patch('/view/{schedule}', 'Tasks\TaskManagementController@update')->middleware('server..schedule');
Route::patch('/view/{schedule}/toggle', 'Tasks\TaskToggleController@index')->middleware('schedule')->name('server.schedules.toggle'); Route::patch('/view/{schedule}/toggle', 'Tasks\TaskToggleController@index')->middleware('server..schedule')->name('server.schedules.toggle');
Route::delete('/view/{schedule}/delete', 'Tasks\TaskManagementController@delete')->middleware('schedule')->name('server.schedules.delete'); Route::delete('/view/{schedule}/delete', 'Tasks\TaskManagementController@delete')->middleware('server..schedule')->name('server.schedules.delete');
});
/*
|--------------------------------------------------------------------------
| Server Ajax Controller Routes
|--------------------------------------------------------------------------
|
| Endpoint: /server/{server}/ajax
|
*/
Route::group(['prefix' => 'ajax'], function () {
Route::post('/settings/reset-database-password', 'AjaxController@postResetDatabasePassword')->name('server.ajax.reset-database-password');
}); });

View file

@ -13,7 +13,6 @@ use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Tests\Assertions\ControllerAssertionsTrait; use Tests\Assertions\ControllerAssertionsTrait;
use Pterodactyl\Services\Database\DatabaseHostService;
use Pterodactyl\Http\Controllers\Admin\DatabaseController; use Pterodactyl\Http\Controllers\Admin\DatabaseController;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -43,7 +42,7 @@ class DatabaseControllerTest extends TestCase
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseHostService * @var \Pterodactyl\Services\Databases\HostsUpdateService
*/ */
protected $service; protected $service;
@ -57,7 +56,7 @@ class DatabaseControllerTest extends TestCase
$this->alert = m::mock(AlertsMessageBag::class); $this->alert = m::mock(AlertsMessageBag::class);
$this->locationRepository = m::mock(LocationRepositoryInterface::class); $this->locationRepository = m::mock(LocationRepositoryInterface::class);
$this->repository = m::mock(DatabaseHostRepositoryInterface::class); $this->repository = m::mock(DatabaseHostRepositoryInterface::class);
$this->service = m::mock(DatabaseHostService::class); $this->service = m::mock(HostUpdateService::class);
$this->controller = new DatabaseController( $this->controller = new DatabaseController(
$this->alert, $this->alert,

View file

@ -15,7 +15,6 @@ use Illuminate\Database\DatabaseManager;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Services\Database\DatabaseHostService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -47,7 +46,7 @@ class DatabaseHostServiceTest extends TestCase
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseHostService * @var \Pterodactyl\Services\Databases\HostsUpdateService
*/ */
protected $service; protected $service;
@ -64,7 +63,7 @@ class DatabaseHostServiceTest extends TestCase
$this->encrypter = m::mock(Encrypter::class); $this->encrypter = m::mock(Encrypter::class);
$this->repository = m::mock(DatabaseHostRepositoryInterface::class); $this->repository = m::mock(DatabaseHostRepositoryInterface::class);
$this->service = new DatabaseHostService( $this->service = new HostUpdateService(
$this->database, $this->database,
$this->databaseRepository, $this->databaseRepository,
$this->repository, $this->repository,

View file

@ -16,7 +16,7 @@ use phpmock\phpunit\PHPMock;
use Illuminate\Database\DatabaseManager; use Illuminate\Database\DatabaseManager;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Extensions\DynamicDatabaseConnection; use Pterodactyl\Extensions\DynamicDatabaseConnection;
use Pterodactyl\Services\Database\DatabaseManagementService; use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class DatabaseManagementServiceTest extends TestCase class DatabaseManagementServiceTest extends TestCase
@ -53,7 +53,7 @@ class DatabaseManagementServiceTest extends TestCase
protected $repository; protected $repository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseManagementService * @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/ */
protected $service; protected $service;

View file

@ -18,7 +18,7 @@ use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ServerDeletionService; use Pterodactyl\Services\Servers\ServerDeletionService;
use Pterodactyl\Services\Database\DatabaseManagementService; use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -36,7 +36,7 @@ class ServerDeletionServiceTest extends TestCase
protected $daemonServerRepository; protected $daemonServerRepository;
/** /**
* @var \Pterodactyl\Services\Database\DatabaseManagementService * @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/ */
protected $databaseManagementService; protected $databaseManagementService;