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
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
use League\Fractal\Resource\Item;
|
2017-12-17 14:57:05 -06:00
|
|
|
use Pterodactyl\Models\Allocation;
|
2022-10-14 10:59:20 -06:00
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-13 14:08:19 -06:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2022-12-14 17:05:46 -07:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2017-12-17 14:57:05 -06:00
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
class AllocationTransformer extends Transformer
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
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.
|
|
|
|
*/
|
2022-12-14 17:05:46 -07:00
|
|
|
public function transform(Allocation $model): array
|
2017-12-17 14:57:05 -06:00
|
|
|
{
|
2018-01-10 23:19:03 -06:00
|
|
|
return [
|
2022-12-14 17:05:46 -07:00
|
|
|
'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),
|
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.
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeNode(Allocation $allocation): Item|NullResource
|
2018-01-10 23:19:03 -06:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
return $this->item($allocation->node, new NodeTransformer());
|
2018-01-10 23:19:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the server relationship onto a given transformation.
|
2017-12-17 14:57:05 -06:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeServer(Allocation $allocation): Item|NullResource
|
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
|
|
|
}
|
|
|
|
|
2022-12-14 17:05:46 -07:00
|
|
|
return $this->item($allocation->server, new ServerTransformer());
|
2017-12-17 14:57:05 -06:00
|
|
|
}
|
|
|
|
}
|