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
|
|
|
|
|
|
|
use Pterodactyl\Models\Allocation;
|
2018-01-13 20:08:19 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2021-08-07 20:25:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2017-12-17 20:57:05 +00:00
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
class AllocationTransformer extends Transformer
|
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
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Allocation::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
public function transform(Allocation $model): array
|
2017-12-17 20:57:05 +00:00
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
return [
|
2021-01-01 22:55:30 +00:00
|
|
|
'id' => $model->id,
|
|
|
|
'ip' => $model->ip,
|
|
|
|
'alias' => $model->ip_alias,
|
|
|
|
'port' => $model->port,
|
|
|
|
'notes' => $model->notes,
|
2021-08-07 20:25:06 +00:00
|
|
|
'assigned' => !is_null($model->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
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Allocation $allocation
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-13 20:08:19 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-01-11 05:19:03 +00:00
|
|
|
*/
|
|
|
|
public function includeNode(Allocation $allocation)
|
|
|
|
{
|
2021-08-07 20:25:06 +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
|
|
|
}
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
return $this->item($allocation->node, new NodeTransformer());
|
2018-01-11 05:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the server relationship onto a given transformation.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Allocation $allocation
|
2021-01-01 22:55:30 +00:00
|
|
|
*
|
2018-01-13 20:08:19 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
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-08-07 20:25:06 +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
|
|
|
}
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
return $this->item($allocation->server, new ServerTransformer());
|
2017-12-17 20:57:05 +00:00
|
|
|
}
|
|
|
|
}
|