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

78 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
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;
2018-01-13 20:08:19 +00:00
use Pterodactyl\Services\Acl\Api\AdminAcl;
class AllocationTransformer extends BaseTransformer
{
/**
* Relationships that can be loaded onto allocation transformations.
*/
protected array $availableIncludes = ['node', 'server'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Allocation::RESOURCE_NAME;
}
/**
* Return a generic transformed allocation array.
*/
public function transform(Allocation $allocation): array
{
return [
'id' => $allocation->id,
'ip' => $allocation->ip,
'alias' => $allocation->ip_alias,
'port' => $allocation->port,
'notes' => $allocation->notes,
2021-01-23 20:33:34 +00:00
'assigned' => !is_null($allocation->server_id),
];
}
/**
* Load the node relationship onto a given transformation.
*
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNode(Allocation $allocation): Item|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2018-01-13 20:08:19 +00:00
return $this->item(
2021-01-23 20:33:34 +00:00
$allocation->node,
$this->makeTransformer(NodeTransformer::class),
Node::RESOURCE_NAME
2018-01-13 20:08:19 +00:00
);
}
/**
* Load the server relationship onto a given transformation.
*
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServer(Allocation $allocation): Item|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS) || !$allocation->server) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2018-01-13 20:08:19 +00:00
return $this->item(
2021-01-23 20:33:34 +00:00
$allocation->server,
$this->makeTransformer(ServerTransformer::class),
Server::RESOURCE_NAME
2018-01-13 20:08:19 +00:00
);
}
}