2017-12-17 14:57:05 -06:00
|
|
|
<?php
|
|
|
|
|
2018-01-19 21:47:06 -06:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2020-06-13 09:56:40 -07:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-12-17 14:57:05 -06:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2018-01-13 14:08:19 -06:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2018-01-11 22:49:46 -06:00
|
|
|
class AllocationTransformer extends BaseTransformer
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2018-01-10 23:19:03 -06:00
|
|
|
/**
|
|
|
|
* Relationships that can be loaded onto allocation transformations.
|
|
|
|
*/
|
2022-05-04 19:11:42 -04:00
|
|
|
protected array $availableIncludes = ['node', 'server'];
|
2018-01-10 23:19:03 -06:00
|
|
|
|
2018-01-25 21:26:06 -06:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Allocation::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-12-17 14:57:05 -06:00
|
|
|
/**
|
|
|
|
* Return a generic transformed allocation array.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(Allocation $allocation)
|
|
|
|
{
|
2018-01-10 23:19:03 -06:00
|
|
|
return [
|
|
|
|
'id' => $allocation->id,
|
|
|
|
'ip' => $allocation->ip,
|
|
|
|
'alias' => $allocation->ip_alias,
|
|
|
|
'port' => $allocation->port,
|
2020-07-09 20:36:08 -07:00
|
|
|
'notes' => $allocation->notes,
|
2021-01-23 12:33:34 -08:00
|
|
|
'assigned' => !is_null($allocation->server_id),
|
2018-01-10 23:19:03 -06:00
|
|
|
];
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-10 23:19:03 -06:00
|
|
|
* Load the node relationship onto a given transformation.
|
2017-12-17 14:57:05 -06:00
|
|
|
*
|
2018-01-13 14:08:19 -06:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2021-01-23 12:33:34 -08:00
|
|
|
*
|
2018-05-13 12:19:35 -04:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2018-01-10 23:19:03 -06:00
|
|
|
*/
|
|
|
|
public function includeNode(Allocation $allocation)
|
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->null();
|
2018-01-10 23:19:03 -06:00
|
|
|
}
|
|
|
|
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->item(
|
2021-01-23 12:33:34 -08:00
|
|
|
$allocation->node,
|
|
|
|
$this->makeTransformer(NodeTransformer::class),
|
|
|
|
Node::RESOURCE_NAME
|
2018-01-13 14:08:19 -06:00
|
|
|
);
|
2018-01-10 23:19:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the server relationship onto a given transformation.
|
|
|
|
*
|
2018-01-13 14:08:19 -06:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2021-01-23 12:33:34 -08:00
|
|
|
*
|
2018-05-13 12:19:35 -04:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-12-17 14:57:05 -06:00
|
|
|
*/
|
2018-01-10 23:19:03 -06:00
|
|
|
public function includeServer(Allocation $allocation)
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2021-01-23 12:33:34 -08:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS) || !$allocation->server) {
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->null();
|
2018-01-10 23:19:03 -06:00
|
|
|
}
|
|
|
|
|
2018-01-13 14:08:19 -06:00
|
|
|
return $this->item(
|
2021-01-23 12:33:34 -08:00
|
|
|
$allocation->server,
|
|
|
|
$this->makeTransformer(ServerTransformer::class),
|
|
|
|
Server::RESOURCE_NAME
|
2018-01-13 14:08:19 -06:00
|
|
|
);
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
}
|