2018-02-28 03:28:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
2019-09-22 22:30:53 +00:00
|
|
|
use Pterodactyl\Models\Egg;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-12-28 20:03:19 +00:00
|
|
|
use Pterodactyl\Models\Subuser;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Item;
|
2020-07-09 04:04:45 +00:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2020-08-22 23:54:12 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2020-08-22 22:43:28 +00:00
|
|
|
use Illuminate\Container\Container;
|
|
|
|
use Pterodactyl\Models\EggVariable;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2020-08-22 22:43:28 +00:00
|
|
|
use Pterodactyl\Services\Servers\StartupCommandService;
|
2018-02-28 03:28:43 +00:00
|
|
|
|
|
|
|
class ServerTransformer extends BaseClientTransformer
|
|
|
|
{
|
2022-05-04 23:35:10 +00:00
|
|
|
protected array $defaultIncludes = ['allocations', 'variables'];
|
2020-07-10 02:17:24 +00:00
|
|
|
|
2022-05-04 23:11:42 +00:00
|
|
|
protected array $availableIncludes = ['egg', 'subusers'];
|
2019-09-22 22:30:53 +00:00
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
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
|
|
|
|
{
|
2020-08-22 22:43:28 +00:00
|
|
|
/** @var \Pterodactyl\Services\Servers\StartupCommandService $service */
|
|
|
|
$service = Container::getInstance()->make(StartupCommandService::class);
|
|
|
|
|
2022-05-22 19:37:39 +00:00
|
|
|
$user = $this->request->user();
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
return [
|
2022-05-22 19:37:39 +00:00
|
|
|
'server_owner' => $user->id === $server->owner_id,
|
2018-02-28 03:28:43 +00:00
|
|
|
'identifier' => $server->uuidShort,
|
2020-11-02 05:14:02 +00:00
|
|
|
'internal_id' => $server->id,
|
2018-02-28 03:28:43 +00:00
|
|
|
'uuid' => $server->uuid,
|
|
|
|
'name' => $server->name,
|
2018-05-28 21:11:23 +00:00
|
|
|
'node' => $server->node->name,
|
2019-12-07 23:58:37 +00:00
|
|
|
'sftp_details' => [
|
|
|
|
'ip' => $server->node->fqdn,
|
|
|
|
'port' => $server->node->daemonSFTP,
|
|
|
|
],
|
2018-02-28 03:28:43 +00:00
|
|
|
'description' => $server->description,
|
|
|
|
'limits' => [
|
|
|
|
'memory' => $server->memory,
|
|
|
|
'swap' => $server->swap,
|
|
|
|
'disk' => $server->disk,
|
|
|
|
'io' => $server->io,
|
|
|
|
'cpu' => $server->cpu,
|
2021-09-14 04:02:12 +00:00
|
|
|
'threads' => $server->threads,
|
|
|
|
'oom_disabled' => $server->oom_disabled,
|
2018-02-28 03:28:43 +00:00
|
|
|
],
|
2022-05-22 19:37:39 +00:00
|
|
|
'invocation' => $service->handle($server, !$user->can(Permission::ACTION_STARTUP_READ, $server)),
|
2020-12-13 19:07:29 +00:00
|
|
|
'docker_image' => $server->image,
|
2020-11-03 04:52:41 +00:00
|
|
|
'egg_features' => $server->egg->inherit_features,
|
2018-03-02 02:08:27 +00:00
|
|
|
'feature_limits' => [
|
|
|
|
'databases' => $server->database_limit,
|
|
|
|
'allocations' => $server->allocation_limit,
|
2020-04-26 19:12:29 +00:00
|
|
|
'backups' => $server->backup_limit,
|
2018-03-02 02:08:27 +00:00
|
|
|
],
|
2021-01-18 00:02:11 +00:00
|
|
|
'status' => $server->status,
|
|
|
|
// This field is deprecated, please use "status".
|
2021-01-17 23:51:56 +00:00
|
|
|
'is_suspended' => $server->isSuspended(),
|
2021-01-18 00:02:11 +00:00
|
|
|
// This field is deprecated, please use "status".
|
2021-01-26 03:20:51 +00:00
|
|
|
'is_installing' => !$server->isInstalled(),
|
|
|
|
'is_transferring' => !is_null($server->transfer),
|
2018-02-28 03:28:43 +00:00
|
|
|
];
|
|
|
|
}
|
2019-09-22 22:30:53 +00:00
|
|
|
|
2020-07-10 02:17:24 +00:00
|
|
|
/**
|
|
|
|
* Returns the allocations associated with this server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeAllocations(Server $server): Collection
|
2020-07-10 02:17:24 +00:00
|
|
|
{
|
2020-11-07 17:57:53 +00:00
|
|
|
$transformer = $this->makeTransformer(AllocationTransformer::class);
|
|
|
|
|
2022-05-22 19:37:39 +00:00
|
|
|
$user = $this->request->user();
|
2020-11-07 17:57:53 +00:00
|
|
|
// 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.
|
2022-05-22 19:37:39 +00:00
|
|
|
if (!$user->can(Permission::ACTION_ALLOCATION_READ, $server)) {
|
2020-11-07 17:57:53 +00:00
|
|
|
$primary = clone $server->allocation;
|
|
|
|
$primary->notes = null;
|
|
|
|
|
|
|
|
return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);
|
2020-08-22 23:54:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 17:57:53 +00:00
|
|
|
return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);
|
2020-07-10 02:17:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 22:43:28 +00:00
|
|
|
/**
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeVariables(Server $server): Collection|NullResource
|
2020-08-22 22:43:28 +00:00
|
|
|
{
|
2022-05-22 19:37:39 +00:00
|
|
|
if (!$this->request->user()->can(Permission::ACTION_STARTUP_READ, $server)) {
|
2020-08-22 23:54:12 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2020-08-22 22:43:28 +00:00
|
|
|
return $this->collection(
|
|
|
|
$server->variables->where('user_viewable', true),
|
|
|
|
$this->makeTransformer(EggVariableTransformer::class),
|
|
|
|
EggVariable::RESOURCE_NAME
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-22 22:30:53 +00:00
|
|
|
/**
|
|
|
|
* Returns the egg associated with this server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeEgg(Server $server): Item
|
2019-09-22 22:30:53 +00:00
|
|
|
{
|
|
|
|
return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
|
|
|
|
}
|
2019-12-28 20:03:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the subusers associated with this server.
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeSubusers(Server $server): Collection|NullResource
|
2019-12-28 20:03:19 +00:00
|
|
|
{
|
2022-05-22 19:37:39 +00:00
|
|
|
if (!$this->request->user()->can(Permission::ACTION_USER_READ, $server)) {
|
2020-08-22 23:54:12 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:03:19 +00:00
|
|
|
return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);
|
|
|
|
}
|
2018-02-28 03:28:43 +00:00
|
|
|
}
|