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

238 lines
7.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Server;
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;
use Pterodactyl\Services\Servers\EnvironmentService;
2018-01-19 03:56:12 +00:00
class ServerTransformer extends BaseTransformer
{
private EnvironmentService $environmentService;
/**
* List of resources that can be included.
*/
protected array $availableIncludes = [
'allocations',
'user',
'subusers',
2018-01-19 03:56:12 +00:00
'nest',
'egg',
'variables',
'location',
'node',
'databases',
'transfer',
];
/**
* Perform dependency injection.
*/
public function handle(EnvironmentService $environmentService)
{
$this->environmentService = $environmentService;
}
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Server::RESOURCE_NAME;
}
/**
* Return a generic transformed server array.
*/
2018-01-19 03:56:12 +00:00
public function transform(Server $server): array
{
return [
'id' => $server->getKey(),
'external_id' => $server->external_id,
'uuid' => $server->uuid,
'identifier' => $server->uuidShort,
'name' => $server->name,
'description' => $server->description,
'status' => $server->status,
// This field is deprecated, please use "status".
'suspended' => $server->isSuspended(),
'limits' => [
'memory' => $server->memory,
'swap' => $server->swap,
'disk' => $server->disk,
'io' => $server->io,
'cpu' => $server->cpu,
'threads' => $server->threads,
'oom_disabled' => $server->oom_disabled,
],
'feature_limits' => [
'databases' => $server->database_limit,
'allocations' => $server->allocation_limit,
'backups' => $server->backup_limit,
],
'user' => $server->owner_id,
'node' => $server->node_id,
'allocation' => $server->allocation_id,
'nest' => $server->nest_id,
'egg' => $server->egg_id,
'container' => [
'startup_command' => $server->startup,
'image' => $server->image,
// This field is deprecated, please use "status".
'installed' => $server->isInstalled() ? 1 : 0,
'environment' => $this->environmentService->handle($server),
],
$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),
$server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),
];
}
/**
* Return a generic array of allocations for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeAllocations(Server $server): Collection|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('allocations');
return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation');
}
/**
* Return a generic array of data about subusers for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeSubusers(Server $server): Collection|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('subusers');
2019-12-28 11:53:33 +00:00
return $this->collection($server->getRelation('subusers'), $this->makeTransformer(SubuserTransformer::class), 'subuser');
}
/**
* Return a generic array of data about subusers for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeUser(Server $server): Item|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('user');
return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');
}
/**
2018-01-19 03:56:12 +00:00
* Return a generic array with nest information for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
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();
}
$server->loadMissing('nest');
return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest');
}
/**
* Return a generic array with egg information for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeEgg(Server $server): Item|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('egg');
return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg');
}
/**
* Return a generic array of data about subusers for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeVariables(Server $server): Collection|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('variables');
return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable');
}
/**
* Return a generic array with location information for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeLocation(Server $server): Item|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('location');
return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location');
}
/**
* Return a generic array with node information for this server.
*
2018-03-02 00:43:39 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNode(Server $server): Item|NullResource
{
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();
}
2018-01-19 03:56:12 +00:00
$server->loadMissing('node');
return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node');
}
/**
* Return a generic array with database information for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeDatabases(Server $server): Collection|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
return $this->null();
}
$server->loadMissing('databases');
return $this->collection($server->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), 'databases');
}
}