Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Transformers/Api/Client/ServerTransformer.php
14044 views
1
<?php
2
3
namespace Pterodactyl\Transformers\Api\Client;
4
5
use Pterodactyl\Models\Egg;
6
use Pterodactyl\Models\Server;
7
use Pterodactyl\Models\Subuser;
8
use League\Fractal\Resource\Item;
9
use Pterodactyl\Models\Allocation;
10
use Pterodactyl\Models\Permission;
11
use Illuminate\Container\Container;
12
use Pterodactyl\Models\EggVariable;
13
use League\Fractal\Resource\Collection;
14
use League\Fractal\Resource\NullResource;
15
use Pterodactyl\Services\Servers\StartupCommandService;
16
17
class ServerTransformer extends BaseClientTransformer
18
{
19
protected array $defaultIncludes = ['allocations', 'variables'];
20
21
protected array $availableIncludes = ['egg', 'subusers'];
22
23
public function getResourceName(): string
24
{
25
return Server::RESOURCE_NAME;
26
}
27
28
/**
29
* Transform a server model into a representation that can be returned
30
* to a client.
31
*/
32
public function transform(Server $server): array
33
{
34
/** @var StartupCommandService $service */
35
$service = Container::getInstance()->make(StartupCommandService::class);
36
37
$user = $this->request->user();
38
39
return [
40
'server_owner' => $user->id === $server->owner_id,
41
'identifier' => config('pterodactyl.features.new_server_identifiers')
42
? $server->identifier
43
: $server->uuidShort,
44
'__deprecated_uuid_short' => $server->uuidShort,
45
// In Pterodactyl 2.0 we'll be replacing `identifier` above with the actual
46
// "identifier" used internally. This is a completely different value compared
47
// to the current however, and would be quite a breaking change to URLs.
48
'server_identifier' => $server->identifier,
49
'internal_id' => $server->id,
50
'uuid' => $server->uuid,
51
'name' => $server->name,
52
'node' => $server->node->name,
53
'is_node_under_maintenance' => $server->node->isUnderMaintenance(),
54
'sftp_details' => [
55
'ip' => $server->node->fqdn,
56
'port' => $server->node->daemonSFTP,
57
],
58
'description' => $server->description,
59
'limits' => [
60
'memory' => $server->memory,
61
'swap' => $server->swap,
62
'disk' => $server->disk,
63
'io' => $server->io,
64
'cpu' => $server->cpu,
65
'threads' => $server->threads,
66
'oom_disabled' => $server->oom_disabled,
67
],
68
'invocation' => $service->handle($server, !$user->can(Permission::ACTION_STARTUP_READ, $server)),
69
'docker_image' => $server->image,
70
'egg_features' => $server->egg->inherit_features,
71
'feature_limits' => [
72
'databases' => $server->database_limit,
73
'allocations' => $server->allocation_limit,
74
'backups' => $server->backup_limit,
75
],
76
'status' => $server->status,
77
// This field is deprecated, please use "status".
78
'is_suspended' => $server->isSuspended(),
79
// This field is deprecated, please use "status".
80
'is_installing' => !$server->isInstalled(),
81
'is_transferring' => !is_null($server->transfer),
82
];
83
}
84
85
/**
86
* Returns the allocations associated with this server.
87
*
88
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
89
*/
90
public function includeAllocations(Server $server): Collection
91
{
92
$transformer = $this->makeTransformer(AllocationTransformer::class);
93
94
$user = $this->request->user();
95
// While we include this permission, we do need to actually handle it slightly different here
96
// for the purpose of keeping things functionally working. If the user doesn't have read permissions
97
// for the allocations we'll only return the primary server allocation, and any notes associated
98
// with it will be hidden.
99
//
100
// This allows us to avoid too much permission regression, without also hiding information that
101
// is generally needed for the frontend to make sense when browsing or searching results.
102
if (!$user->can(Permission::ACTION_ALLOCATION_READ, $server)) {
103
$primary = clone $server->allocation;
104
$primary->notes = null;
105
106
return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);
107
}
108
109
return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);
110
}
111
112
/**
113
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
114
*/
115
public function includeVariables(Server $server): Collection|NullResource
116
{
117
if (!$this->request->user()->can(Permission::ACTION_STARTUP_READ, $server)) {
118
return $this->null();
119
}
120
121
return $this->collection(
122
$server->variables->where('user_viewable', true),
123
$this->makeTransformer(EggVariableTransformer::class),
124
EggVariable::RESOURCE_NAME
125
);
126
}
127
128
/**
129
* Returns the egg associated with this server.
130
*
131
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
132
*/
133
public function includeEgg(Server $server): Item
134
{
135
return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
136
}
137
138
/**
139
* Returns the subusers associated with this server.
140
*
141
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
142
*/
143
public function includeSubusers(Server $server): Collection|NullResource
144
{
145
if (!$this->request->user()->can(Permission::ACTION_USER_READ, $server)) {
146
return $this->null();
147
}
148
149
return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);
150
}
151
}
152
153