From c17f9ba8a9f535c8276f7eeca22b8e0ed9faff7a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sun, 24 Nov 2019 12:50:16 -0800 Subject: [PATCH] Move server view management parts to new controller and clean up code --- .../Admin/Servers/ServerController.php | 51 +++++ .../Admin/Servers/ServerViewController.php | 176 ++++++++++++++++++ .../Controllers/Admin/ServersController.php | 146 --------------- .../Admin/Servers/ServerInstalled.php | 40 ++++ app/Models/Nest.php | 13 ++ app/Models/Server.php | 2 +- .../ServerConfigurationStructureService.php | 2 +- .../Controllers/JavascriptInjection.php | 11 ++ .../admin/servers/view/database.blade.php | 2 +- routes/admin.php | 23 ++- 10 files changed, 309 insertions(+), 157 deletions(-) create mode 100644 app/Http/Controllers/Admin/Servers/ServerController.php create mode 100644 app/Http/Controllers/Admin/Servers/ServerViewController.php create mode 100644 app/Http/Middleware/Admin/Servers/ServerInstalled.php diff --git a/app/Http/Controllers/Admin/Servers/ServerController.php b/app/Http/Controllers/Admin/Servers/ServerController.php new file mode 100644 index 000000000..31bee2cdc --- /dev/null +++ b/app/Http/Controllers/Admin/Servers/ServerController.php @@ -0,0 +1,51 @@ +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') + ), + ]); + } +} diff --git a/app/Http/Controllers/Admin/Servers/ServerViewController.php b/app/Http/Controllers/Admin/Servers/ServerViewController.php new file mode 100644 index 000000000..de78ca17f --- /dev/null +++ b/app/Http/Controllers/Admin/Servers/ServerViewController.php @@ -0,0 +1,176 @@ +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')); + } +} diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index c89d9a621..70c3af092 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -9,7 +9,6 @@ namespace Pterodactyl\Http\Controllers\Admin; -use Javascript; use Illuminate\Http\Request; use Pterodactyl\Models\User; use Pterodactyl\Models\Server; @@ -171,151 +170,6 @@ class ServersController extends Controller $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. * diff --git a/app/Http/Middleware/Admin/Servers/ServerInstalled.php b/app/Http/Middleware/Admin/Servers/ServerInstalled.php new file mode 100644 index 000000000..2f0a384f3 --- /dev/null +++ b/app/Http/Middleware/Admin/Servers/ServerInstalled.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/app/Models/Nest.php b/app/Models/Nest.php index 8c1a6cccd..cd31a6b76 100644 --- a/app/Models/Nest.php +++ b/app/Models/Nest.php @@ -2,6 +2,19 @@ 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 { /** diff --git a/app/Models/Server.php b/app/Models/Server.php index 4092c4357..68f3e8441 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -16,7 +16,7 @@ use Znck\Eloquent\Traits\BelongsToThrough; * @property string $name * @property string $description * @property bool $skip_scripts - * @property bool $suspended + * @property int $suspended * @property int $owner_id * @property int $memory * @property int $swap diff --git a/app/Services/Servers/ServerConfigurationStructureService.php b/app/Services/Servers/ServerConfigurationStructureService.php index ef5efd727..c0465e6cb 100644 --- a/app/Services/Servers/ServerConfigurationStructureService.php +++ b/app/Services/Servers/ServerConfigurationStructureService.php @@ -73,7 +73,7 @@ class ServerConfigurationStructureService { return [ 'uuid' => $server->uuid, - 'suspended' => $server->suspended, + 'suspended' => (bool) $server->suspended, 'environment' => $this->environment->handle($server), 'build' => [ 'oom_disabled' => $server->oom_disabled, diff --git a/app/Traits/Controllers/JavascriptInjection.php b/app/Traits/Controllers/JavascriptInjection.php index 06cb6de58..2f0e472e5 100644 --- a/app/Traits/Controllers/JavascriptInjection.php +++ b/app/Traits/Controllers/JavascriptInjection.php @@ -61,4 +61,15 @@ trait JavascriptInjection 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); + } } diff --git a/resources/views/admin/servers/view/database.blade.php b/resources/views/admin/servers/view/database.blade.php index 25d6d1573..5bea4be77 100644 --- a/resources/views/admin/servers/view/database.blade.php +++ b/resources/views/admin/servers/view/database.blade.php @@ -41,7 +41,7 @@
- Database passwords can be viewed when visiting this server on the front-end. + Database passwords can be viewed when visiting this server on the front-end.
diff --git a/routes/admin.php b/routes/admin.php index 2d099760c..e91b2a4e9 100644 --- a/routes/admin.php +++ b/routes/admin.php @@ -1,5 +1,8 @@ name('admin.index'); Route::get('/statistics', 'StatisticsController@index')->name('admin.statistics'); @@ -101,15 +104,19 @@ Route::group(['prefix' => 'users'], 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('/view/{server}', 'ServersController@viewIndex')->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::get('/view/{server}/startup', 'ServersController@viewStartup')->name('admin.servers.view.startup'); - Route::get('/view/{server}/database', 'ServersController@viewDatabase')->name('admin.servers.view.database'); - Route::get('/view/{server}/manage', 'ServersController@viewManage')->name('admin.servers.view.manage'); - Route::get('/view/{server}/delete', 'ServersController@viewDelete')->name('admin.servers.view.delete'); + Route::get('/view/{server}', 'Servers\ServerViewController@index')->name('admin.servers.view'); + + Route::group(['middleware' => [ServerInstalled::class]], function () { + Route::get('/view/{server}/details', 'Servers\ServerViewController@details')->name('admin.servers.view.details'); + Route::get('/view/{server}/build', 'Servers\ServerViewController@build')->name('admin.servers.view.build'); + Route::get('/view/{server}/startup', 'Servers\ServerViewController@startup')->name('admin.servers.view.startup'); + 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('/view/{server}/build', 'ServersController@updateBuild');