2017-04-02 20:51:56 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2017-04-02 20:51:56 +00:00
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2018-01-19 03:56:12 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2018-01-20 03:47:06 +00:00
|
|
|
use Pterodactyl\Services\Servers\EnvironmentService;
|
2017-04-02 20:51:56 +00:00
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
class ServerTransformer extends BaseTransformer
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\EnvironmentService
|
|
|
|
*/
|
|
|
|
private $environmentService;
|
|
|
|
|
2017-04-02 20:51:56 +00:00
|
|
|
/**
|
|
|
|
* List of resources that can be included.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $availableIncludes = [
|
|
|
|
'allocations',
|
|
|
|
'user',
|
|
|
|
'subusers',
|
2018-01-19 03:56:12 +00:00
|
|
|
'nest',
|
|
|
|
'egg',
|
2017-04-08 00:28:58 +00:00
|
|
|
'variables',
|
|
|
|
'location',
|
|
|
|
'node',
|
2019-08-31 08:29:44 +00:00
|
|
|
'databases',
|
2020-12-16 16:34:47 +00:00
|
|
|
'transfer',
|
2017-04-02 20:51:56 +00:00
|
|
|
];
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* Perform dependency injection.
|
|
|
|
*/
|
|
|
|
public function handle(EnvironmentService $environmentService)
|
|
|
|
{
|
|
|
|
$this->environmentService = $environmentService;
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Server::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-04-02 20:51:56 +00:00
|
|
|
/**
|
|
|
|
* Return a generic transformed server array.
|
|
|
|
*/
|
2021-01-01 22:55:30 +00:00
|
|
|
public function transform(Server $model): array
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2018-01-20 03:47:06 +00:00
|
|
|
return [
|
2021-01-01 22:55:30 +00:00
|
|
|
'id' => $model->getKey(),
|
|
|
|
'external_id' => $model->external_id,
|
|
|
|
'uuid' => $model->uuid,
|
|
|
|
'identifier' => $model->uuidShort,
|
|
|
|
'name' => $model->name,
|
|
|
|
'description' => $model->description,
|
2021-02-07 23:16:22 +00:00
|
|
|
'status' => $model->status,
|
2018-01-20 03:47:06 +00:00
|
|
|
'limits' => [
|
2021-01-01 22:55:30 +00:00
|
|
|
'memory' => $model->memory,
|
|
|
|
'swap' => $model->swap,
|
|
|
|
'disk' => $model->disk,
|
|
|
|
'io' => $model->io,
|
|
|
|
'cpu' => $model->cpu,
|
|
|
|
'threads' => $model->threads,
|
2018-01-20 03:47:06 +00:00
|
|
|
],
|
2018-03-02 02:08:27 +00:00
|
|
|
'feature_limits' => [
|
2021-01-01 22:55:30 +00:00
|
|
|
'databases' => $model->database_limit,
|
|
|
|
'allocations' => $model->allocation_limit,
|
|
|
|
'backups' => $model->backup_limit,
|
2018-03-02 02:08:27 +00:00
|
|
|
],
|
2021-05-20 22:00:46 +00:00
|
|
|
'owner_id' => $model->owner_id,
|
|
|
|
'node_id' => $model->node_id,
|
|
|
|
'allocation_id' => $model->allocation_id,
|
|
|
|
'nest_id' => $model->nest_id,
|
|
|
|
'egg_id' => $model->egg_id,
|
2018-01-20 03:47:06 +00:00
|
|
|
'container' => [
|
2021-01-01 22:55:30 +00:00
|
|
|
'startup_command' => $model->startup,
|
|
|
|
'image' => $model->image,
|
|
|
|
'environment' => $this->environmentService->handle($model),
|
2018-01-20 03:47:06 +00:00
|
|
|
],
|
2021-01-01 22:55:30 +00:00
|
|
|
$model->getUpdatedAtColumn() => $this->formatTimestamp($model->updated_at),
|
|
|
|
$model->getCreatedAtColumn() => $this->formatTimestamp($model->created_at),
|
2018-01-20 03:47:06 +00:00
|
|
|
];
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of allocations for this server.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-02-25 21:30:56 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-02 20:51:56 +00:00
|
|
|
*/
|
|
|
|
public function includeAllocations(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('allocations');
|
|
|
|
|
|
|
|
return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation');
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of data about subusers for this server.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-02 20:51:56 +00:00
|
|
|
*/
|
|
|
|
public function includeSubusers(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('subusers');
|
|
|
|
|
2019-12-28 11:53:33 +00:00
|
|
|
return $this->collection($server->getRelation('subusers'), $this->makeTransformer(SubuserTransformer::class), 'subuser');
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of data about subusers for this server.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-02 20:51:56 +00:00
|
|
|
*/
|
|
|
|
public function includeUser(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_USERS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('user');
|
|
|
|
|
|
|
|
return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|
2017-04-08 00:28:58 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-19 03:56:12 +00:00
|
|
|
* Return a generic array with nest information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
2018-03-02 00:43:39 +00:00
|
|
|
public function includeNest(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NESTS)) {
|
2018-03-02 00:43:39 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$server->loadMissing('nest');
|
|
|
|
|
|
|
|
return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest');
|
|
|
|
}
|
2017-04-08 00:28:58 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-31 08:29:44 +00:00
|
|
|
* Return a generic array with egg information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
2019-08-31 08:29:44 +00:00
|
|
|
public function includeEgg(Server $server)
|
2017-04-08 00:28:58 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_EGGS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('egg');
|
|
|
|
|
2019-08-31 08:29:44 +00:00
|
|
|
return $this->item($server->getRelation('egg'), $this->makeTransformer(EggTransformer::class), 'egg');
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array of data about subusers for this server.
|
|
|
|
*
|
2018-01-20 03:47:06 +00:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
|
|
|
public function includeVariables(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('variables');
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable');
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-13 18:13:37 +00:00
|
|
|
* Return a generic array with location information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
|
|
|
public function includeLocation(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('location');
|
|
|
|
|
|
|
|
return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location');
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-13 18:13:37 +00:00
|
|
|
* Return a generic array with node information for this server.
|
2017-04-08 00:28:58 +00:00
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-03-02 00:43:39 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2017-04-08 00:28:58 +00:00
|
|
|
*/
|
|
|
|
public function includeNode(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_NODES)) {
|
2018-01-19 03:56:12 +00:00
|
|
|
return $this->null();
|
2017-04-09 19:31:10 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 03:56:12 +00:00
|
|
|
$server->loadMissing('node');
|
|
|
|
|
|
|
|
return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node');
|
2017-04-08 00:28:58 +00:00
|
|
|
}
|
2019-08-31 08:29:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array with database information for this server.
|
|
|
|
*
|
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
|
|
|
public function includeDatabases(Server $server)
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
|
2019-08-31 08:29:44 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$server->loadMissing('databases');
|
|
|
|
|
|
|
|
return $this->collection($server->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), 'databases');
|
|
|
|
}
|
2017-04-02 20:51:56 +00:00
|
|
|
}
|