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

167 lines
4.9 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Node;
2021-02-16 01:48:10 +00:00
use League\Fractal\Resource\NullResource;
2018-01-13 20:08:19 +00:00
use Pterodactyl\Services\Acl\Api\AdminAcl;
class NodeTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
2021-02-16 01:48:10 +00:00
protected $availableIncludes = ['allocations', 'database_host', 'location', 'mounts', 'servers'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Node::RESOURCE_NAME;
}
/**
2018-01-13 20:08:19 +00:00
* Return a node transformed into a format that can be consumed by the
* external administrative API.
*/
public function transform(Node $model): array
{
$response = $model->toArray();
2018-01-13 20:08:19 +00:00
$response[$model->getUpdatedAtColumn()] = $this->formatTimestamp($model->updated_at);
$response[$model->getCreatedAtColumn()] = $this->formatTimestamp($model->created_at);
2018-01-13 20:08:19 +00:00
$resources = $model->servers()->select(['memory', 'disk'])->get();
$response['allocated_resources'] = [
'memory' => $resources->sum('memory'),
'disk' => $resources->sum('disk'),
];
2018-01-13 20:08:19 +00:00
return $response;
}
/**
2021-02-16 01:48:10 +00:00
* Return the allocations associated with this node.
*
2018-01-13 20:08:19 +00:00
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
2021-01-23 20:33:34 +00:00
*
2021-02-16 01:48:10 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeAllocations(Node $node)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2018-01-13 20:08:19 +00:00
$node->loadMissing('allocations');
return $this->collection(
2021-01-23 20:33:34 +00:00
$node->getRelation('allocations'),
$this->makeTransformer(AllocationTransformer::class),
'allocation'
2018-01-13 20:08:19 +00:00
);
}
/**
2021-02-16 01:48:10 +00:00
* Return the database host associated with this node.
*
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
*
2021-02-16 01:48:10 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeDatabaseHost(Node $node)
{
if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {
return $this->null();
}
$node->loadMissing('databaseHost');
$databaseHost = $node->getRelation('databaseHost');
if (is_null($databaseHost)) {
return new NullResource();
}
return $this->item(
$node->getRelation('databaseHost'),
$this->makeTransformer(DatabaseHostTransformer::class),
'databaseHost'
);
}
/**
* Return the location associated with this node.
*
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeLocation(Node $node)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2018-01-13 20:08:19 +00:00
$node->loadMissing('location');
2018-01-13 20:08:19 +00:00
return $this->item(
2021-01-23 20:33:34 +00:00
$node->getRelation('location'),
$this->makeTransformer(LocationTransformer::class),
'location'
2018-01-13 20:08:19 +00:00
);
}
/**
2021-02-16 01:48:10 +00:00
* Return the mounts associated with this node.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeMounts(Node $node)
{
if (!$this->authorize(AdminAcl::RESOURCE_MOUNTS)) {
return $this->null();
}
$node->loadMissing('mounts');
return $this->collection(
$node->getRelation('mounts'),
$this->makeTransformer(MountTransformer::class),
'mount'
);
}
/**
* Return the servers associated with this node.
*
2018-01-13 20:08:19 +00:00
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
2021-01-23 20:33:34 +00:00
*
2021-02-16 01:48:10 +00:00
* @throws \Illuminate\Contracts\Container\BindingResolutionException
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeServers(Node $node)
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
2018-01-13 20:08:19 +00:00
return $this->null();
}
2018-01-13 20:08:19 +00:00
$node->loadMissing('servers');
2018-01-13 20:08:19 +00:00
return $this->collection(
2021-01-23 20:33:34 +00:00
$node->getRelation('servers'),
$this->makeTransformer(ServerTransformer::class),
'server'
2018-01-13 20:08:19 +00:00
);
}
}