Skip to content

Commit 6d2ffab

Browse files
authored
Merge pull request pterodactyl#286 from Pterodactyl/feature/updated-models
Implementation of improved Model logic and relationships throughout code base.
2 parents 7aa4608 + 2fdde75 commit 6d2ffab

File tree

93 files changed

+1697
-1506
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+1697
-1506
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
2121
* Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields.
2222
* User model now defines mass assignment fields using `$fillable` rather than `$guarded`.
2323
* 2FA checkpoint on login is now its own page, and not an AJAX based call. Improves security on that front.
24+
* Updated Server model code to be more efficient, as well as make life easier for backend changes and work.
25+
26+
### Deprecated
27+
* `Server::getUserDaemonSecret(Server $server)` was removed and replaced with `User::daemonSecret(Server $server)` in order to clean up models.
28+
* `Server::getByUUID()` was replaced with `Server::byUuid()` as well as various other functions through-out the Server model.
29+
* `Server::getHeaders()` was removed and replaced with `Server::getClient()` which returns a Guzzle Client with the correct headers already assigned.
2430

2531
## v0.5.6 (Bodacious Boreopterus)
2632
### Added

app/Http/Controllers/API/LocationController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace Pterodactyl\Http\Controllers\API;
2626

27-
use DB;
2827
use Illuminate\Http\Request;
2928
use Pterodactyl\Models\Location;
3029

@@ -49,11 +48,12 @@ public function __construct()
4948
*/
5049
public function lists(Request $request)
5150
{
52-
return Location::select('locations.*', DB::raw('GROUP_CONCAT(nodes.id) as nodes'))
53-
->join('nodes', 'locations.id', '=', 'nodes.location')
54-
->groupBy('locations.id')
55-
->get()->each(function ($location) {
56-
$location->nodes = explode(',', $location->nodes);
57-
})->all();
51+
return Location::with('nodes')->get()->map(function ($item) {
52+
$item->nodes->transform(function ($item) {
53+
return collect($item)->only(['id', 'name', 'fqdn', 'scheme', 'daemonListen', 'daemonSFTP']);
54+
});
55+
56+
return $item;
57+
})->toArray();
5858
}
5959
}

app/Http/Controllers/API/NodeController.php

Lines changed: 29 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
namespace Pterodactyl\Http\Controllers\API;
2626

