Move server view management parts to new controller and clean up code

This commit is contained in:
Dane Everitt 2019-11-24 12:50:16 -08:00
parent 8f0044575f
commit c17f9ba8a9
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
10 changed files with 309 additions and 157 deletions

View file

@ -0,0 +1,51 @@
<?php
namespace Pterodactyl\Http\Controllers\Admin\Servers;
use Illuminate\Http\Request;
use Illuminate\Contracts\View\Factory;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
class ServerController extends Controller
{
/**
* @var \Illuminate\Contracts\View\Factory
*/
private $view;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $repository;
/**
* ServerController constructor.
*
* @param \Illuminate\Contracts\View\Factory $view
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
*/
public function __construct(
Factory $view,
ServerRepository $repository
) {
$this->view = $view;
$this->repository = $repository;
}
/**
* Returns all of the servers that exist on the system using a paginated result set. If
* a query is passed along in the request it is also passed to the repository function.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
*/
public function index(Request $request)
{
return $this->view->make('admin.servers.index', [
'servers' => $this->repository->setSearchTerm($request->input('query'))->getAllServers(
config()->get('pterodactyl.paginate.admin.servers')
),
]);
}
}

View file

@ -0,0 +1,176 @@
<?php
namespace Pterodactyl\Http\Controllers\Admin\Servers;
use Illuminate\Http\Request;
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\View\Factory;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Repositories\Eloquent\NestRepository;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Traits\Controllers\JavascriptInjection;
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
class ServerViewController extends Controller
{
use JavascriptInjection;
/**
* @var \Illuminate\Contracts\View\Factory
*/
private $view;
/**
* @var \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository
*/
private $databaseHostRepository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
private $repository;
/**
* @var \Pterodactyl\Repositories\Eloquent\NestRepository
*/
private $nestRepository;
/**
* ServerViewController constructor.
*
* @param \Pterodactyl\Repositories\Eloquent\DatabaseHostRepository $databaseHostRepository
* @param \Pterodactyl\Repositories\Eloquent\NestRepository $nestRepository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
* @param \Illuminate\Contracts\View\Factory $view
*/
public function __construct(
DatabaseHostRepository $databaseHostRepository,
NestRepository $nestRepository,
ServerRepository $repository,
Factory $view
) {
$this->view = $view;
$this->databaseHostRepository = $databaseHostRepository;
$this->repository = $repository;
$this->nestRepository = $nestRepository;
}
/**
* Returns the index view for a server.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*/
public function index(Request $request, Server $server)
{
return $this->view->make('admin.servers.view.index', compact('server'));
}
/**
* Returns the server details page.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*/
public function details(Request $request, Server $server)
{
return $this->view->make('admin.servers.view.details', compact('server'));
}
/**
* Returns a view of server build settings.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*/
public function build(Request $request, Server $server)
{
$allocations = $server->node->allocations->toBase();
return $this->view->make('admin.servers.view.build', [
'server' => $server,
'assigned' => $allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
'unassigned' => $allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
]);
}
/**
* Returns the server startup management page.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function startup(Request $request, Server $server)
{
$parameters = $this->repository->getVariablesWithValues($server->id, true);
$nests = $this->nestRepository->getWithEggs();
$this->plainInject([
'server' => $server,
'server_variables' => $parameters->data,
'nests' => $nests->map(function (Nest $item) {
return array_merge($item->toArray(), [
'eggs' => $item->eggs->keyBy('id')->toArray(),
]);
})->keyBy('id'),
]);
return $this->view->make('admin.servers.view.startup', compact('server', 'nests'));
}
/**
* Returns all of the databases that exist for the server.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*/
public function database(Request $request, Server $server)
{
return $this->view->make('admin.servers.view.database', [
'hosts' => $this->databaseHostRepository->all(),
'server' => $server,
]);
}
/**
* Returns the base server management page, or an exception if the server
* is in a state that cannot be recovered from.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function manage(Request $request, Server $server)
{
if ($server->installed > 1) {
throw new DisplayException(
'This server is in a failed install state and cannot be recovered. Please delete and re-create the server.'
);
}
return $this->view->make('admin.servers.view.manage', compact('server'));
}
/**
* Returns the server deletion page.
*
* @param \Illuminate\Http\Request $request
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Contracts\View\View
*/
public function delete(Request $request, Server $server)
{
return $this->view->make('admin.servers.view.delete', compact('server'));
}
}

