Use the client API to load servers on the listing page

This commit is contained in:
Dane Everitt 2018-05-28 13:23:40 -07:00
parent ad69193ac0
commit 6e5c365018
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
8 changed files with 98 additions and 9 deletions

View File

@ -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))

View File

@ -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);

View File

@ -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);

View File

@ -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,

View File

@ -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

View File

@ -9,7 +9,7 @@
</div>
<transition-group class="w-full m-auto mt-4 animate fadein sm:flex flex-wrap content-start">
<div class="server-box" :key="index" v-for="(server, index) in servers">
<router-link :to="{ name: 'server', params: { id: server.uuidShort }}" class="content">
<router-link :to="{ name: 'server', params: { id: server.identifier }}" class="content">
<div class="float-right">
<div class="indicator"></div>
</div>
@ -49,6 +49,7 @@
</template>
<script>
import Server from '../../models/server';
import _ from 'lodash';
export default {
@ -73,11 +74,14 @@
loadServers: function (query = '') {
const self = this;
window.axios.get(this.route('dashboard.servers'), {
window.axios.get(this.route('api.client.index'), {
params: { query },
})
.then(function (response) {
self.servers = response.data;
self.servers = [];
response.data.data.forEach(function (obj) {
self.servers.push(new Server().fill(obj.attributes))
});
})
.catch(function (error) {
console.error(error);

View File

@ -0,0 +1,19 @@
const Allocation = function () {
this.ip = null;
this.port = null;
};
/**
* Return a new allocation model.
*
* @param obj
* @returns {Allocation}
*/
Allocation.prototype.fill = function (obj) {
this.ip = obj.ip || null;
this.port = obj.port || null;
return this;
};
export default Allocation;

View File

@ -0,0 +1,40 @@
import Allocation from './allocation';
const Server = function () {
this.identifier = null;
this.uuid = null;
this.name = '';
this.description = '';
this.allocation = null;
this.limits = {
memory: 0,
swap: 0,
disk: 0,
io: 0,
cpu: 0,
};
this.feature_limits = {
databases: 0,
allocations: 0,
};
};
/**
* Return a new server model filled with data from the provided object.
*
* @param {object} obj
* @returns {Server}
*/
Server.prototype.fill = function (obj) {
this.identifier = obj.identifier;
this.uuid = obj.uuid;
this.name = obj.name;
this.description = obj.description;
this.allocation = new Allocation().fill(obj.allocation || {});
this.limits = obj.limits;
this.feature_limits = obj.feature_limits;
return this;
};
export default Server;