Skip to content

Commit a6bc36a

Browse files
committed
add initial api management page
1 parent 09d9f2a commit a6bc36a

File tree

7 files changed

+169
-1
lines changed

7 files changed

+169
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Pterodactyl\Http\Controllers\Admin;
4+
5+
use Alert;
6+
use Log;
7+
use Pterodactyl\Models;
8+
9+
use Pterodactyl\Http\Controllers\Controller;
10+
use Illuminate\Http\Request;
11+
12+
class APIController extends Controller
13+
{
14+
15+
public function __construct()
16+
{
17+
//
18+
}
19+
20+
public function getIndex(Request $request)
21+
{
22+
$keys = Models\APIKey::all();
23+
foreach($keys as &$key) {
24+
$key->permissions = Models\APIPermission::where('key_id', $key->id)->get();
25+
}
26+
27+
return view('admin.api.index', [
28+
'keys' => $keys
29+
]);
30+
}
31+
32+
}

app/Http/Routes/AdminRoutes.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function map(Router $router) {
203203

204204
});
205205

206-
// Server Routes
206+
// Location Routes
207207
$router->group([
208208
'prefix' => 'admin/locations',
209209
'middleware' => [
@@ -218,6 +218,29 @@ public function map(Router $router) {
218218
]);
219219
});
220220

221+
// API Routes
222+
$router->group([
223+
'prefix' => 'admin/api',
224+
'middleware' => [
225+
'auth',
226+
'admin',
227+
'csrf'
228+
]
229+
], function () use ($router) {
230+
$router->get('/', [
231+
'as' => 'admin.api',
232+
'uses' => 'Admin\APIController@getIndex'
233+
]);
234+
$router->get('/new', [
235+
'as' => 'admin.api.new',
236+
'uses' => 'Admin\APIController@getNew'
237+
]);
238+
$router->delete('/revoke/{key?}', [
239+
'as' => 'admin.api.revoke',
240+
'uses' => 'Admin\APIController@deleteKey'
241+
]);
242+
});
243+
221244
}
222245

223246
}

app/Models/APIKey.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,18 @@ class APIKey extends Model
1414
*/
1515
protected $table = 'api_keys';
1616

17+
/**
18+
* The attributes excluded from the model's JSON form.
19+
*
20+
* @var array
21+
*/
22+
protected $hidden = ['secret'];
23+
24+
/**
25+
* Fields that are not mass assignable.
26+
*
27+
* @var array
28+
*/
29+
protected $guarded = ['id', 'created_at', 'updated_at'];
30+
1731
}

app/Models/APIPermission.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,12 @@ class APIPermission extends Model
1414
*/
1515
protected $table = 'api_permissions';
1616

17+
/**
18+
* Fields that are not mass assignable.
19+
*
20+
* @var array
21+
*/
22+
protected $guarded = ['id'];
23+
24+
1725
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
@extends('layouts.admin')
2+
3+
@section('title')
4+
API Management
5+
@endsection
6+
7+
@section('content')
8+
<div class="col-md-12">
9+
<ul class="breadcrumb">
10+
<li><a href="/admin">Admin Control</a></li>
11+
<li class="active">API Management</li>
12+
</ul>
13+
<h3>API Key Information</h3><hr />
14+
<table class="table table-bordered table-hover">
15+
<thead>
16+
<tr>
17+
<th>API Public Key</th>
18+
<th>Allowed IPs</th>
19+
<th>Permissions</th>
20+
<th class="text-center">Created</th>
21+
<th class="text-center"></th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
@foreach ($keys as $key)
26+
<tr>
27+
<td><code>{{ $key->public }}</code></td>
28+
<td>
29+
@if (is_null($key->allowed_ips))
30+
<code>*</code>
31+
@else
32+
@foreach(json_decode($key->allowed_ips) as $ip)
33+
<code style="line-height:2;">{{ $ip }}</code><br />
34+
@endforeach
35+
@endif
36+
</td>
37+
<td>
38+
@foreach(json_decode($key->permissions) as &$perm)
39+
<code style="line-height:2;">{{ $perm->permission }}</code><br />
40+
@endforeach
41+
</td>
42+
<td class="text-center">{{ $key->created_at }}</td>
43+
<td class="text-center"><a href="#delete" class="text-danger" data-action="delete" data-attr="{{ $key->public }}"><i class="fa fa-trash"></i></a></td>
44+
</tr>
45+
@endforeach
46+
</tbody>
47+
</table>
48+
<div class="well">
49+
<a href="{{ route('admin.api.new') }}"><button class="btn btn-success btn-sm">Create New API Key</button></a>
50+
</div>
51+
</div>
52+
<script>
53+
$(document).ready(function () {
54+
$('#sidebar_links').find("a[href='/admin/api']").addClass('active');
55+
$('[data-action="delete"]').click(function (event) {
56+
var self = $(this);
57+
event.preventDefault();
58+
swal({
59+
type: 'error',
60+
title: 'Revoke API Key',
61+
text: 'Once this API key is revoked any applications currently using it will stop working.',
62+
showCancelButton: true,
63+
allowOutsideClick: true,
64+
confirmButtonText: 'Revoke',
65+
confirmButtonColor: '#d9534f',
66+
}, function () {
67+
$.ajax({
68+
method: 'DELETE',
69+
url: '{{ route('admin.api.revoke') }}/' + self.data('attr'),
70+
headers: {
71+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
72+
}
73+
}).done(function (data) {
74+
swal({
75+
type: 'success',
76+
});
77+
self.parent().parent().slideUp();
78+
}).fail(function (jqXHR) {
79+
console.error(jqXHR);
80+
swal({
81+
type: 'error',
82+
title: 'Whoops!',
83+
text: 'An error occured while attempting to revoke this key.'
84+
});
85+
});
86+
});
87+
});
88+
});
89+
</script>
90+
@endsection

resources/views/admin/api/new.blade.php

Whitespace-only changes.

resources/views/layouts/admin.blade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<a href="#" class="list-group-item list-group-item-heading"><strong>Management</strong></a>
6262
<a href="/admin" id="sidenav_admin-index" class="list-group-item">Admin Index</a>
6363
<a href="/admin/settings" class="list-group-item">General Settings</a>
64+
<a href="/admin/api" class="list-group-item">API Management</a>
6465
</div>
6566
<div class="list-group">
6667
<a href="#" class="list-group-item list-group-item-heading"><strong>Account Management</strong></a>

0 commit comments

Comments
 (0)