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
10284 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' => $server->uuidShort,
42
'internal_id' => $server->id,
43
'uuid' => $server->uuid,
44
'name' => $server->name,
45
'node' => $server->node->name,
46
'is_node_under_maintenance' => $server->node->isUnderMaintenance(),
47
'sftp_details' => [
48
'ip' => $server->node->fqdn,
49
'port' => $server->node->daemonSFTP,
50
],
51
'description' => $server->description,
52
'limits' => [
53
'memory' => $server->memory,
54
'swap' => $server->swap,
55
'disk' => $server->disk,
56
'io' => $server->io,
57
'cpu' => $server->cpu,
58
'threads' => $server->threads,
59
'oom_disabled' => $server->oom_disabled,
60
],
61
'invocation' => $service->handle($server, !$user->can(Permission::ACTION_STARTUP_READ, $server)),
62
'docker_image' => $server->image,
63
'egg_features' => $server->egg->inherit_features,
64
'feature_limits' => [
65
'databases' => $server->database_limit,
66
'allocations' => $server->allocation_limit,
67
'backups' => $server->backup_limit,
68
],
69
'status' => $server->status,
70
// This field is deprecated, please use "status".
71
'is_suspended' => $server->isSuspended(),
72
// This field is deprecated, please use "status".
73
'is_installing' => !$server->isInstalled(),
74
'is_transferring' => !is_null($server->transfer),
75
];
76
}
77
78
/**
79
* Returns the allocations associated with this server.
80
*
81
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
82
*/
83
public function includeAllocations(Server $server): Collection
84
{
85
$transformer = $this->makeTransformer(AllocationTransformer::class);
86
87
$user = $this->request->user();
88
// While we include this permission, we do need to actually handle it slightly different here
89
// for the purpose of keeping things functionally working. If the user doesn't have read permissions
90
// for the allocations we'll only return the primary server allocation, and any notes associated
91
// with it will be hidden.
92
//
93
// This allows us to avoid too much permission regression, without also hiding information that
94
// is generally needed for the frontend to make sense when browsing or searching results.
95
if (!$user->can(Permission::ACTION_ALLOCATION_READ, $server)) {
96
$primary = clone $server->allocation;
97
$primary->notes = null;
98
99
return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);
100
}
101
102
return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);
103
}
104
105
/**
106
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
107
*/
108
public function includeVariables(Server $server): Collection|NullResource
109
{
110
if (!$this->request->user()->can(Permission::ACTION_STARTUP_READ, $server)) {
111
return $this->null();
112
}
113
114
return $this->collection(
115
$server->variables->where('user_viewable', true),
116
$this->makeTransformer(EggVariableTransformer::class),
117
EggVariable::RESOURCE_NAME
118
);
119
}
120
121
/**
122
* Returns the egg associated with this server.
123
*
124
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
125
*/
126
public function includeEgg(Server $server): Item
127
{
128
return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
129
}
130
131
/**
132
* Returns the subusers associated with this server.
133
*
134
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
135
*/
136
public function includeSubusers(Server $server): Collection|NullResource
137
{
138
if (!$this->request->user()->can(Permission::ACTION_USER_READ, $server)) {
139
return $this->null();
140
}
141
142
return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);
143
}
144
}
145
146