From 6e5c365018e51d17641f96445bf2974f67d5c74f Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 28 May 2018 13:23:40 -0700 Subject: [PATCH] Use the client API to load servers on the listing page --- .../Api/Client/ClientController.php | 4 +- app/Http/Middleware/Api/AuthenticateKey.php | 17 ++++++++ app/Http/Middleware/Api/SetSessionDriver.php | 5 --- .../Api/Client/ServerTransformer.php | 4 ++ resources/assets/scripts/bootstrap.js | 8 ++++ .../components/dashboard/Dashboard.vue | 10 +++-- resources/assets/scripts/models/allocation.js | 19 +++++++++ resources/assets/scripts/models/server.js | 40 +++++++++++++++++++ 8 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 resources/assets/scripts/models/allocation.js create mode 100644 resources/assets/scripts/models/server.js diff --git a/app/Http/Controllers/Api/Client/ClientController.php b/app/Http/Controllers/Api/Client/ClientController.php index d2e1f33a9..62b5d2c4a 100644 --- a/app/Http/Controllers/Api/Client/ClientController.php +++ b/app/Http/Controllers/Api/Client/ClientController.php @@ -35,7 +35,9 @@ class ClientController extends ClientApiController */ public function index(GetServersRequest $request): array { - $servers = $this->repository->filterUserAccessServers($request->user(), User::FILTER_LEVEL_SUBUSER); + $servers = $this->repository + ->setSearchTerm($request->input('query')) + ->filterUserAccessServers($request->user(), User::FILTER_LEVEL_ALL); return $this->fractal->collection($servers) ->transformWith($this->getTransformer(ServerTransformer::class)) diff --git a/app/Http/Middleware/Api/AuthenticateKey.php b/app/Http/Middleware/Api/AuthenticateKey.php index 774fb930b..3ae04f6fe 100644 --- a/app/Http/Middleware/Api/AuthenticateKey.php +++ b/app/Http/Middleware/Api/AuthenticateKey.php @@ -3,6 +3,7 @@ namespace Pterodactyl\Http\Middleware\Api; use Closure; +use Lcobucci\JWT\Parser; use Cake\Chronos\Chronos; use Illuminate\Http\Request; use Pterodactyl\Models\ApiKey; @@ -64,6 +65,22 @@ class AuthenticateKey $raw = $request->bearerToken(); + // This is an internal JWT, treat it differently to get the correct user + // before passing it along. + if (strlen($raw) > ApiKey::IDENTIFIER_LENGTH + ApiKey::KEY_LENGTH) { + $token = (new Parser)->parse($raw); + + $model = (new ApiKey)->fill([ + 'user_id' => $token->getClaim('uid'), + 'key_type' => ApiKey::TYPE_ACCOUNT, + ]); + + $this->auth->guard()->loginUsingId($token->getClaim('uid')); + $request->attributes->set('api_key', $model); + + return $next($request); + } + $identifier = substr($raw, 0, ApiKey::IDENTIFIER_LENGTH); $token = substr($raw, ApiKey::IDENTIFIER_LENGTH); diff --git a/app/Http/Middleware/Api/SetSessionDriver.php b/app/Http/Middleware/Api/SetSessionDriver.php index 3d5c16617..e61604dbd 100644 --- a/app/Http/Middleware/Api/SetSessionDriver.php +++ b/app/Http/Middleware/Api/SetSessionDriver.php @@ -4,7 +4,6 @@ namespace Pterodactyl\Http\Middleware\Api; use Closure; use Illuminate\Http\Request; -use Barryvdh\Debugbar\LaravelDebugbar; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Config\Repository as ConfigRepository; @@ -41,10 +40,6 @@ class SetSessionDriver */ public function handle(Request $request, Closure $next) { - if ($this->config->get('app.debug')) { - $this->app->make(LaravelDebugbar::class)->disable(); - } - $this->config->set('session.driver', 'array'); return $next($request); diff --git a/app/Transformers/Api/Client/ServerTransformer.php b/app/Transformers/Api/Client/ServerTransformer.php index 6816d6d74..c44630b5c 100644 --- a/app/Transformers/Api/Client/ServerTransformer.php +++ b/app/Transformers/Api/Client/ServerTransformer.php @@ -29,6 +29,10 @@ class ServerTransformer extends BaseClientTransformer 'uuid' => $server->uuid, 'name' => $server->name, 'description' => $server->description, + 'allocation' => [ + 'ip' => $server->allocation->alias, + 'port' => $server->allocation->port, + ], 'limits' => [ 'memory' => $server->memory, 'swap' => $server->swap, diff --git a/resources/assets/scripts/bootstrap.js b/resources/assets/scripts/bootstrap.js index 24ea8c7f0..562a7adf3 100644 --- a/resources/assets/scripts/bootstrap.js +++ b/resources/assets/scripts/bootstrap.js @@ -21,6 +21,14 @@ window.axios = require('axios'); window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; window.axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.token || ''; +if (typeof phpdebugbar !== 'undefined') { + window.axios.interceptors.response.use(function (response) { + phpdebugbar.ajaxHandler.handle(response.request); + + return response; + }); +} + /** * Next we will register the CSRF Token as a common header with Axios so that * all outgoing HTTP requests automatically have it attached. This is just diff --git a/resources/assets/scripts/components/dashboard/Dashboard.vue b/resources/assets/scripts/components/dashboard/Dashboard.vue index f12f2fd02..5ee5199ce 100644 --- a/resources/assets/scripts/components/dashboard/Dashboard.vue +++ b/resources/assets/scripts/components/dashboard/Dashboard.vue @@ -9,7 +9,7 @@
- +
@@ -49,6 +49,7 @@