View file

@ -9,7 +9,6 @@
namespace Pterodactyl\Http\Controllers\Admin; namespace Pterodactyl\Http\Controllers\Admin;
use Javascript;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\User; use Pterodactyl\Models\User;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
@ -171,151 +170,6 @@ class ServersController extends Controller
$this->suspensionService = $suspensionService; $this->suspensionService = $suspensionService;
} }
/**
* Display the index page with all servers currently on the system.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function index(Request $request)
{
return view('admin.servers.index', [
'servers' => $this->repository->setSearchTerm($request->input('query'))->getAllServers(
$this->config->get('pterodactyl.paginate.admin.servers')
),
]);
}
/**
* Display the index when viewing a specific server.
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\View\View
*/
public function viewIndex(Server $server)
{
return view('admin.servers.view.index', ['server' => $server]);
}
/**
* Display the details page when viewing a specific server.
*
* @param int $server
* @return \Illuminate\View\View
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function viewDetails($server)
{
return view('admin.servers.view.details', [
'server' => $this->repository->findFirstWhere([
['id', '=', $server],
['installed', '=', 1],
]),
]);
}
/**
* Display the build details page when viewing a specific server.
*
* @param int $server
* @return \Illuminate\View\View
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function viewBuild($server)
{
$server = $this->repository->findFirstWhere([
['id', '=', $server],
['installed', '=', 1],
]);
$allocations = $this->allocationRepository->getAllocationsForNode($server->node_id);
return view('admin.servers.view.build', [
'server' => $server,
'assigned' => $allocations->where('server_id', $server->id)->sortBy('port')->sortBy('ip'),
'unassigned' => $allocations->where('server_id', null)->sortBy('port')->sortBy('ip'),
]);
}
/**
* Display startup configuration page for a server.
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\View\View
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function viewStartup(Server $server)
{
$parameters = $this->repository->getVariablesWithValues($server->id, true);
if (! $parameters->server->installed) {
abort(404);
}
$nests = $this->nestRepository->getWithEggs();
Javascript::put([
'server' => $server,
'nests' => $nests->map(function ($item) {
return array_merge($item->toArray(), [
'eggs' => $item->eggs->keyBy('id')->toArray(),
]);
})->keyBy('id'),
'server_variables' => $parameters->data,
]);
return view('admin.servers.view.startup', [
'server' => $parameters->server,
'nests' => $nests,
]);
}
/**
* Display the database management page for a specific server.
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\View\View
*/
public function viewDatabase(Server $server)
{
$this->repository->loadDatabaseRelations($server);
return view('admin.servers.view.database', [
'hosts' => $this->databaseHostRepository->all(),
'server' => $server,
]);
}
/**
* Display the management page when viewing a specific server.
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\View\View
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function viewManage(Server $server)
{
if ($server->installed > 1) {
throw new DisplayException('This server is in a failed installation state and must be deleted and recreated.');
}
return view('admin.servers.view.manage', ['server' => $server]);
}
/**
* Display the deletion page for a server.
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\View\View
*/
public function viewDelete(Server $server)
{
return view('admin.servers.view.delete', ['server' => $server]);
}
/** /**
* Update the details for a server. * Update the details for a server.
* *

View file

@ -0,0 +1,40 @@
<?php
namespace Pterodactyl\Http\Middleware\Admin\Servers;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ServerInstalled
{
/**
* Checks that the server is installed before allowing access through the route.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
/** @var \Pterodactyl\Models\Server|null $server */
$server = $request->route()->parameter('server');
if (! $server instanceof Server) {
throw new NotFoundHttpException(
'No server resource was located in the request parameters.'
);
}
if ($server->installed !== 1) {
throw new HttpException(
Response::HTTP_FORBIDDEN, 'Access to this resource is not allowed due to the current installation state.'
);
}
return $next($request);
}
}

View file

