Skip to content

Commit c071efd

Browse files
committed
Finish API routes for users.
1 parent 9777330 commit c071efd

File tree

5 files changed

+61
-16
lines changed

5 files changed

+61
-16
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131

3232
class CoreController extends Controller
3333
{
34+
/**
35+
* Controller to handle base user request for all of their servers.
36+
*
37+
* @param \Illuminate\Http\Request $request
38+
* @return array
39+
*/
3440
public function index(Request $request)
3541
{
3642
$servers = $request->user()->access('service', 'node', 'allocation', 'option')->get();

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,20 @@
2727
use Fractal;
2828
use Illuminate\Http\Request;
2929
use Pterodactyl\Models\Server;
30+
use GuzzleHttp\Exception\ConnectException;
3031
use Pterodactyl\Http\Controllers\Controller;
3132
use Pterodactyl\Transformers\User\ServerTransformer;
33+
use Pterodactyl\Repositories\Daemon\PowerRepository;
3234

3335
class ServerController extends Controller
3436
{
37+
/**
38+
* Controller to handle base request for individual server information.
39+
*
40+
* @param \Illuminate\Http\Request $request
41+
* @param string $uuid
42+
* @return array
43+
*/
3544
public function index(Request $request, $uuid)
3645
{
3746
$server = Server::byUuid($uuid);
@@ -46,13 +55,39 @@ public function index(Request $request, $uuid)
4655
return $fractal->transformWith(new ServerTransformer)->toArray();
4756
}
4857

58+
/**
59+
* Controller to handle request for server power toggle.
60+
*
61+
* @param \Illuminate\Http\Request $request
62+
* @param string $uuid
63+
* @return \Illuminate\Http\Response
64+
*/
4965
public function power(Request $request, $uuid)
5066
{
67+
$server = Server::byUuid($uuid);
68+
$request->user()->can('power-' . $request->input('action'), $server);
69+
70+
$repo = new PowerRepository($server);
71+
$repo->do($request->input('action'));
5172

73+
return response('', 204)->header('Content-Type', 'application/json');
5274
}
5375

76+
/**
77+
* Controller to handle base request for individual server information.
78+
*
79+
* @param \Illuminate\Http\Request $request
80+
* @param string $uuid
81+
* @return \Illuminate\Http\Response
82+
*/
5483
public function command(Request $request, $uuid)
5584
{
85+
$server = Server::byUuid($uuid);
86+
$request->user()->can('send-command', $server);
87+
88+
$repo = new CommandRepository($server);
89+
$repo->send($request->input('command'));
5690

91+
return response('', 204)->header('Content-Type', 'application/json');
5792
}
5893
}

app/Http/Middleware/HMACAuthorization.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Config;
3030
use Closure;
3131
use Response;
32+
use Debugbar;
3233
use IPTools\IP;
3334
use IPTools\Range;
3435
use Illuminate\Http\Request;
@@ -74,6 +75,7 @@ class HMACAuthorization
7475
*/
7576
public function __construct()
7677
{
78+
Debugbar::disable();
7779
Config::set('session.driver', 'array');
7880
}
7981

app/Repositories/Daemon/CommandRepository.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Pterodactyl\Repositories\Daemon;
2626

2727
use Pterodactyl\Models;
28+
use GuzzleHttp\Exception\ConnectException;
2829
use Pterodactyl\Exceptions\DisplayException;
2930

3031
class CommandRepository
@@ -60,20 +61,20 @@ public function send($command)
6061
{
6162
// We don't use the user's specific daemon secret here since we
6263
// are assuming that a call to this function has been validated.
63-
// Additionally not all calls to this will be from a logged in user.
64-
// (e.g. task queue or API)
6564
try {
66-
$response = $this->server->node->guzzleClient([
67-
'X-Access-Token' => $this->server->daemonSecret,
68-
'X-Access-Server' => $this->server->uuid,
69-
])->request('POST', '/server/command', ['json' => ['command' => $command]]);
65+
$response = $this->server->guzzleClient()->request('PUT', '/server/command', [
66+
'http_errors' => false,
67+
'json' => [
68+
'command' => $command,
69+
],
70+
]);
7071

7172
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
72-
throw new DisplayException('Command sending responded with a non-200 error code.');
73+
throw new DisplayException('Command sending responded with a non-200 error code (HTTP/' . $response->getStatusCode() . ').');
7374
}
7475

7576
return $response->getBody();
76-
} catch (\Exception $ex) {
77+
} catch (ConnectException $ex) {
7778
throw $ex;
7879
}
7980
}

app/Repositories/Daemon/PowerRepository.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
namespace Pterodactyl\Repositories\Daemon;
2626

2727
use Pterodactyl\Models;
28+
use GuzzleHttp\Exception\ConnectException;
2829
use Pterodactyl\Exceptions\DisplayException;
2930

3031
class PowerRepository
@@ -60,20 +61,20 @@ public function do($action)
6061
{
6162
// We don't use the user's specific daemon secret here since we
6263
// are assuming that a call to this function has been validated.
63-
// Additionally not all calls to this will be from a logged in user.
64-
// (e.g. task queue or API)
6564
try {
66-
$response = $this->server->node->guzzleClient([
67-
'X-Access-Token' => $this->server->daemonSecret,
68-
'X-Access-Server' => $this->server->uuid,
69-
])->request('PUT', '/server/power', ['json' => ['action' => $action]]);
65+
$response = $this->server->guzzleClient()->request('PUT', '/server/power', [
66+
'http_errors' => false,
67+
'json' => [
68+
'action' => $action,
69+
],
70+
]);
7071

7172
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
72-
throw new DisplayException('Power status responded with a non-200 error code.');
73+
throw new DisplayException('Power toggle endpoint responded with a non-200 error code (HTTP/' . $response->getStatusCode() . ').');
7374
}
7475

7576
return $response->getBody();
76-
} catch (\Exception $ex) {
77+
} catch (ConnectException $ex) {
7778
throw $ex;
7879
}
7980
}

0 commit comments

Comments
 (0)