Path: blob/1.0-develop/app/Transformers/Api/Client/ServerTransformer.php
14044 views
<?php12namespace Pterodactyl\Transformers\Api\Client;34use Pterodactyl\Models\Egg;5use Pterodactyl\Models\Server;6use Pterodactyl\Models\Subuser;7use League\Fractal\Resource\Item;8use Pterodactyl\Models\Allocation;9use Pterodactyl\Models\Permission;10use Illuminate\Container\Container;11use Pterodactyl\Models\EggVariable;12use League\Fractal\Resource\Collection;13use League\Fractal\Resource\NullResource;14use Pterodactyl\Services\Servers\StartupCommandService;1516class ServerTransformer extends BaseClientTransformer17{18protected array $defaultIncludes = ['allocations', 'variables'];1920protected array $availableIncludes = ['egg', 'subusers'];2122public function getResourceName(): string23{24return Server::RESOURCE_NAME;25}2627/**28* Transform a server model into a representation that can be returned29* to a client.30*/31public function transform(Server $server): array32{33/** @var StartupCommandService $service */34$service = Container::getInstance()->make(StartupCommandService::class);3536$user = $this->request->user();3738return [39'server_owner' => $user->id === $server->owner_id,40'identifier' => config('pterodactyl.features.new_server_identifiers')41? $server->identifier42: $server->uuidShort,43'__deprecated_uuid_short' => $server->uuidShort,44// In Pterodactyl 2.0 we'll be replacing `identifier` above with the actual45// "identifier" used internally. This is a completely different value compared46// to the current however, and would be quite a breaking change to URLs.47'server_identifier' => $server->identifier,48'internal_id' => $server->id,49'uuid' => $server->uuid,50'name' => $server->name,51'node' => $server->node->name,52'is_node_under_maintenance' => $server->node->isUnderMaintenance(),53'sftp_details' => [54'ip' => $server->node->fqdn,55'port' => $server->node->daemonSFTP,56],57'description' => $server->description,58'limits' => [59'memory' => $server->memory,60'swap' => $server->swap,61'disk' => $server->disk,62'io' => $server->io,63'cpu' => $server->cpu,64'threads' => $server->threads,65'oom_disabled' => $server->oom_disabled,66],67'invocation' => $service->handle($server, !$user->can(Permission::ACTION_STARTUP_READ, $server)),68'docker_image' => $server->image,69'egg_features' => $server->egg->inherit_features,70'feature_limits' => [71'databases' => $server->database_limit,72'allocations' => $server->allocation_limit,73'backups' => $server->backup_limit,74],75'status' => $server->status,76// This field is deprecated, please use "status".77'is_suspended' => $server->isSuspended(),78// This field is deprecated, please use "status".79'is_installing' => !$server->isInstalled(),80'is_transferring' => !is_null($server->transfer),81];82}8384/**85* Returns the allocations associated with this server.86*87* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException88*/89public function includeAllocations(Server $server): Collection90{91$transformer = $this->makeTransformer(AllocationTransformer::class);9293$user = $this->request->user();94// While we include this permission, we do need to actually handle it slightly different here95// for the purpose of keeping things functionally working. If the user doesn't have read permissions96// for the allocations we'll only return the primary server allocation, and any notes associated97// with it will be hidden.98//99// This allows us to avoid too much permission regression, without also hiding information that100// is generally needed for the frontend to make sense when browsing or searching results.101if (!$user->can(Permission::ACTION_ALLOCATION_READ, $server)) {102$primary = clone $server->allocation;103$primary->notes = null;104105return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);106}107108return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);109}110111/**112* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException113*/114public function includeVariables(Server $server): Collection|NullResource115{116if (!$this->request->user()->can(Permission::ACTION_STARTUP_READ, $server)) {117return $this->null();118}119120return $this->collection(121$server->variables->where('user_viewable', true),122$this->makeTransformer(EggVariableTransformer::class),123EggVariable::RESOURCE_NAME124);125}126127/**128* Returns the egg associated with this server.129*130* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException131*/132public function includeEgg(Server $server): Item133{134return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);135}136137/**138* Returns the subusers associated with this server.139*140* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException141*/142public function includeSubusers(Server $server): Collection|NullResource143{144if (!$this->request->user()->can(Permission::ACTION_USER_READ, $server)) {145return $this->null();146}147148return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);149}150}151152153