Path: blob/1.0-develop/app/Transformers/Api/Client/ServerTransformer.php
10284 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' => $server->uuidShort,41'internal_id' => $server->id,42'uuid' => $server->uuid,43'name' => $server->name,44'node' => $server->node->name,45'is_node_under_maintenance' => $server->node->isUnderMaintenance(),46'sftp_details' => [47'ip' => $server->node->fqdn,48'port' => $server->node->daemonSFTP,49],50'description' => $server->description,51'limits' => [52'memory' => $server->memory,53'swap' => $server->swap,54'disk' => $server->disk,55'io' => $server->io,56'cpu' => $server->cpu,57'threads' => $server->threads,58'oom_disabled' => $server->oom_disabled,59],60'invocation' => $service->handle($server, !$user->can(Permission::ACTION_STARTUP_READ, $server)),61'docker_image' => $server->image,62'egg_features' => $server->egg->inherit_features,63'feature_limits' => [64'databases' => $server->database_limit,65'allocations' => $server->allocation_limit,66'backups' => $server->backup_limit,67],68'status' => $server->status,69// This field is deprecated, please use "status".70'is_suspended' => $server->isSuspended(),71// This field is deprecated, please use "status".72'is_installing' => !$server->isInstalled(),73'is_transferring' => !is_null($server->transfer),74];75}7677/**78* Returns the allocations associated with this server.79*80* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException81*/82public function includeAllocations(Server $server): Collection83{84$transformer = $this->makeTransformer(AllocationTransformer::class);8586$user = $this->request->user();87// While we include this permission, we do need to actually handle it slightly different here88// for the purpose of keeping things functionally working. If the user doesn't have read permissions89// for the allocations we'll only return the primary server allocation, and any notes associated90// with it will be hidden.91//92// This allows us to avoid too much permission regression, without also hiding information that93// is generally needed for the frontend to make sense when browsing or searching results.94if (!$user->can(Permission::ACTION_ALLOCATION_READ, $server)) {95$primary = clone $server->allocation;96$primary->notes = null;9798return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);99}100101return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);102}103104/**105* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException106*/107public function includeVariables(Server $server): Collection|NullResource108{109if (!$this->request->user()->can(Permission::ACTION_STARTUP_READ, $server)) {110return $this->null();111}112113return $this->collection(114$server->variables->where('user_viewable', true),115$this->makeTransformer(EggVariableTransformer::class),116EggVariable::RESOURCE_NAME117);118}119120/**121* Returns the egg associated with this server.122*123* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException124*/125public function includeEgg(Server $server): Item126{127return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);128}129130/**131* Returns the subusers associated with this server.132*133* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException134*/135public function includeSubusers(Server $server): Collection|NullResource136{137if (!$this->request->user()->can(Permission::ACTION_USER_READ, $server)) {138return $this->null();139}140141return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);142}143}144145146