Refactor to use more laravel logic and improve compatibility with older PHP versions (#206)

* Fix @param namespaces for PHPDocs in ServerPolicy

* Reduce permission check duplication in ServerPolicy

This introduces a new checkPermission method to reduce code duplication when checking for permissions.

* Simplify logic to list accessible servers for the user

We can directly use the pluck function that laravel collections provide to simplify the logic.

* Fix pagination issue when databases/servers exceed 20

Laravels strips out the currently selected tab (or any GET query for that matter) by default when using pagination. the appends() methods helps with keeping that information.

* Refactor unnecessary array_merge calls

We can just append to the array instead of constantly merging a new copy.

* Fix accessing “API Access” on some versions of PHP

The “new” word is reserved and should not be used as a method name.

http://stackoverflow.com/questions/9575590/why-am-i-getting-an-unexpected-t-new-error-in-php

* Fix revoking API keys on older versions of php (5.6)

“string” was not a valid function argument type yet, so revoking keys results in an error on older installations.

* Fix issues with API due to methods named “list”

“list” is yet another reserved keyword in PHP and messes up older installations of PHP (5.6).
This renames all methods named “list” to “lists”. The API route names are left untouched (e.g. still called “api.admin.users.list”).

* Refactor and shorten some API logic

Used laravel collection methods where applicable to directly transform the values instead of converting back and forth.
This also removes some dead variables that were never used as well as getting rid of a n+1 problem in the Service API (loading service variables afterwards, not during the model creation).

* Return model save status in repositories where applicable

* Fix typo in ServicePolicy#powerStart

* Apply StyleCI corrections
This commit is contained in:
spaceemotion 2016-12-12 20:30:57 +01:00 committed by Dane Everitt
parent c3abb32c0c
commit a85ac87ae8
21 changed files with 199 additions and 335 deletions

View file

@ -47,17 +47,13 @@ class LocationController extends BaseController
* @Versions({"v1"})
* @Response(200)
*/
public function list(Request $request)
public function lists(Request $request)
{
$locations = Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes'))
return Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes'))
->join('nodes', 'locations.id', '=', 'nodes.location')
->groupBy('locations.id')
->get();
foreach ($locations as &$location) {
$location->nodes = explode(',', $location->nodes);
}
return $locations->toArray();
->get()->each(function ($location) {
$location->nodes = explode(',', $location->nodes);
})->all();
}
}

View file

@ -56,7 +56,7 @@ class NodeController extends BaseController
* })
* @Response(200)
*/
public function list(Request $request)
public function lists(Request $request)
{
return Models\Node::all()->toArray();
}

View file

@ -57,7 +57,7 @@ class ServerController extends BaseController
* })
* @Response(200)
*/
public function list(Request $request)
public function lists(Request $request)
{
return Models\Server::all()->toArray();
}

View file

@ -38,7 +38,7 @@ class ServiceController extends BaseController
//
}
public function list(Request $request)
public function lists(Request $request)
{
return Models\Service::all()->toArray();
}
@ -50,14 +50,12 @@ class ServiceController extends BaseController
throw new NotFoundHttpException('No service by that ID was found.');
}
$options = Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')->where('parent_service', $service->id)->get();
foreach ($options as &$opt) {
$opt->variables = Models\ServiceVariables::where('option_id', $opt->id)->get();
}
return [
'service' => $service,
'options' => $options,
'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')
->where('parent_service', $service->id)
->with('variables')
->get(),
];
}
}

View file

@ -32,11 +32,8 @@ class InfoController extends BaseController
{
public function me(Request $request)
{
$servers = Models\Server::getUserServers();
$response = [];
foreach ($servers as &$server) {
$response = array_merge($response, [[
return Models\Server::getUserServers()->map(function ($server) {
return [
'id' => $server->uuidShort,
'uuid' => $server->uuid,
'name' => $server->name,
@ -48,9 +45,7 @@ class InfoController extends BaseController
'port' => $server->port,
'service' => $server->a_serviceName,
'option' => $server->a_serviceOptionName,
]]);
}
return $response;
];
})->all();
}
}

View file

@ -92,7 +92,6 @@ class ServerController extends BaseController
public function power(Request $request, $uuid)
{
$server = Models\Server::getByUUID($uuid);
$node = Models\Node::getByID($server->node);
$client = Models\Node::guzzleRequest($server->node);
Auth::user()->can('power-' . $request->input('action'), $server);

View file

@ -55,7 +55,7 @@ class UserController extends BaseController
* })
* @Response(200)
*/
public function list(Request $request)
public function lists(Request $request)
{
return Models\User::all()->toArray();
}

View file

@ -48,7 +48,7 @@ class APIController extends Controller
]);
}
public function new(Request $request)
public function create(Request $request)
{
return view('base.api.new');
}
@ -57,7 +57,7 @@ class APIController extends Controller
{
try {
$repo = new APIRepository($request->user());
$secret = $repo->new($request->except(['_token']));
$secret = $repo->create($request->except(['_token']));
Alert::success('An API Keypair has successfully been generated. The API secret for this public key is shown below and will not be shown again.<br /><br /><code>' . $secret . '</code>')->flash();
return redirect()->route('account.api');

View file

@ -55,7 +55,7 @@ class APIRoutes
*/
$api->get('users', [
'as' => 'api.admin.users.list',
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@list',
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@lists',
]);
$api->post('users', [
@ -83,7 +83,7 @@ class APIRoutes
*/
$api->get('servers', [
'as' => 'api.admin.servers.list',
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@list',
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@lists',
]);
$api->post('servers', [
@ -126,7 +126,7 @@ class APIRoutes
*/
$api->get('nodes', [
'as' => 'api.admin.nodes.list',
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@list',
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@lists',
]);
$api->post('nodes', [
@ -164,7 +164,7 @@ class APIRoutes
*/
$api->get('locations', [
'as' => 'api.admin.locations.list',
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@list',
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@lists',
]);
/*
@ -172,7 +172,7 @@ class APIRoutes
*/
$api->get('services', [
'as' => 'api.admin.services.list',
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@list',
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@lists',
]);
$api->get('services/{id}', [

View file

@ -85,7 +85,7 @@ class BaseRoutes
]);
$router->get('/new', [
'as' => 'account.api.new',
'uses' => 'Base\APIController@new',
'uses' => 'Base\APIController@create',
]);
$router->post('/new', [
'uses' => 'Base\APIController@save',