Finish API routes for users.
This commit is contained in:
parent
97773300ed
commit
c071efd008
|
@ -31,6 +31,12 @@ use Pterodactyl\Transformers\User\ServerTransformer;
|
||||||
|
|
||||||
class CoreController extends Controller
|
class CoreController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Controller to handle base user request for all of their servers.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function index(Request $request)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$servers = $request->user()->access('service', 'node', 'allocation', 'option')->get();
|
$servers = $request->user()->access('service', 'node', 'allocation', 'option')->get();
|
||||||
|
|
|
@ -27,11 +27,20 @@ namespace Pterodactyl\Http\Controllers\API\User;
|
||||||
use Fractal;
|
use Fractal;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
use Pterodactyl\Http\Controllers\Controller;
|
use Pterodactyl\Http\Controllers\Controller;
|
||||||
use Pterodactyl\Transformers\User\ServerTransformer;
|
use Pterodactyl\Transformers\User\ServerTransformer;
|
||||||
|
use Pterodactyl\Repositories\Daemon\PowerRepository;
|
||||||
|
|
||||||
class ServerController extends Controller
|
class ServerController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Controller to handle base request for individual server information.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param string $uuid
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public function index(Request $request, $uuid)
|
public function index(Request $request, $uuid)
|
||||||
{
|
{
|
||||||
$server = Server::byUuid($uuid);
|
$server = Server::byUuid($uuid);
|
||||||
|
@ -46,13 +55,39 @@ class ServerController extends Controller
|
||||||
return $fractal->transformWith(new ServerTransformer)->toArray();
|
return $fractal->transformWith(new ServerTransformer)->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller to handle request for server power toggle.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param string $uuid
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function power(Request $request, $uuid)
|
public function power(Request $request, $uuid)
|
||||||
{
|
{
|
||||||
|
$server = Server::byUuid($uuid);
|
||||||
|
$request->user()->can('power-' . $request->input('action'), $server);
|
||||||
|
|
||||||
|
$repo = new PowerRepository($server);
|
||||||
|
$repo->do($request->input('action'));
|
||||||
|
|
||||||
|
return response('', 204)->header('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller to handle base request for individual server information.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param string $uuid
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
public function command(Request $request, $uuid)
|
public function command(Request $request, $uuid)
|
||||||
{
|
{
|
||||||
|
$server = Server::byUuid($uuid);
|
||||||
|
$request->user()->can('send-command', $server);
|
||||||
|
|
||||||
|
$repo = new CommandRepository($server);
|
||||||
|
$repo->send($request->input('command'));
|
||||||
|
|
||||||
|
return response('', 204)->header('Content-Type', 'application/json');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ use Crypt;
|
||||||
use Config;
|
use Config;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Response;
|
use Response;
|
||||||
|
use Debugbar;
|
||||||
use IPTools\IP;
|
use IPTools\IP;
|
||||||
use IPTools\Range;
|
use IPTools\Range;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
@ -74,6 +75,7 @@ class HMACAuthorization
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
Debugbar::disable();
|
||||||
Config::set('session.driver', 'array');
|
Config::set('session.driver', 'array');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
namespace Pterodactyl\Repositories\Daemon;
|
namespace Pterodactyl\Repositories\Daemon;
|
||||||
|
|
||||||
use Pterodactyl\Models;
|
use Pterodactyl\Models;
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
use Pterodactyl\Exceptions\DisplayException;
|
use Pterodactyl\Exceptions\DisplayException;
|
||||||
|
|
||||||
class CommandRepository
|
class CommandRepository
|
||||||
|
@ -60,20 +61,20 @@ class CommandRepository
|
||||||
{
|
{
|
||||||
// We don't use the user's specific daemon secret here since we
|
// We don't use the user's specific daemon secret here since we
|
||||||
// are assuming that a call to this function has been validated.
|
// are assuming that a call to this function has been validated.
|
||||||
// Additionally not all calls to this will be from a logged in user.
|
|
||||||
// (e.g. task queue or API)
|
|
||||||
try {
|
try {
|
||||||
$response = $this->server->node->guzzleClient([
|
$response = $this->server->guzzleClient()->request('PUT', '/server/command', [
|
||||||
'X-Access-Token' => $this->server->daemonSecret,
|
'http_errors' => false,
|
||||||
'X-Access-Server' => $this->server->uuid,
|
'json' => [
|
||||||
])->request('POST', '/server/command', ['json' => ['command' => $command]]);
|
'command' => $command,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
|
||||||
throw new DisplayException('Command sending responded with a non-200 error code.');
|
throw new DisplayException('Command sending responded with a non-200 error code (HTTP/' . $response->getStatusCode() . ').');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response->getBody();
|
return $response->getBody();
|
||||||
} catch (\Exception $ex) {
|
} catch (ConnectException $ex) {
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
namespace Pterodactyl\Repositories\Daemon;
|
namespace Pterodactyl\Repositories\Daemon;
|
||||||
|
|
||||||
use Pterodactyl\Models;
|
use Pterodactyl\Models;
|
||||||
|
use GuzzleHttp\Exception\ConnectException;
|
||||||
use Pterodactyl\Exceptions\DisplayException;
|
use Pterodactyl\Exceptions\DisplayException;
|
||||||
|
|
||||||
class PowerRepository
|
class PowerRepository
|
||||||
|
@ -60,20 +61,20 @@ class PowerRepository
|
||||||
{
|
{
|
||||||
// We don't use the user's specific daemon secret here since we
|
// We don't use the user's specific daemon secret here since we
|
||||||
// are assuming that a call to this function has been validated.
|
// are assuming that a call to this function has been validated.
|
||||||
// Additionally not all calls to this will be from a logged in user.
|
|
||||||
// (e.g. task queue or API)
|
|
||||||
try {
|
try {
|
||||||
$response = $this->server->node->guzzleClient([
|
$response = $this->server->guzzleClient()->request('PUT', '/server/power', [
|
||||||
'X-Access-Token' => $this->server->daemonSecret,
|
'http_errors' => false,
|
||||||
'X-Access-Server' => $this->server->uuid,
|
'json' => [
|
||||||
])->request('PUT', '/server/power', ['json' => ['action' => $action]]);
|
'action' => $action,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
|
||||||
throw new DisplayException('Power status responded with a non-200 error code.');
|
throw new DisplayException('Power toggle endpoint responded with a non-200 error code (HTTP/' . $response->getStatusCode() . ').');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $response->getBody();
|
return $response->getBody();
|
||||||
} catch (\Exception $ex) {
|
} catch (ConnectException $ex) {
|
||||||
throw $ex;
|
throw $ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue