Skip to content

Commit 633bba6

Browse files
committed
Add support for external_id on servers, closes pterodactyl#975
1 parent f655188 commit 633bba6

File tree

11 files changed

+73
-15
lines changed

11 files changed

+73
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1010

1111
### Added
1212
* Adds ability to include egg variables on an API request.
13+
* Added `external_id` column to servers that allows for easier linking with external services such as WHMCS.
1314

1415
## v0.7.1 (Derelict Dermodactylus)
1516
### Fixed

app/Http/Controllers/Admin/ServersController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function viewDelete(Server $server)
406406
public function setDetails(Request $request, Server $server)
407407
{
408408
$this->detailsModificationService->handle($server, $request->only([
409-
'owner_id', 'name', 'description',
409+
'owner_id', 'external_id', 'name', 'description',
410410
]));
411411

412412
$this->alert->success(trans('admin/server.alerts.details_updated'))->flash();

app/Http/Requests/Api/Application/Servers/StoreServerRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function rules(): array
3131
$rules = Server::getCreateRules();
3232

3333
return [
34+
'external_id' => $rules['external_id'],
3435
'name' => $rules['name'],
3536
'description' => array_merge(['nullable'], $rules['description']),
3637
'user' => $rules['owner_id'],

app/Models/Server.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Server extends Model implements CleansAttributes, ValidableContract
5353
* @var array
5454
*/
5555
protected static $applicationRules = [
56+
'external_id' => 'sometimes',
5657
'owner_id' => 'required',
5758
'name' => 'required',
5859
'memory' => 'required',
@@ -74,6 +75,7 @@ class Server extends Model implements CleansAttributes, ValidableContract
7475
* @var array
7576
*/
7677
protected static $dataIntegrityRules = [
78+
'external_id' => 'nullable|string|between:1,191|unique:servers',
7779
'owner_id' => 'integer|exists:users,id',
7880
'name' => 'string|min:1|max:255',
7981
'node_id' => 'exists:nodes,id',
@@ -122,13 +124,14 @@ class Server extends Model implements CleansAttributes, ValidableContract
122124
* @var array
123125
*/
124126
protected $searchableColumns = [
125-
'name' => 50,
126-
'uuidShort' => 10,
127-
'uuid' => 10,
128-
'pack.name' => 5,
129-
'user.email' => 20,
130-
'user.username' => 20,
127+
'name' => 100,
128+
'uuid' => 80,
129+
'uuidShort' => 80,
130+
'external_id' => 50,
131+
'user.email' => 40,
132+
'user.username' => 30,
131133
'node.name' => 10,
134+
'pack.name' => 10,
132135
];
133136

134137
/**

app/Services/Servers/DetailsModificationService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function handle(Server $server, array $data)
6969
$this->connection->beginTransaction();
7070

7171
$response = $this->repository->setFreshModel($this->getUpdatedModel())->update($server->id, [
72+
'external_id' => array_get($data, 'external_id'),
7273
'owner_id' => array_get($data, 'owner_id'),
7374
'name' => array_get($data, 'name'),
7475
'description' => array_get($data, 'description') ?? '',

app/Services/Servers/ServerCreationService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ private function configureDeployment(array $data, DeploymentObject $deployment):
211211
private function createModel(array $data): Server
212212
{
213213
return $this->repository->create([
214+
'external_id' => array_get($data, 'external_id'),
214215
'uuid' => Uuid::uuid4()->toString(),
215216
'uuidShort' => str_random(8),
216217
'node_id' => array_get($data, 'node_id'),

app/Transformers/Api/Application/ServerTransformer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Pterodactyl\Transformers\Api\Application;
44

5-
use Cake\Chronos\Chronos;
65
use Pterodactyl\Models\Server;
76
use Pterodactyl\Services\Acl\Api\AdminAcl;
87
use Pterodactyl\Services\Servers\EnvironmentService;
@@ -63,6 +62,7 @@ public function transform(Server $server): array
6362
{
6463
return [
6564
'id' => $server->getKey(),
65+
'external_id' => $server->external_id,
6666
'uuid' => $server->uuid,
6767
'identifier' => $server->uuidShort,
6868
'name' => $server->name,
@@ -87,8 +87,8 @@ public function transform(Server $server): array
8787
'installed' => (int) $server->installed === 1,
8888
'environment' => $this->environmentService->handle($server),
8989
],
90-
'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $server->created_at)->setTimezone('UTC')->toIso8601String(),
91-
'updated_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $server->updated_at)->setTimezone('UTC')->toIso8601String(),
90+
$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),
91+
$server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),
9292
];
9393
}
9494

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddExternalIdColumnToServersTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('servers', function (Blueprint $table) {
17+
$table->string('external_id')->after('id')->nullable()->unique();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('servers', function (Blueprint $table) {
29+
$table->dropColumn('external_id');
30+
});
31+
}
32+
}

resources/themes/pterodactyl/admin/servers/view/details.blade.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,17 @@
4747
<form action="{{ route('admin.servers.view.details', $server->id) }}" method="POST">
4848
<div class="box-body">
4949
<div class="form-group">
50-
<label for="name" class="control-label">Server Name</label>
50+
<label for="name" class="control-label">Server Name <span class="field-required"></span></label>
5151
<input type="text" name="name" value="{{ old('name', $server->name) }}" class="form-control" />
5252
<p class="text-muted small">Character limits: <code>a-zA-Z0-9_-</code> and <code>[Space]</code> (max 35 characters).</p>
5353
</div>
5454
<div class="form-group">
55-
<label for="pUserId" class="control-label">Server Owner</label>
55+
<label for="external_id" class="control-label">External Identifier</label>
56+
<input type="text" name="external_id" value="{{ old('external_id', $server->external_id) }}" class="form-control" />
57+
<p class="text-muted small">Leave empty to not assign an external identifier for this server. The external ID should be unique to this server and not be in use by any other servers.</p>
58+
</div>
59+
<div class="form-group">
60+
<label for="pUserId" class="control-label">Server Owner <span class="field-required"></span></label>
5661
<select name="owner_id" class="form-control" id="pUserId">
5762
<option value="{{ $server->owner_id }}" selected>{{ $server->user->email }}</option>
5863
</select>

resources/themes/pterodactyl/admin/servers/view/index.blade.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@
4747
</div>
4848
<div class="box-body table-responsive no-padding">
4949
<table class="table table-hover">
50+
<tr>
51+
<td>Internal Identifier</td>
52+
<td><code>{{ $server->id }}</code></td>
53+
</tr>
54+
<tr>
55+
<td>External Identifier</td>
56+
@if(is_null($server->external_id))
57+
<td><span class="label label-default">Not Set</span></td>
58+
@else
59+
<td><code>{{ $server->external_id }}</code></td>
60+
@endif
61+
</tr>
5062
<tr>
5163
<td>UUID / Docker Container ID</td>
5264
<td><code>{{ $server->uuid }}</code></td>
@@ -127,7 +139,7 @@
127139
<div class="col-sm-12">
128140
<div class="small-box bg-gray">
129141
<div class="inner">
130-
<h3>{{ str_limit($server->user->username, 8) }}</h3>
142+
<h3>{{ str_limit($server->user->username, 16) }}</h3>
131143
<p>Server Owner</p>
132144
</div>
133145
<div class="icon"><i class="fa fa-user"></i></div>
@@ -139,7 +151,7 @@
139151
<div class="col-sm-12">
140152
<div class="small-box bg-gray">
141153
<div class="inner">
142-
<h3>{{ str_limit($server->node->name, 8) }}</h3>
154+
<h3>{{ str_limit($server->node->name, 16) }}</h3>
143155
<p>Server Node</p>
144156
</div>
145157
<div class="icon"><i class="fa fa-codepen"></i></div>

0 commit comments

Comments
 (0)