api(application): v2 backport

This commit is contained in:
Matthew Penner 2022-12-14 17:05:46 -07:00
parent 4cd0bee231
commit 67bf3e342e
No known key found for this signature in database
172 changed files with 2922 additions and 1579 deletions

View file

@ -0,0 +1,29 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\AdminRole;
use Pterodactyl\Transformers\Api\Transformer;
class AdminRoleTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return AdminRole::RESOURCE_NAME;
}
/**
* Transform admin role into a representation for the application API.
*/
public function transform(AdminRole $model): array
{
return [
'id' => $model->id,
'name' => $model->name,
'description' => $model->description,
];
}
}

View file

@ -2,18 +2,14 @@
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
use League\Fractal\Resource\Item;
use Pterodactyl\Models\Allocation;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class AllocationTransformer extends BaseTransformer
class AllocationTransformer extends Transformer
{
/**
* Relationships that can be loaded onto allocation transformations.
*/
protected array $availableIncludes = ['node', 'server'];
/**
@ -27,22 +23,21 @@ class AllocationTransformer extends BaseTransformer
/**
* Return a generic transformed allocation array.
*/
public function transform(Allocation $allocation): array
public function transform(Allocation $model): array
{
return [
'id' => $allocation->id,
'ip' => $allocation->ip,
'alias' => $allocation->ip_alias,
'port' => $allocation->port,
'notes' => $allocation->notes,
'assigned' => !is_null($allocation->server_id),
'id' => $model->id,
'ip' => $model->ip,
'alias' => $model->ip_alias,
'port' => $model->port,
'notes' => $model->notes,
'server_id' => $model->server_id,
'assigned' => !is_null($model->server_id),
];
}
/**
* Load the node relationship onto a given transformation.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNode(Allocation $allocation): Item|NullResource
{
@ -50,17 +45,11 @@ class AllocationTransformer extends BaseTransformer
return $this->null();
}
return $this->item(
$allocation->node,
$this->makeTransformer(NodeTransformer::class),
Node::RESOURCE_NAME
);
return $this->item($allocation->node, new NodeTransformer());
}
/**
* Load the server relationship onto a given transformation.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServer(Allocation $allocation): Item|NullResource
{
@ -68,10 +57,6 @@ class AllocationTransformer extends BaseTransformer
return $this->null();
}
return $this->item(
$allocation->server,
$this->makeTransformer(ServerTransformer::class),
Server::RESOURCE_NAME
);
return $this->item($allocation->server, new ServerTransformer());
}
}

View file

@ -1,114 +0,0 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Illuminate\Http\Request;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\ApiKey;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Model;
use League\Fractal\TransformerAbstract;
use Pterodactyl\Services\Acl\Api\AdminAcl;
/**
* @method array transform(Model $model)
*/
abstract class BaseTransformer extends TransformerAbstract
{
public const RESPONSE_TIMEZONE = 'UTC';
protected Request $request;
/**
* BaseTransformer constructor.
*/
public function __construct()
{
// Transformers allow for dependency injection on the handle method.
if (method_exists($this, 'handle')) {
Container::getInstance()->call([$this, 'handle']);
}
}
/**
* Return the resource name for the JSONAPI output.
*/
abstract public function getResourceName(): string;
/**
* Sets the request on the instance.
*/
public function setRequest(Request $request): self
{
$this->request = $request;
return $this;
}
/**
* Returns a new transformer instance with the request set on the instance.
*/
public static function fromRequest(Request $request): BaseTransformer
{
return app(static::class)->setRequest($request);
}
/**
* Determine if the API key loaded onto the transformer has permission
* to access a different resource. This is used when including other
* models on a transformation request.
*
* @deprecated prefer $user->can/cannot methods
*/
protected function authorize(string $resource): bool
{
$allowed = [ApiKey::TYPE_ACCOUNT, ApiKey::TYPE_APPLICATION];
$token = $this->request->user()->currentAccessToken();
if (!$token instanceof ApiKey || !in_array($token->key_type, $allowed)) {
return false;
}
// If this is not a deprecated application token type we can only check that
// the user is a root admin at the moment. In a future release we'll be rolling
// out more specific permissions for keys.
if ($token->key_type === ApiKey::TYPE_ACCOUNT) {
return $this->request->user()->root_admin;
}
return AdminAcl::check($token, $resource);
}
/**
* Create a new instance of the transformer and pass along the currently
* set API key.
*
* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
*
* @param class-string<T> $abstract
*
* @return T
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*
* @noinspection PhpDocSignatureInspection
*/
protected function makeTransformer(string $abstract)
{
Assert::subclassOf($abstract, self::class);
return $abstract::fromRequest($this->request);
}
/**
* Return an ISO-8601 formatted timestamp to use in the API response.
*/
protected function formatTimestamp(string $timestamp): string
{
return CarbonImmutable::createFromFormat(CarbonInterface::DEFAULT_TO_STRING_FORMAT, $timestamp)
->setTimezone(self::RESPONSE_TIMEZONE)
->toAtomString();
}
}

View file

@ -2,17 +2,15 @@
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class DatabaseHostTransformer extends BaseTransformer
class DatabaseHostTransformer extends Transformer
{
protected array $availableIncludes = [
'databases',
];
protected array $availableIncludes = ['databases'];
/**
* Return the resource name for the JSONAPI output.
@ -33,16 +31,14 @@ class DatabaseHostTransformer extends BaseTransformer
'host' => $model->host,
'port' => $model->port,
'username' => $model->username,
'node' => $model->node_id,
'created_at' => $model->created_at->toAtomString(),
'updated_at' => $model->updated_at->toAtomString(),
'node_id' => $model->node_id,
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Include the databases associated with this host.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeDatabases(DatabaseHost $model): Collection|NullResource
{
@ -50,8 +46,7 @@ class DatabaseHostTransformer extends BaseTransformer
return $this->null();
}
$model->loadMissing('databases');
return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME);
// TODO
return $this->collection($model->databases, new ServerDatabaseTransformer());
}
}

View file

@ -2,26 +2,23 @@
namespace Pterodactyl\Transformers\Api\Application;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server;
use League\Fractal\Resource\Item;
use Pterodactyl\Models\EggVariable;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class EggTransformer extends BaseTransformer
class EggTransformer extends Transformer
{
/**
* Relationships that can be loaded onto this transformation.
*/
protected array $availableIncludes = [
'nest',
'servers',
'config',
'nest',
'script',
'servers',
'variables',
];
@ -50,19 +47,14 @@ class EggTransformer extends BaseTransformer
'id' => $model->id,
'uuid' => $model->uuid,
'name' => $model->name,
'nest' => $model->nest_id,
'nest_id' => $model->nest_id,
'author' => $model->author,
'description' => $model->description,
// "docker_image" is deprecated, but left here to avoid breaking too many things at once
// in external software. We'll remove it down the road once things have gotten the chance
// to upgrade to using "docker_images".
'docker_image' => count($model->docker_images) > 0 ? Arr::first($model->docker_images) : '',
'docker_images' => $model->docker_images,
'config' => [
'files' => $files,
'startup' => json_decode($model->config_startup, true),
'stop' => $model->config_stop,
'logs' => json_decode($model->config_logs, true),
'file_denylist' => $model->file_denylist,
'extends' => $model->config_from,
],
@ -74,43 +66,11 @@ class EggTransformer extends BaseTransformer
'container' => $model->script_container,
'extends' => $model->copy_script_from,
],
$model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at),
$model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Include the Nest relationship for the given Egg in the transformation.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNest(Egg $model): Item|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
return $this->null();
}
$model->loadMissing('nest');
return $this->item($model->getRelation('nest'), $this->makeTransformer(NestTransformer::class), Nest::RESOURCE_NAME);
}
/**
* Include the Servers relationship for the given Egg in the transformation.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(Egg $model): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$model->loadMissing('servers');
return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME);
}
/**
* Include more detailed information about the configuration if this Egg is
* extending another.
@ -121,8 +81,6 @@ class EggTransformer extends BaseTransformer
return $this->null();
}
$model->loadMissing('configFrom');
return $this->item($model, function (Egg $model) {
return [
'files' => json_decode($model->inherit_config_files),
@ -133,6 +91,18 @@ class EggTransformer extends BaseTransformer
});
}
/**
* Include the Nest relationship for the given Egg in the transformation.
*/
public function includeNest(Egg $model): Item|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
return $this->null();
}
return $this->item($model->nest, new NestTransformer());
}
/**
* Include more detailed information about the script configuration if the
* Egg is extending another.
@ -143,8 +113,6 @@ class EggTransformer extends BaseTransformer
return $this->null();
}
$model->loadMissing('scriptFrom');
return $this->item($model, function (Egg $model) {
return [
'privileged' => $model->script_is_privileged,
@ -155,10 +123,20 @@ class EggTransformer extends BaseTransformer
});
}
/**
* Include the Servers relationship for the given Egg in the transformation.
*/
public function includeServers(Egg $model): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
return $this->collection($model->servers, new ServerTransformer());
}
/**
* Include the variables that are defined for this Egg.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeVariables(Egg $model): Collection|NullResource
{
@ -168,10 +146,6 @@ class EggTransformer extends BaseTransformer
$model->loadMissing('variables');
return $this->collection(
$model->getRelation('variables'),
$this->makeTransformer(EggVariableTransformer::class),
EggVariable::RESOURCE_NAME
);
return $this->collection($model->variables, new EggVariableTransformer());
}
}

View file

@ -4,8 +4,9 @@ namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Transformers\Api\Transformer;
class EggVariableTransformer extends BaseTransformer
class EggVariableTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
@ -15,7 +16,10 @@ class EggVariableTransformer extends BaseTransformer
return Egg::RESOURCE_NAME;
}
public function transform(EggVariable $model)
/**
* Transform egg variable into a representation for the application API.
*/
public function transform(EggVariable $model): array
{
return $model->toArray();
}

View file

@ -6,8 +6,9 @@ use Pterodactyl\Models\Location;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class LocationTransformer extends BaseTransformer
class LocationTransformer extends Transformer
{
/**
* List of resources that can be included.
@ -25,37 +26,19 @@ class LocationTransformer extends BaseTransformer
/**
* Return a generic transformed location array.
*/
public function transform(Location $location): array
public function transform(Location $model): array
{
return [
'id' => $location->id,
'short' => $location->short,
'long' => $location->long,
$location->getUpdatedAtColumn() => $this->formatTimestamp($location->updated_at),
$location->getCreatedAtColumn() => $this->formatTimestamp($location->created_at),
'id' => $model->id,
'short' => $model->short,
'long' => $model->long,
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(Location $location): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$location->loadMissing('servers');
return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNodes(Location $location): Collection|NullResource
{
@ -63,8 +46,18 @@ class LocationTransformer extends BaseTransformer
return $this->null();
}
$location->loadMissing('nodes');
return $this->collection($location->nodes, new NodeTransformer());
}
return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node');
/**
* Return the nodes associated with this location.
*/
public function includeServers(Location $location): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
return $this->collection($location->servers, new ServerTransformer());
}
}

View file

@ -0,0 +1,75 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Mount;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class MountTransformer extends Transformer
{
/**
* List of resources that can be included.
*/
protected array $availableIncludes = ['eggs', 'nodes', 'servers'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Mount::RESOURCE_NAME;
}
public function transform(Mount $model): array
{
return [
'id' => $model->id,
'uuid' => $model->uuid,
'name' => $model->name,
'description' => $model->description,
'source' => $model->source,
'target' => $model->target,
'read_only' => $model->read_only,
'user_mountable' => $model->user_mountable,
];
}
/**
* Return the eggs associated with this mount.
*/
public function includeEggs(Mount $mount): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
return $this->null();
}
return $this->collection($mount->eggs, new EggTransformer());
}
/**
* Return the nodes associated with this mount.
*/
public function includeNodes(Mount $mount): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
return $this->collection($mount->nodes, new NodeTransformer());
}
/**
* Return the servers associated with this mount.
*/
public function includeServers(Mount $mount): Collection|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
return $this->collection($mount->servers, new ServerTransformer());
}
}

