Skip to content

Commit a85ac87

Browse files
spaceemotionDaneEveritt
authored andcommitted
Refactor to use more laravel logic and improve compatibility with older PHP versions (pterodactyl#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
1 parent c3abb32 commit a85ac87

File tree

21 files changed

+199
-335
lines changed

21 files changed

+199
-335
lines changed

app/Http/Controllers/API/LocationController.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,13 @@ public function __construct()
4747
* @Versions({"v1"})
4848
* @Response(200)
4949
*/
50-
public function list(Request $request)
50+
public function lists(Request $request)
5151
{
52-
$locations = Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes'))
52+
return Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes'))
5353
->join('nodes', 'locations.id', '=', 'nodes.location')
5454
->groupBy('locations.id')
55-
->get();
56-
57-
foreach ($locations as &$location) {
58-
$location->nodes = explode(',', $location->nodes);
59-
}
60-
61-
return $locations->toArray();
55+
->get()->each(function ($location) {
56+
$location->nodes = explode(',', $location->nodes);
57+
})->all();
6258
}
6359
}

app/Http/Controllers/API/NodeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct()
5656
* })
5757
* @Response(200)
5858
*/
59-
public function list(Request $request)
59+
public function lists(Request $request)
6060
{
6161
return Models\Node::all()->toArray();
6262
}

app/Http/Controllers/API/ServerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct()
5757
* })
5858
* @Response(200)
5959
*/
60-
public function list(Request $request)
60+
public function lists(Request $request)
6161
{
6262
return Models\Server::all()->toArray();
6363
}

app/Http/Controllers/API/ServiceController.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct()
3838
//
3939
}
4040

41-
public function list(Request $request)
41+
public function lists(Request $request)
4242
{
4343
return Models\Service::all()->toArray();
4444
}
@@ -50,14 +50,12 @@ public function view(Request $request, $id)
5050
throw new NotFoundHttpException('No service by that ID was found.');
5151
}
5252

53-
$options = Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')->where('parent_service', $service->id)->get();
54-
foreach ($options as &$opt) {
55-
$opt->variables = Models\ServiceVariables::where('option_id', $opt->id)->get();
56-
}
57-
5853
return [
5954
'service' => $service,
60-
'options' => $options,
55+
'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')
56+
->where('parent_service', $service->id)
57+
->with('variables')
58+
->get(),
6159
];
6260
}
6361
}

app/Http/Controllers/API/User/InfoController.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ class InfoController extends BaseController
3232
{
3333
public function me(Request $request)
3434
{
35-
$servers = Models\Server::getUserServers();
36-
$response = [];
37-
38-
foreach ($servers as &$server) {
39-
$response = array_merge($response, [[
35+
return Models\Server::getUserServers()->map(function ($server) {
36+
return [
4037
'id' => $server->uuidShort,
4138
'uuid' => $server->uuid,
4239
'name' => $server->name,
@@ -48,9 +45,7 @@ public function me(Request $request)
4845
'port' => $server->port,
4946
'service' => $server->a_serviceName,
5047
'option' => $server->a_serviceOptionName,
51-
]]);
52-
}
53-
54-
return $response;
48+
];
49+
})->all();
5550
}
5651
}

app/Http/Controllers/API/User/ServerController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public function info(Request $request, $uuid)
9292
public function power(Request $request, $uuid)
9393
{
9494
$server = Models\Server::getByUUID($uuid);
95-
$node = Models\Node::getByID($server->node);
9695
$client = Models\Node::guzzleRequest($server->node);
9796

9897
Auth::user()->can('power-' . $request->input('action'), $server);

app/Http/Controllers/API/UserController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct()
5555
* })
5656
* @Response(200)
5757
*/
58-
public function list(Request $request)
58+
public function lists(Request $request)
5959
{
6060
return Models\User::all()->toArray();
6161
}

app/Http/Controllers/Base/APIController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function index(Request $request)
4848
]);
4949
}
5050

51-
public function new(Request $request)
51+
public function create(Request $request)
5252
{
5353
return view('base.api.new');
5454
}
@@ -57,7 +57,7 @@ public function save(Request $request)
5757
{
5858
try {
5959
$repo = new APIRepository($request->user());
60-
$secret = $repo->new($request->except(['_token']));
60+
$secret = $repo->create($request->except(['_token']));
6161
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();
6262

6363
return redirect()->route('account.api');

app/Http/Routes/APIRoutes.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function map(Router $router)
5555
*/
5656
$api->get('users', [
5757
'as' => 'api.admin.users.list',
58-
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@list',
58+
'uses' => 'Pterodactyl\Http\Controllers\API\UserController@lists',
5959
]);
6060

6161
$api->post('users', [
@@ -83,7 +83,7 @@ public function map(Router $router)
8383
*/
8484
$api->get('servers', [
8585
'as' => 'api.admin.servers.list',
86-
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@list',
86+
'uses' => 'Pterodactyl\Http\Controllers\API\ServerController@lists',
8787
]);
8888

8989
$api->post('servers', [
@@ -126,7 +126,7 @@ public function map(Router $router)
126126
*/
127127
$api->get('nodes', [
128128
'as' => 'api.admin.nodes.list',
129-
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@list',
129+
'uses' => 'Pterodactyl\Http\Controllers\API\NodeController@lists',
130130
]);
131131

132132
$api->post('nodes', [
@@ -164,15 +164,15 @@ public function map(Router $router)
164164
*/
165165
$api->get('locations', [
166166
'as' => 'api.admin.locations.list',
167-
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@list',
167+
'uses' => 'Pterodactyl\Http\Controllers\API\LocationController@lists',
168168
]);
169169

170170
/*
171171
* Service Routes
172172
*/
173173
$api->get('services', [
174174
'as' => 'api.admin.services.list',
175-
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@list',
175+
'uses' => 'Pterodactyl\Http\Controllers\API\ServiceController@lists',
176176
]);
177177

178178
$api->get('services/{id}', [

app/Http/Routes/BaseRoutes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function map(Router $router)
8585
]);
8686
$router->get('/new', [
8787
'as' => 'account.api.new',
88-
'uses' => 'Base\APIController@new',
88+
'uses' => 'Base\APIController@create',
8989
]);
9090
$router->post('/new', [
9191
'uses' => 'Base\APIController@save',

0 commit comments

Comments
 (0)