@ -2,6 +2,19 @@
namespace Pterodactyl\Models; namespace Pterodactyl\Models;
/**
* @property int $id
* @property string $uuid
* @property string $author
* @property string $name
* @property string $description
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Server[] $servers
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Egg[] $eggs
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Pack[] $packs
*/
class Nest extends Validable class Nest extends Validable
{ {
/** /**

View file

@ -16,7 +16,7 @@ use Znck\Eloquent\Traits\BelongsToThrough;
* @property string $name * @property string $name
* @property string $description * @property string $description
* @property bool $skip_scripts * @property bool $skip_scripts
* @property bool $suspended * @property int $suspended
* @property int $owner_id * @property int $owner_id
* @property int $memory * @property int $memory
* @property int $swap * @property int $swap

View file

@ -73,7 +73,7 @@ class ServerConfigurationStructureService
{ {
return [ return [
'uuid' => $server->uuid, 'uuid' => $server->uuid,
'suspended' => $server->suspended, 'suspended' => (bool) $server->suspended,
'environment' => $this->environment->handle($server), 'environment' => $this->environment->handle($server),
'build' => [ 'build' => [
'oom_disabled' => $server->oom_disabled, 'oom_disabled' => $server->oom_disabled,

View file

@ -61,4 +61,15 @@ trait JavascriptInjection
return Javascript::put($overwrite ? $args : $response); return Javascript::put($overwrite ? $args : $response);
} }
/**
* Injects the exact array passed in, nothing more.
*
* @param array $args
* @return array
*/
public function plainInject($args = [])
{
return Javascript::put($args);
}
} }

View file

@ -41,7 +41,7 @@
<div class="row"> <div class="row">
<div class="col-sm-7"> <div class="col-sm-7">
<div class="alert alert-info"> <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. Database passwords can be viewed when <a href="/server/{{ $server->uuidShort }}/databases">visiting this server</a> on the front-end.
</div> </div>
<div class="box box-primary"> <div class="box box-primary">
<div class="box-header with-border"> <div class="box-header with-border">

View file

@ -1,5 +1,8 @@
<?php <?php
use Illuminate\Support\Facades\Route;
use Pterodactyl\Http\Middleware\Admin\Servers\ServerInstalled;
Route::get('/', 'BaseController@index')->name('admin.index'); Route::get('/', 'BaseController@index')->name('admin.index');
Route::get('/statistics', 'StatisticsController@index')->name('admin.statistics'); Route::get('/statistics', 'StatisticsController@index')->name('admin.statistics');
@ -101,15 +104,19 @@ Route::group(['prefix' => 'users'], function () {
| |
*/ */
Route::group(['prefix' => 'servers'], function () { Route::group(['prefix' => 'servers'], function () {
Route::get('/', 'ServersController@index')->name('admin.servers'); Route::get('/', 'Servers\ServerController@index')->name('admin.servers');
Route::get('/new', 'Servers\CreateServerController@index')->name('admin.servers.new'); Route::get('/new', 'Servers\CreateServerController@index')->name('admin.servers.new');
Route::get('/view/{server}', 'ServersController@viewIndex')->name('admin.servers.view'); Route::get('/view/{server}', 'Servers\ServerViewController@index')->name('admin.servers.view');
Route::get('/view/{server}/details', 'ServersController@viewDetails')->name('admin.servers.view.details');
Route::get('/view/{server}/build', 'ServersController@viewBuild')->name('admin.servers.view.build'); Route::group(['middleware' => [ServerInstalled::class]], function () {
Route::get('/view/{server}/startup', 'ServersController@viewStartup')->name('admin.servers.view.startup'); Route::get('/view/{server}/details', 'Servers\ServerViewController@details')->name('admin.servers.view.details');
Route::get('/view/{server}/database', 'ServersController@viewDatabase')->name('admin.servers.view.database'); Route::get('/view/{server}/build', 'Servers\ServerViewController@build')->name('admin.servers.view.build');
Route::get('/view/{server}/manage', 'ServersController@viewManage')->name('admin.servers.view.manage'); Route::get('/view/{server}/startup', 'Servers\ServerViewController@startup')->name('admin.servers.view.startup');
Route::get('/view/{server}/delete', 'ServersController@viewDelete')->name('admin.servers.view.delete'); Route::get('/view/{server}/database', 'Servers\ServerViewController@database')->name('admin.servers.view.database');
});
Route::get('/view/{server}/manage', 'Servers\ServerViewController@manage')->name('admin.servers.view.manage');
Route::get('/view/{server}/delete', 'Servers\ServerViewController@delete')->name('admin.servers.view.delete');
Route::post('/new', 'Servers\CreateServerController@store'); Route::post('/new', 'Servers\CreateServerController@store');
Route::post('/view/{server}/build', 'ServersController@updateBuild'); Route::post('/view/{server}/build', 'ServersController@updateBuild');