View file

@ -2,21 +2,18 @@
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Server;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class NestTransformer extends BaseTransformer
class NestTransformer extends Transformer
{
/**
* Relationships that can be loaded onto this transformation.
*/
protected array $availableIncludes = [
'eggs', 'servers',
];
protected array $availableIncludes = ['eggs', 'servers'];
/**
* Return the resource name for the JSONAPI output.
@ -34,16 +31,14 @@ class NestTransformer extends BaseTransformer
{
$response = $model->toArray();
$response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at);
$response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at);
$response['created_at'] = self::formatTimestamp($model->created_at);
$response['updated_at'] = self::formatTimestamp($model->updated_at);
return $response;
}
/**
* Include the Eggs relationship on the given Nest model transformation.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeEggs(Nest $model): Collection|NullResource
{
@ -51,15 +46,11 @@ class NestTransformer extends BaseTransformer
return $this->null();
}
$model->loadMissing('eggs');
return $this->collection($model->getRelation('eggs'), $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
return $this->collection($model->eggs, new EggTransformer());
}
/**
* Include the servers relationship on the given Nest model.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(Nest $model): Collection|NullResource
{
@ -67,8 +58,6 @@ class NestTransformer extends BaseTransformer
return $this->null();
}
$model->loadMissing('servers');
return $this->collection($model->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), Server::RESOURCE_NAME);
return $this->collection($model->servers, new ServerTransformer());
}
}

View file

@ -7,8 +7,9 @@ use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class NodeTransformer extends BaseTransformer
class NodeTransformer extends Transformer
{
/**
* List of resources that can be included.
@ -27,9 +28,9 @@ class NodeTransformer extends BaseTransformer
* Return a node transformed into a format that can be consumed by the
* external administrative API.
*/
public function transform(Node $node): array
public function transform(Node $model): array
{
$response = collect($node->toArray())->mapWithKeys(function ($value, $key) {
$response = collect($model->toArray())->mapWithKeys(function ($value, $key) {
// I messed up early in 2016 when I named this column as poorly
// as I did. This is the tragic result of my mistakes.
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
@ -37,10 +38,10 @@ class NodeTransformer extends BaseTransformer
return [snake_case($key) => $value];
})->toArray();
$response[$node->getUpdatedAtColumn()] = $this->formatTimestamp($node->updated_at);
$response[$node->getCreatedAtColumn()] = $this->formatTimestamp($node->created_at);
$response['created_at'] = self::formatTimestamp($model->created_at);
$response['updated_at'] = self::formatTimestamp($model->updated_at);
$resources = $node->servers()->select(['memory', 'disk'])->get();
$resources = $model->servers()->select(['memory', 'disk'])->get();
$response['allocated_resources'] = [
'memory' => $resources->sum('memory'),
@ -51,9 +52,7 @@ class NodeTransformer extends BaseTransformer
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
* Return the allocations associated with this node.
*/
public function includeAllocations(Node $node): Collection|NullResource
{
@ -61,19 +60,11 @@ class NodeTransformer extends BaseTransformer
return $this->null();
}
$node->loadMissing('allocations');
return $this->collection(
$node->getRelation('allocations'),
$this->makeTransformer(AllocationTransformer::class),
'allocation'
);
return $this->collection($node->allocations, new AllocationTransformer());
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
* Return the location associated with this node.
*/
public function includeLocation(Node $node): Item|NullResource
{
@ -81,19 +72,11 @@ class NodeTransformer extends BaseTransformer
return $this->null();
}
$node->loadMissing('location');
return $this->item(
$node->getRelation('location'),
$this->makeTransformer(LocationTransformer::class),
'location'
);
return $this->item($node->location, new LocationTransformer());
}
/**
* Return the nodes associated with this location.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
* Return the servers associated with this node.
*/
public function includeServers(Node $node): Collection|NullResource
{
@ -101,12 +84,6 @@ class NodeTransformer extends BaseTransformer
return $this->null();
}
$node->loadMissing('servers');
return $this->collection(
$node->getRelation('servers'),
$this->makeTransformer(ServerTransformer::class),
'server'
);
return $this->collection($node->servers, new ServerTransformer());
}
}

View file

@ -4,14 +4,14 @@ namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Database;
use League\Fractal\Resource\Item;
use Pterodactyl\Models\DatabaseHost;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
use Illuminate\Contracts\Encryption\Encrypter;
class ServerDatabaseTransformer extends BaseTransformer
class ServerDatabaseTransformer extends Transformer
{
protected array $availableIncludes = ['password', 'host'];
protected array $availableIncludes = ['host', 'password'];
private Encrypter $encrypter;
@ -38,17 +38,29 @@ class ServerDatabaseTransformer extends BaseTransformer
{
return [
'id' => $model->id,
'server' => $model->server_id,
'host' => $model->database_host_id,
'database' => $model->database,
'database_host_id' => $model->database_host_id,
'server_id' => $model->server_id,
'name' => $model->database,
'username' => $model->username,
'remote' => $model->remote,
'max_connections' => $model->max_connections,
'created_at' => $model->created_at->toAtomString(),
'updated_at' => $model->updated_at->toAtomString(),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Return the database host relationship for this server database.
*/
public function includeHost(Database $model): Item|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {
return $this->null();
}
return $this->item($model->host, new DatabaseHostTransformer());
}
/**
* Include the database password in the request.
*/
@ -60,24 +72,4 @@ class ServerDatabaseTransformer extends BaseTransformer
];
}, 'database_password');
}
/**
* Return the database host relationship for this server database.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeHost(Database $model): Item|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {
return $this->null();
}
$model->loadMissing('host');
return $this->item(
$model->getRelation('host'),
$this->makeTransformer(DatabaseHostTransformer::class),
DatabaseHost::RESOURCE_NAME
);
}
}

View file

@ -7,9 +7,10 @@ use League\Fractal\Resource\Item;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
use Pterodactyl\Services\Servers\EnvironmentService;
class ServerTransformer extends BaseTransformer
class ServerTransformer extends Transformer
{
private EnvironmentService $environmentService;
@ -48,53 +49,47 @@ class ServerTransformer extends BaseTransformer
/**
* Return a generic transformed server array.
*/
public function transform(Server $server): array
public function transform(Server $model): 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(),
'id' => $model->getKey(),
'external_id' => $model->external_id,
'uuid' => $model->uuid,
'identifier' => $model->uuidShort,
'name' => $model->name,
'description' => $model->description,
'status' => $model->status,
'limits' => [
'memory' => $server->memory,
'swap' => $server->swap,
'disk' => $server->disk,
'io' => $server->io,
'cpu' => $server->cpu,
'threads' => $server->threads,
'oom_disabled' => $server->oom_disabled,
'cpu' => $model->cpu,
'disk' => $model->disk,
'io' => $model->io,
'memory' => $model->memory,
'oom_disabled' => $model->oom_disabled,
'swap' => $model->swap,
'threads' => $model->threads,
],
'feature_limits' => [
'databases' => $server->database_limit,
'allocations' => $server->allocation_limit,
'backups' => $server->backup_limit,
'allocations' => $model->allocation_limit,
'backups' => $model->backup_limit,
'databases' => $model->database_limit,
],
'user' => $server->owner_id,
'node' => $server->node_id,
'allocation' => $server->allocation_id,
'nest' => $server->nest_id,
'egg' => $server->egg_id,
'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,
'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),
'startup' => $model->startup,
'image' => $model->image,
'environment' => $this->environmentService->handle($model),
],
$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),
$server->getCreatedAtColumn() => $this->formatTimestamp($server->created_at),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Return a generic array of allocations for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeAllocations(Server $server): Collection|NullResource
{
@ -102,15 +97,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('allocations');
return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation');
return $this->collection($server->allocations, new AllocationTransformer());
}
/**
* Return a generic array of data about subusers for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeSubusers(Server $server): Collection|NullResource
{
@ -118,15 +109,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('subusers');
return $this->collection($server->getRelation('subusers'), $this->makeTransformer(SubuserTransformer::class), 'subuser');
return $this->collection($server->subusers, new SubuserTransformer());
}
/**
* Return a generic array of data about subusers for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeUser(Server $server): Item|NullResource
{
@ -134,15 +121,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('user');
return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');
return $this->item($server->user, new UserTransformer());
}
/**
* Return a generic array with nest information for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNest(Server $server): Item|NullResource
{
@ -150,15 +133,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('nest');
return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest');
return $this->item($server->nest, new NestTransformer());
}
/**
* Return a generic array with egg information for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeEgg(Server $server): Item|NullResource
{
@ -166,15 +145,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('egg');
return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg');
return $this->item($server->egg, new EggTransformer());
}
/**
* Return a generic array of data about subusers for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeVariables(Server $server): Collection|NullResource
{
@ -182,15 +157,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('variables');
return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable');
return $this->collection($server->variables, new ServerVariableTransformer());
}
/**
* Return a generic array with location information for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeLocation(Server $server): Item|NullResource
{
@ -198,15 +169,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('location');
return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location');
return $this->item($server->location, new LocationTransformer());
}
/**
* Return a generic array with node information for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNode(Server $server): Item|NullResource
{
@ -214,15 +181,11 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('node');
return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node');
return $this->item($server->node, new NodeTransformer());
}
/**
* Return a generic array with database information for this server.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeDatabases(Server $server): Collection|NullResource
{
@ -230,8 +193,6 @@ class ServerTransformer extends BaseTransformer
return $this->null();
}
$server->loadMissing('databases');
return $this->collection($server->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), 'databases');
return $this->collection($server->databases, new ServerDatabaseTransformer());
}
}

View file

@ -7,8 +7,9 @@ use Pterodactyl\Models\EggVariable;
use Pterodactyl\Models\ServerVariable;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class ServerVariableTransformer extends BaseTransformer
class ServerVariableTransformer extends Transformer
{
/**
* List of resources that can be included.
@ -26,15 +27,13 @@ class ServerVariableTransformer extends BaseTransformer
/**
* Return a generic transformed server variable array.
*/
public function transform(EggVariable $variable): array
public function transform(EggVariable $model): array
{
return $variable->toArray();
return $model->toArray();
}
/**
* Return the parent service variable data.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeParent(EggVariable $variable): Item|NullResource
{
@ -42,8 +41,7 @@ class ServerVariableTransformer extends BaseTransformer
return $this->null();
}
$variable->loadMissing('variable');
return $this->item($variable->getRelation('variable'), $this->makeTransformer(EggVariableTransformer::class), 'variable');
// TODO: what the fuck?
return $this->item($variable->variable, new EggVariableTransformer());
}
}

View file

@ -6,13 +6,14 @@ use Pterodactyl\Models\Subuser;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class SubuserTransformer extends BaseTransformer
class SubuserTransformer extends Transformer
{
/**
* List of resources that can be included.
*/
protected array $availableIncludes = ['user', 'server'];
protected array $availableIncludes = ['server', 'user'];
/**
* Return the resource name for the JSONAPI output.
@ -25,38 +26,20 @@ class SubuserTransformer extends BaseTransformer
/**
* Return a transformed Subuser model that can be consumed by external services.
*/
public function transform(Subuser $subuser): array
public function transform(Subuser $model): array
{
return [
'id' => $subuser->id,
'user_id' => $subuser->user_id,
'server_id' => $subuser->server_id,
'permissions' => $subuser->permissions,
'created_at' => $this->formatTimestamp($subuser->created_at),
'updated_at' => $this->formatTimestamp($subuser->updated_at),
'id' => $model->id,
'user_id' => $model->user_id,
'server_id' => $model->server_id,
'permissions' => $model->permissions,
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Return a generic item of user for this subuser.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeUser(Subuser $subuser): Item|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
return $this->null();
}
$subuser->loadMissing('user');
return $this->item($subuser->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');
}
/**
* Return a generic item of server for this subuser.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServer(Subuser $subuser): Item|NullResource
{
@ -64,8 +47,18 @@ class SubuserTransformer extends BaseTransformer
return $this->null();
}
$subuser->loadMissing('server');
return $this->item($subuser->server, new ServerTransformer());
}
return $this->item($subuser->getRelation('server'), $this->makeTransformer(ServerTransformer::class), 'server');
/**
* Return a generic item of user for this subuser.
*/
public function includeUser(Subuser $subuser): Item|NullResource
{
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
return $this->null();
}
return $this->item($subuser->user, new UserTransformer());
}
}

