misc_pterodactyl-panel/app/Transformers/Api/Client/ServerTransformer.php

127 lines
4.5 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Server;
use League\Fractal\Resource\Item;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Permission;
use Illuminate\Container\Container;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
2022-12-15 00:05:46 +00:00
use Pterodactyl\Transformers\Api\Transformer;
use Pterodactyl\Services\Servers\StartupCommandService;
2022-12-15 00:05:46 +00:00
class ServerTransformer extends Transformer
{
2022-05-04 23:35:10 +00:00
protected array $defaultIncludes = ['allocations', 'variables'];
protected array $availableIncludes = ['egg', 'subusers'];
public function getResourceName(): string
{
return Server::RESOURCE_NAME;
}
/**
* Transform a server model into a representation that can be returned
* to a client.
*/
public function transform(Server $server): array
{
/** @var \Pterodactyl\Services\Servers\StartupCommandService $service */
$service = Container::getInstance()->make(StartupCommandService::class);
$user = $this->request->user();
return [
'server_owner' => $user->id === $server->owner_id,
'identifier' => $server->uuidShort,
2020-11-02 05:14:02 +00:00
'internal_id' => $server->id,
'uuid' => $server->uuid,
'name' => $server->name,
2018-05-28 21:11:23 +00:00
'node' => $server->node->name,
'is_node_under_maintenance' => $server->node->isUnderMaintenance(),
2019-12-07 23:58:37 +00:00
'sftp_details' => [
'ip' => $server->node->fqdn,
2022-12-15 01:08:50 +00:00
'port' => $server->node->public_port_sftp,
2019-12-07 23:58:37 +00:00
],
'description' => $server->description,
'limits' => [
'memory' => $server->memory,
'swap' => $server->swap,
'disk' => $server->disk,
'io' => $server->io,
'cpu' => $server->cpu,
'threads' => $server->threads,
'oom_disabled' => $server->oom_disabled,
],
'invocation' => $service->handle($server, !$user->can(Permission::ACTION_STARTUP_READ, $server)),
'docker_image' => $server->image,
'egg_features' => $server->egg->inherit_features,
'feature_limits' => [
'databases' => $server->database_limit,
'allocations' => $server->allocation_limit,
'backups' => $server->backup_limit,
],
'status' => $server->status,
2021-01-26 03:20:51 +00:00
'is_transferring' => !is_null($server->transfer),
];
}
/**
* Returns the allocations associated with this server.
*/
public function includeAllocations(Server $server): Collection
{
2022-12-15 00:05:46 +00:00
$transformer = new AllocationTransformer();
$user = $this->request->user();
// While we include this permission, we do need to actually handle it slightly different here
// for the purpose of keeping things functionally working. If the user doesn't have read permissions
// for the allocations we'll only return the primary server allocation, and any notes associated
// with it will be hidden.
//
// This allows us to avoid too much permission regression, without also hiding information that
// is generally needed for the frontend to make sense when browsing or searching results.
if (!$user->can(Permission::ACTION_ALLOCATION_READ, $server)) {
$primary = clone $server->allocation;
$primary->notes = null;
2022-12-15 00:05:46 +00:00
return $this->collection([$primary], $transformer);
}
2022-12-15 00:05:46 +00:00
return $this->collection($server->allocations, $transformer);
}
public function includeVariables(Server $server): Collection|NullResource
{
if (!$this->request->user()->can(Permission::ACTION_STARTUP_READ, $server)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->collection($server->variables->where('user_viewable', true), new EggVariableTransformer());
}
/**
* Returns the egg associated with this server.
*/
public function includeEgg(Server $server): Item
{
2022-12-15 00:05:46 +00:00
return $this->item($server->egg, new EggTransformer());
}
/**
* Returns the subusers associated with this server.
*/
public function includeSubusers(Server $server): Collection|NullResource
{
if (!$this->request->user()->can(Permission::ACTION_USER_READ, $server)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->collection($server->subusers, new SubuserTransformer());
}
}