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