View file

@ -6,8 +6,9 @@ use Pterodactyl\Models\User;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Transformers\Api\Transformer;
class UserTransformer extends BaseTransformer
class UserTransformer extends Transformer
{
/**
* List of resources that can be included.
@ -25,28 +26,27 @@ class UserTransformer extends BaseTransformer
/**
* Return a transformed User model that can be consumed by external services.
*/
public function transform(User $user): array
public function transform(User $model): array
{
return [
'id' => $user->id,
'external_id' => $user->external_id,
'uuid' => $user->uuid,
'username' => $user->username,
'email' => $user->email,
'first_name' => $user->name_first,
'last_name' => $user->name_last,
'language' => $user->language,
'root_admin' => (bool) $user->root_admin,
'2fa' => (bool) $user->use_totp,
'created_at' => $this->formatTimestamp($user->created_at),
'updated_at' => $this->formatTimestamp($user->updated_at),
'id' => $model->id,
'external_id' => $model->external_id,
'uuid' => $model->uuid,
'username' => $model->username,
'email' => $model->email,
'language' => $model->language,
'root_admin' => (bool) $model->root_admin,
'2fa' => (bool) $model->use_totp,
'avatar_url' => $model->avatarURL(),
'admin_role_id' => $model->admin_role_id,
'role_name' => $model->adminRoleName(),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Return the servers associated with this user.
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(User $user): Collection|NullResource
{
@ -54,8 +54,6 @@ class UserTransformer extends BaseTransformer
return $this->null();
}
$user->loadMissing('servers');
return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
return $this->collection($user->servers, new ServerTransformer());
}
}