2017-04-02 20:51:56 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-04-02 20:51:56 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Resource\Collection;
|
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-19 03:56:12 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2022-12-15 00:05:46 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Services\Servers\EnvironmentService;
|
2017-04-02 20:51:56 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
class ServerTransformer extends Transformer
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
private EnvironmentService $environmentService;
|
2018-01-20 03:47:06 +00:00
|
|
|
|
2017-04-02 20:51:56 +00:00
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*/
|
2022-05-04 23:11:42 +00:00
|
|
|
protected array $availableIncludes = [
|
2017-04-02 20:51:56 +00:00
|
|
|
'allocations',
|
|
|
|
'user',
|
|
|
|
'subusers',
|
2018-01-19 03:56:12 +00:00
|
|
|
'nest',
|
|
|
|
'egg',
|
2017-04-08 00:28:58 +00:00
|
|
|
'variables',
|
|
|
|
'location',
|
|
|
|
'node',
|
2019-08-31 08:29:44 +00:00
|
|
|
'databases',
|
2020-12-16 16:34:47 +00:00
|
|
|
'transfer',
|
2017-04-02 20:51:56 +00:00
|
|
|
];
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* Perform dependency injection.
|
|
|
|
*/
|
|
|
|
public function handle(EnvironmentService $environmentService)
|
|
|
|
{
|
|
|
|
$this->environmentService = $environmentService;
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Server::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-04-02 20:51:56 +00:00
|
|
|
/**
|
|
|
|
* Return a generic transformed server array.
|
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function transform(Server $model): array
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2018-01-20 03:47:06 +00:00
|
|
|
return [
|
2022-12-15 00:05:46 +00:00
|
|
|
'id' => $model->getKey(),
|
|
|
|
'external_id' => $model->external_id,
|
|
|
|
'uuid' => $model->uuid,
|
|
|
|
'identifier' => $model->uuidShort,
|
|
|
|
'name' => $model->name,
|
|
|
|
'description' => $model->description,
|
|
|
|
'status' => $model->status,
|
2018-01-20 03:47:06 +00:00
|
|
|
'limits' => [
|
2022-12-15 00:05:46 +00:00
|
|
|
'cpu' => $model->cpu,
|
|
|
|
'disk' => $model->disk,
|
|
|
|
'io' => $model->io,
|
|
|
|
'memory' => $model->memory,
|
|
|
|
'oom_disabled' => $model->oom_disabled,
|
|
|
|
'swap' => $model->swap,
|
|
|
|
'threads' => $model->threads,
|
2018-01-20 03:47:06 +00:00
|
|
|
],
|
2018-03-02 02:08:27 +00:00
|
|
|
'feature_limits' => [
|
2022-12-15 00:05:46 +00:00
|
|
|
'allocations' => $model->allocation_limit,
|
|
|
|
'backups' => $model->backup_limit,
|
|
|
|
'databases' => $model->database_limit,
|
2018-03-02 02:08:27 +00:00
|
|
|
],
|
2022-12-15 00:05:46 +00:00
|
|
|
'user_id' => $model->owner_id,
|
|
|
|
'node_id' => $model->node_id,
|
|
|
|
'allocation_id' => $model->allocation_id,
|
|
|
|
'nest_id' => $model->nest_id,
|
|
|
|
'egg_id' => $model->egg_id,
|
2018-01-20 03:47:06 +00:00
|
|
|
'container' => [
|
2022-12-15 00:05:46 +00:00
|
|
|
'startup' => $model->startup,
|
|
|
|
'image' => $model->image,
|
|
|
|
'environment' => $this->environmentService->handle($model),
|
2018-01-20 03:47:06 +00:00
|
|
|
],
|
2022-12-15 00:05:46 +00:00
|
|
|
'created_at' => self::formatTimestamp($model->created_at),
|
|
|
|
'updated_at' => self::formatTimestamp($model->updated_at),
|
2018-01-20 03:47:06 +00:00
|
|
|
];
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of allocations for this server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeAllocations(Server $server): Collection|NullResource
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->collection($server->allocations, new AllocationTransformer());
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of data about subusers for this server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeSubusers(Server $server): Collection|NullResource
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->collection($server->subusers, new SubuserTransformer());
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of data about subusers for this server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeUser(Server $server): Item|NullResource
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->item($server->user, new UserTransformer());
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
2017-04-08 00:28:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-19 03:56:12 +00:00
|
|
|
* Return a generic array with nest information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeNest(Server $server): Item|NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
|
2018-03-02 00:43:39 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->item($server->nest, new NestTransformer());
|
2018-03-02 00:43:39 +00:00
|
|
|
}
|
2017-04-08 00:28:58 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-31 08:29:44 +00:00
|
|
|
* Return a generic array with egg information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeEgg(Server $server): Item|NullResource
|
2017-04-08 00:28:58 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->item($server->egg, new EggTransformer());
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of data about subusers for this server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeVariables(Server $server): Collection|NullResource
|
2017-04-08 00:28:58 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->collection($server->variables, new ServerVariableTransformer());
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-13 18:13:37 +00:00
|
|
|
* Return a generic array with location information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeLocation(Server $server): Item|NullResource
|
2017-04-08 00:28:58 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->item($server->location, new LocationTransformer());
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-13 18:13:37 +00:00
|
|
|
* Return a generic array with node information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeNode(Server $server): Item|NullResource
|
2017-04-08 00:28:58 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->item($server->node, new NodeTransformer());
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
2019-08-31 08:29:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array with database information for this server.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeDatabases(Server $server): Collection|NullResource
|
2019-08-31 08:29:44 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
|
2019-08-31 08:29:44 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->collection($server->databases, new ServerDatabaseTransformer());
|
2019-08-31 08:29:44 +00:00
|
|
|
}
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|