27+
use Log;
2728
use Pterodactyl\Models;
2829
use Illuminate\Http\Request;
2930
use Dingo\Api\Exception\ResourceException;
@@ -96,15 +97,21 @@ public function lists(Request $request)
9697
public function create(Request $request)
9798
{
9899
try {
99-
$node = new NodeRepository;
100-
$new = $node->create($request->all());
101-
102-
return ['id' => $new];
100+
$repo = new NodeRepository;
101+
$node = $repo->create($request->only([
102+
'name', 'location_id', 'public', 'fqdn',
103+
'scheme', 'memory', 'memory_overallocate',
104+
'disk', 'disk_overallocate', 'daemonBase',
105+
'daemonSFTP', 'daemonListen',
106+
]));
107+
108+
return ['id' => $repo->id];
103109
} catch (DisplayValidationException $ex) {
104110
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
105111
} catch (DisplayException $ex) {
106112
throw new ResourceException($ex->getMessage());
107-
} catch (\Exception $e) {
113+
} catch (\Exception $ex) {
114+
Log::error($ex);
108115
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.');
109116
}
110117
}
@@ -124,88 +131,35 @@ public function create(Request $request)
124131
*/
125132
public function view(Request $request, $id, $fields = null)
126133
{
127-
$node = Models\Node::where('id', $id);
134+
$node = Models\Node::with('allocations')->where('id', $id)->first();
135+
if (! $node) {
136+
throw new NotFoundHttpException('No node by that ID was found.');
137+
}
138+
139+
$node->allocations->transform(function ($item) {
140+
return collect($item)->only([
141+
'id', 'ip', 'ip_alias', 'port', 'server_id',
142+
]);
143+
});
128144

129145
if (! is_null($request->input('fields'))) {
130-
foreach (explode(',', $request->input('fields')) as $field) {
131-
if (! empty($field)) {
132-
$node->addSelect($field);
133-
}
146+
$fields = explode(',', $request->input('fields'));
147+
if (! empty($fields) && is_array($fields)) {
148+
return collect($node)->only($fields);
134149
}
135150
}
136151

137-
try {
138-
if (! $node->first()) {
139-
throw new NotFoundHttpException('No node by that ID was found.');
140-
}
141-
142-
return [
143-
'node' => $node->first(),
144-
'allocations' => [
145-
'assigned' => Models\Allocation::where('node', $id)->whereNotNull('assigned_to')->get(),
146-
'unassigned' => Models\Allocation::where('node', $id)->whereNull('assigned_to')->get(),
147-
],
148-
];
149-
} catch (NotFoundHttpException $ex) {
150-
throw $ex;
151-
} catch (\Exception $ex) {
152-
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
153-
}
152+
return $node;
154153
}
155154

156155
public function config(Request $request, $id)
157156
{
158-
if (! $request->secure()) {
159-
throw new BadRequestHttpException('This API route can only be accessed using a secure connection.');
160-
}
161-
162157
$node = Models\Node::where('id', $id)->first();
163158
if (! $node) {
164159
throw new NotFoundHttpException('No node by that ID was found.');
165160
}
166161

167-
return [
168-
'web' => [
169-
'listen' => $node->daemonListen,
170-
'host' => '0.0.0.0',
171-
'ssl' => [
172-
'enabled' => ($node->scheme === 'https'),
173-
'certificate' => '/etc/certs/' . $node->fqdn . '/fullchain.pem',
174-
'key' => '/etc/certs/' . $node->fqdn . '/privkey.pem',
175-
],
176-
],
177-
'docker' => [
178-
'socket' => '/var/run/docker.sock',
179-
'autoupdate_images' => true,
180-
],
181-
'sftp' => [
182-
'path' => $node->daemonBase,
183-
'port' => (int) $node->daemonSFTP,
184-
'container' => 'ptdl-sftp',
185-
],
186-
'query' => [
187-
'kill_on_fail' => true,
188-
'fail_limit' => 5,
189-
],
190-
'logger' => [
191-
'path' => 'logs/',
192-
'src' => false,
193-
'level' => 'info',
194-
'period' => '1d',
195-
'count' => 3,
196-
],
197-
'remote' => [
198-
'base' => config('app.url'),
199-
'download' => route('remote.download'),
200-
'installed' => route('remote.install'),
201-
],
202-
'uploads' => [
203-
'size_limit' => $node->upload_size,
204-
],
205-
'keys' => [
206-
$node->daemonSecret,
207-
],
208-
];
162+
return $node->getConfigurationAsJson();
209163
}
210164

211165
/**
@@ -219,12 +173,7 @@ public function config(Request $request, $id)
219173
*/
220174
public function allocations(Request $request)
221175
{
222-
$allocations = Models\Allocation::all();
223-
if ($allocations->count() < 1) {
224-
throw new NotFoundHttpException('No allocations have been created.');
225-
}
226-
227-
return $allocations;
176+
return Models\Allocation::all()->toArray();
228177
}
229178

230179
/**
@@ -238,18 +187,7 @@ public function allocations(Request $request)
238187
*/
239188
public function allocationsView(Request $request, $id)
240189
{
241-
$query = Models\Allocation::where('assigned_to', $id)->get();
242-
try {
243-
if (empty($query)) {
244-
throw new NotFoundHttpException('No allocations for that server were found.');
245-
}
246-
247-
return $query;
248-
} catch (NotFoundHttpException $ex) {
249-
throw $ex;
250-
} catch (\Exception $ex) {
251-
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
252-
}
190+
return Models\Allocation::where('assigned_to', $id)->get()->toArray();
253191
}
254192

255193
/**

app/Http/Controllers/API/ServerController.php

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ public function lists(Request $request)
7272
public function create(Request $request)
7373
{
7474
try {
75-
$server = new ServerRepository;
76-
$new = $server->create($request->all());
75+
$repo = new ServerRepository;
76+
$server = $repo->create($request->all());
7777

78-
return ['id' => $new];
78+
return ['id' => $server->id];
7979
} catch (DisplayValidationException $ex) {
8080
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
8181
} catch (DisplayException $ex) {
@@ -101,58 +101,38 @@ public function create(Request $request)
101101
*/
102102
public function view(Request $request, $id)
103103
{
104-
$query = Models\Server::where('id', $id);
104+
$server = Models\Server::with('node', 'allocations', 'pack')->where('id', $id)->first();
105+
if (! $server) {
106+
throw new NotFoundHttpException('No server by that ID was found.');
107+
}
105108

106109
if (! is_null($request->input('fields'))) {
107-
foreach (explode(',', $request->input('fields')) as $field) {
108-
if (! empty($field)) {
109-
$query->addSelect($field);
110-
}
110+
$fields = explode(',', $request->input('fields'));
111+
if (! empty($fields) && is_array($fields)) {
112+
return collect($server)->only($fields);
111113
}
112114
}
113115

114-
try {
115-
if (! $query->first()) {
116-
throw new NotFoundHttpException('No server by that ID was found.');
117-
}
118-
119-
// Requested Daemon Stats
120-
$server = $query->with(
121-
'allocations',
122-
'pack'
123-
)->first();
124-
if ($request->input('daemon') === 'true') {
125-
$node = Models\Node::findOrFail($server->node);
126-
$client = Models\Node::guzzleRequest($node->id);
116+
if ($request->input('daemon') === 'true') {
117+
try {
118+
$response = $server->node->guzzleClient([
119+
'X-Access-Token' => $server->node->daemonSecret,
120+
])->request('GET', '/servers');
127121

128-
$response = $client->request('GET', '/servers', [
129-
'headers' => [
130-
'X-Access-Token' => $node->daemonSecret,
131-
],
132-
]);
133-
134-
// Only return the daemon token if the request is using HTTPS
135-
if ($request->secure()) {
136-
$server->daemon_token = $server->daemonSecret;
137-
}
138122
$server->daemon = json_decode($response->getBody())->{$server->uuid};
139-
140-
return $server->toArray();
123+
} catch (\GuzzleHttp\Exception\TransferException $ex) {
124+
// Couldn't hit the daemon, return what we have though.
125+
$server->daemon = [
126+
'error' => 'There was an error encountered while attempting to connect to the remote daemon.',
127+
];
141128
}
129+
}
142130

143-
return $server->toArray();
144-
} catch (NotFoundHttpException $ex) {
145-
throw $ex;
146-
} catch (\GuzzleHttp\Exception\TransferException $ex) {
147-
// Couldn't hit the daemon, return what we have though.
148-
$server->daemon = [
149-
'error' => 'There was an error encountered while attempting to connect to the remote daemon.',
150-
];
131+
$server->allocations->transform(function ($item) {
132+
return collect($item)->except(['created_at', 'updated_at']);
133+
});
151134

152-
return $server->toArray();
153-
} catch (\Exception $ex) {
154-
throw new BadRequestHttpException('There was an issue with the fields passed in the request.');
155-
}
135+
return $server->toArray();
156136
}
157137

158138
/**
@@ -179,7 +159,9 @@ public function config(Request $request, $id)
179159
{
180160
try {
181161
$server = new ServerRepository;
182-
$server->updateDetails($id, $request->all());
162+
$server->updateDetails($id, $request->only([
163+
'owner', 'name', 'reset_token',
164+
]));
183165

184166
return Models\Server::findOrFail($id);
185167
} catch (DisplayValidationException $ex) {
@@ -224,7 +206,10 @@ public function build(Request $request, $id)
224206
{
225207
try {
226208
$server = new ServerRepository;
227-
$server->changeBuild($id, $request->all());
209+
$server->changeBuild($id, $request->only([
210+
'default', 'add_additional', 'remove_additional',
211+
'memory', 'swap', 'io', 'cpu', 'disk',
212+
]));
228213

229214
return Models\Server::findOrFail($id);
230215
} catch (DisplayValidationException $ex) {

app/Http/Controllers/API/ServiceController.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,11 @@ public function lists(Request $request)
4545

4646
public function view(Request $request, $id)
4747
{
48-
$service = Models\Service::find($id);
48+
$service = Models\Service::with('options.variables', 'options.packs')->find($id);
4949
if (! $service) {
5050
throw new NotFoundHttpException('No service by that ID was found.');
5151
}
5252

53-
return [
54-
'service' => $service,
55-
'options' => Models\ServiceOptions::select('id', 'name', 'description', 'tag', 'docker_image')
56-
->where('parent_service', $service->id)
57-
->with('variables')
58-
->with('packs')
59-
->get(),
60-
];
53+
return $service->toArray();
6154
}
6255
}

app/Http/Controllers/API/User/InfoController.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,23 @@
2424

2525
namespace Pterodactyl\Http\Controllers\API\User;
2626

27-
use Pterodactyl\Models;
2827
use Illuminate\Http\Request;
2928
use Pterodactyl\Http\Controllers\API\BaseController;
3029

3130
class InfoController extends BaseController
3231
{
3332
public function me(Request $request)
3433
{
35-
return Models\Server::getUserServers()->map(function ($server) {
34+
return $request->user()->serverAccessCollection()->load('allocation', 'option')->map(function ($server) {
3635
return [
3736
'id' => $server->uuidShort,
3837
'uuid' => $server->uuid,
3938
'name' => $server->name,
40-
'node' => $server->nodeName,
41-
'ip' => [
42-
'set' => $server->ip,
43-
'alias' => $server->ip_alias,
44-
],
45-
'port' => $server->port,
46-
'service' => $server->a_serviceName,
47-
'option' => $server->a_serviceOptionName,
39+
'node' => $server->node->name,
40+
'ip' => $server->allocation->alias,
41+
'port' => $server->allocation->port,
42+
'service' => $server->service->name,
43+
'option' => $server->option->name,
4844
];
4945
})->all();
5046
}

0 commit comments

Comments
 (0)