Skip to content

Commit a7fdb96

Browse files
committed
support for changing allocation on frontend
1 parent fb77e23 commit a7fdb96

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

app/Http/Controllers/Server/AjaxController.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,38 @@ public function postSaveFile(Request $request, $uuid)
178178

179179
}
180180

181+
/**
182+
* [postSetConnection description]
183+
* @param Request $request
184+
* @param string $uuid
185+
* @return \Illuminate\Http\Response
186+
*/
187+
public function postSetConnection(Request $request, $uuid)
188+
{
189+
190+
$server = Server::getByUUID($uuid);
191+
$this->authorize('set-connection', $server);
192+
193+
try {
194+
195+
$repo = new Repositories\ServerRepository;
196+
$repo->changeBuild($server->id, [
197+
'default' => $request->input('connection'),
198+
]);
199+
return response('The default connection for this server has been updated. Please be aware that you will need to restart your server for this change to go into effect.');
200+
201+
} catch (\Exception $e) {
202+
if ($e instanceof \Pterodactyl\Exceptions\DisplayException || $e instanceof \Pterodactyl\Exceptions\DisplayValidationException) {
203+
return response()->json([
204+
'error' => $e->getMessage(),
205+
], 503);
206+
} else {
207+
Log::error($e);
208+
return response()->json([
209+
'error' => 'An unhandled exception occured while attemping to modify the default connection for this server.'
210+
], 503);
211+
}
212+
}
213+
}
214+
181215
}

app/Http/Routes/ServerRoutes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function map(Router $router) {
1818
// Ajax Routes
1919
$router->group(['prefix' => 'ajax'], function ($server) use ($router) {
2020
$router->get('status', [ 'uses' => 'Server\AjaxController@getStatus' ]);
21+
$router->post('set-connection', [ 'uses' => 'Server\AjaxController@postSetConnection' ]);
2122
$router->post('files/directory-list', [ 'uses' => 'Server\AjaxController@postDirectoryList' ]);
2223
$router->post('files/save', [ 'uses' => 'Server\AjaxController@postSaveFile' ]);
2324
});

app/Policies/ServerPolicy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,20 @@ public function downloadFiles(User $user, Server $server)
175175
return $user->permissions()->server($server)->permission('download-files')->exists();
176176
}
177177

178+
/**
179+
* Check if user has permission to change the default connection information.
180+
*
181+
* @param Pterodactyl\Models\User $user
182+
* @param Pterodactyl\Models\Server $server
183+
* @return boolean
184+
*/
185+
public function setConnection(User $user, Server $server)
186+
{
187+
if ($this->isOwner($user, $server)) {
188+
return true;
189+
}
190+
191+
return $user->permissions()->server($server)->permission('set-connection')->exists();
192+
}
193+
178194
}

resources/views/server/index.blade.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
<div class="panel-heading"></div>
8989
<div class="panel-body">
9090
<div class="alert alert-info">Below is a listing of all avaliable IPs and Ports for your service. To change the default connection address for your server, simply click on the one you would like to make default below.</div>
91-
<ul class="nav nav-pills nav-stacked">
91+
<ul class="nav nav-pills nav-stacked" id="conn_options">
9292
@foreach ($allocations as $allocation)
9393
<li role="presentation" @if($allocation->ip === $server->ip && $allocation->port === $server->port) class="active" @endif><a href="#/set-connnection/{{ $allocation->ip }}:{{ $allocation->port }}" data-action="set-connection" data-connection="{{ $allocation->ip }}:{{ $allocation->port }}">{{ $allocation->ip }} <span class="badge">{{ $allocation->port }}</span></a></li>
9494
@endforeach
@@ -292,8 +292,7 @@
292292
});
293293
}
294294
if({{ $server->cpu }} > 0) {
295-
CPUChart.series[ i + 1 ].addPoint(parseFloat(((proc.data.cpu.cores[i] / {{ $server->cpu }}) * 100).toFixed(3).toString())
296-
, true, true);
295+
CPUChart.series[ i + 1 ].addPoint(parseFloat(((proc.data.cpu.cores[i] / {{ $server->cpu }}) * 100).toFixed(3).toString()), true, true);
297296
} else {
298297
CPUChart.series[ i + 1 ].addPoint(proc.data.cpu.cores[i], true, true);
299298
}
@@ -378,6 +377,39 @@ function updatePlayerListVisibility(data) {
378377
}
379378
}
380379
380+
@can('set-connection', $server)
381+
// Send Request
382+
$('[data-action="set-connection"]').click(function (event) {
383+
event.preventDefault();
384+
var element = $(this);
385+
if (element.hasClass('active')) {
386+
return;
387+
}
388+
389+
$.ajax({
390+
method: 'POST',
391+
url: '/server/{{ $server->uuidShort }}/ajax/set-connection',
392+
data: {
393+
connection: element.data('connection')
394+
},
395+
headers: {
396+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
397+
}
398+
}).done(function (data) {
399+
$('#conn_options').find('li.active').removeClass('active');
400+
element.parent().addClass('active');
401+
alert(data);
402+
}).fail(function (jqXHR) {
403+
console.error(jqXHR);
404+
if (typeof jqXHR.responseJSON.error === 'undefined' || jqXHR.responseJSON.error === '') {
405+
return alert('An error occured while attempting to perform this action.');
406+
} else {
407+
return alert(jqXHR.responseJSON.error);
408+
}
409+
});
410+
});
411+
@endcan
412+
381413
@can('command', $server)
382414
// Send Command to Server
383415
$('#console_command').submit(function (event) {

0 commit comments

Comments
 (0)