Skip to content

Commit 6e5c365

Browse files
committed
Use the client API to load servers on the listing page
1 parent ad69193 commit 6e5c365

File tree

8 files changed

+98
-9
lines changed

8 files changed

+98
-9
lines changed

app/Http/Controllers/Api/Client/ClientController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public function __construct(ServerRepositoryInterface $repository)
3535
*/
3636
public function index(GetServersRequest $request): array
3737
{
38-
$servers = $this->repository->filterUserAccessServers($request->user(), User::FILTER_LEVEL_SUBUSER);
38+
$servers = $this->repository
39+
->setSearchTerm($request->input('query'))
40+
->filterUserAccessServers($request->user(), User::FILTER_LEVEL_ALL);
3941

4042
return $this->fractal->collection($servers)
4143
->transformWith($this->getTransformer(ServerTransformer::class))

app/Http/Middleware/Api/AuthenticateKey.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Pterodactyl\Http\Middleware\Api;
44

55
use Closure;
6+
use Lcobucci\JWT\Parser;
67
use Cake\Chronos\Chronos;
78
use Illuminate\Http\Request;
89
use Pterodactyl\Models\ApiKey;
@@ -64,6 +65,22 @@ public function handle(Request $request, Closure $next, int $keyType)
6465

6566
$raw = $request->bearerToken();
6667

68+
// This is an internal JWT, treat it differently to get the correct user
69+
// before passing it along.
70+
if (strlen($raw) > ApiKey::IDENTIFIER_LENGTH + ApiKey::KEY_LENGTH) {
71+
$token = (new Parser)->parse($raw);
72+
73+
$model = (new ApiKey)->fill([
74+
'user_id' => $token->getClaim('uid'),
75+
'key_type' => ApiKey::TYPE_ACCOUNT,
76+
]);
77+
78+
$this->auth->guard()->loginUsingId($token->getClaim('uid'));
79+
$request->attributes->set('api_key', $model);
80+
81+
return $next($request);
82+
}
83+
6784
$identifier = substr($raw, 0, ApiKey::IDENTIFIER_LENGTH);
6885
$token = substr($raw, ApiKey::IDENTIFIER_LENGTH);
6986

app/Http/Middleware/Api/SetSessionDriver.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use Illuminate\Http\Request;
7-
use Barryvdh\Debugbar\LaravelDebugbar;
87
use Illuminate\Contracts\Foundation\Application;
98
use Illuminate\Contracts\Config\Repository as ConfigRepository;
109

@@ -41,10 +40,6 @@ public function __construct(Application $app, ConfigRepository $config)
4140
*/
4241
public function handle(Request $request, Closure $next)
4342
{
44-
if ($this->config->get('app.debug')) {
45-
$this->app->make(LaravelDebugbar::class)->disable();
46-
}
47-
4843
$this->config->set('session.driver', 'array');
4944

5045
return $next($request);

app/Transformers/Api/Client/ServerTransformer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function transform(Server $server): array
2929
'uuid' => $server->uuid,
3030
'name' => $server->name,
3131
'description' => $server->description,
32+
'allocation' => [
33+
'ip' => $server->allocation->alias,
34+
'port' => $server->allocation->port,
35+
],
3236
'limits' => [
3337
'memory' => $server->memory,
3438
'swap' => $server->swap,

resources/assets/scripts/bootstrap.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ window.axios = require('axios');
2121
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
2222
window.axios.defaults.headers.common['Authorization'] = 'Bearer ' + localStorage.token || '';
2323

24+
if (typeof phpdebugbar !== 'undefined') {
25+
window.axios.interceptors.response.use(function (response) {
26+
phpdebugbar.ajaxHandler.handle(response.request);
27+
28+
return response;
29+
});
30+
}
31+
2432
/**
2533
* Next we will register the CSRF Token as a common header with Axios so that
2634
* all outgoing HTTP requests automatically have it attached. This is just

resources/assets/scripts/components/dashboard/Dashboard.vue

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</div>
1010
<transition-group class="w-full m-auto mt-4 animate fadein sm:flex flex-wrap content-start">
1111
<div class="server-box" :key="index" v-for="(server, index) in servers">
12-
<router-link :to="{ name: 'server', params: { id: server.uuidShort }}" class="content">
12+
<router-link :to="{ name: 'server', params: { id: server.identifier }}" class="content">
1313
<div class="float-right">
1414
<div class="indicator"></div>
1515
</div>
@@ -49,6 +49,7 @@
4949
</template>
5050

5151
<script>
52+
import Server from '../../models/server';
5253
import _ from 'lodash';
5354
5455
export default {
@@ -73,11 +74,14 @@
7374
loadServers: function (query = '') {
7475
const self = this;
7576
76-
window.axios.get(this.route('dashboard.servers'), {
77+
window.axios.get(this.route('api.client.index'), {
7778
params: { query },
7879
})
7980
.then(function (response) {
80-
self.servers = response.data;
81+
self.servers = [];
82+
response.data.data.forEach(function (obj) {
83+
self.servers.push(new Server().fill(obj.attributes))
84+
});
8185
})
8286
.catch(function (error) {
8387
console.error(error);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const Allocation = function () {
2+
this.ip = null;
3+
this.port = null;
4+
};
5+
6+
/**
7+
* Return a new allocation model.
8+
*
9+
* @param obj
10+
* @returns {Allocation}
11+
*/
12+
Allocation.prototype.fill = function (obj) {
13+
this.ip = obj.ip || null;
14+
this.port = obj.port || null;
15+
16+
return this;
17+
};
18+
19+
export default Allocation;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Allocation from './allocation';
2+
3+
const Server = function () {
4+
this.identifier = null;
5+
this.uuid = null;
6+
this.name = '';
7+
this.description = '';
8+
this.allocation = null;
9+
this.limits = {
10+
memory: 0,
11+
swap: 0,
12+
disk: 0,
13+
io: 0,
14+
cpu: 0,
15+
};
16+
this.feature_limits = {
17+
databases: 0,
18+
allocations: 0,
19+
};
20+
};
21+
22+
/**
23+
* Return a new server model filled with data from the provided object.
24+
*
25+
* @param {object} obj
26+
* @returns {Server}
27+
*/
28+
Server.prototype.fill = function (obj) {
29+
this.identifier = obj.identifier;
30+
this.uuid = obj.uuid;
31+
this.name = obj.name;
32+
this.description = obj.description;
33+
this.allocation = new Allocation().fill(obj.allocation || {});
34+
this.limits = obj.limits;
35+
this.feature_limits = obj.feature_limits;
36+
37+
return this;
38+
};
39+
40+
export default Server;

0 commit comments

Comments
 (0)