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',
|
2017-04-08 00:28:58 +00:00
|
|
|
'pack',
|
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',
|
2017-04-02 20:51:56 +00:00
|
|
|
];
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* Perform dependency injection.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
|
|
|
|
*/
|
|
|
|
public function handle(EnvironmentService $environmentService)
|
|
|
|
{
|
|
|
|
$this->environmentService = $environmentService;
|
|
|
|
}
|
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Server::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2017-04-02 20:51:56 +00:00
|
|
|
/**
|
|
|
|
* Return a generic transformed server array.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2017-04-02 20:51:56 +00:00
|
|
|
* @return array
|
2018-01-20 03:47:06 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-04-02 20:51:56 +00:00
|
|
|
*/
|
2018-01-19 03:56:12 +00:00
|
|
|
public function transform(Server $server): array
|
2017-04-02 20:51:56 +00:00
|
|
|
{
|
2018-01-20 03:47:06 +00:00
|
|
|
return [
|
|
|
|
'id' => $server->getKey(),
|
2018-02-24 17:57:12 +00:00
|
|
|
'external_id' => $server->external_id,
|
2018-01-20 03:47:06 +00:00
|
|
|
'uuid' => $server->uuid,
|
|
|
|
'identifier' => $server->uuidShort,
|
|
|
|
'name' => $server->name,
|
|
|
|
'description' => $server->description,
|
|
|
|
'suspended' => (bool) $server->suspended,
|
|
|
|
'limits' => [
|
|
|
|
'memory' => $server->memory,
|
|
|
|
'swap' => $server->swap,
|
|
|
|
'disk' => $server->disk,
|
|
|
|
'io' => $server->io,
|
|
|
|
'cpu' => $server->cpu,
|
2020-03-29 20:55:06 +00:00
|
|
|
'threads' => $server->threads,
|
2018-01-20 03:47:06 +00:00
|
|
|
],
|
2018-03-02 02:08:27 +00:00
|
|
|
'feature_limits' => [
|
|
|
|
'databases' => $server->database_limit,
|
|
|
|
'allocations' => $server->allocation_limit,
|
|
|
|
],
|
2018-01-20 03:47:06 +00:00
|
|
|
'user' => $server->owner_id,
|
|
|
|
'node' => $server->node_id,
|
|
|
|
'allocation' => $server->allocation_id,
|
|
|
|
'nest' => $server->nest_id,
|
|
|
|
'egg' => $server->egg_id,
|
|
|
|
'pack' => $server->pack_id,
|
|
|
|
'container' => [
|
|
|
|
'startup_command' => $server->startup,
|
|
|
|
'image' => $server->image,
|
|
|
|
'installed' => (int) $server->installed === 1,
|
|
|
|
'environment' => $this->environmentService->handle($server),
|
|
|
|
],
|
2018-02-24 17:57:12 +00:00
|
|
|
$server->getUpdatedAtColumn() => $this->formatTimestamp($server->updated_at),
|
|
|
|
$server->getCreatedAtColumn() => $this->formatTimestamp($server->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
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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)
|
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
|
|
|
|
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
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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)
|
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_USERS)) {
|
|
|
|
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
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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)
|
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_USERS)) {
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array with pack information for this server.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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 includePack(Server $server)
|
|
|
|
{
|
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_PACKS)) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$server->loadMissing('pack');
|
|
|
|
if (is_null($server->getRelation('pack'))) {
|
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->item($server->getRelation('pack'), $this->makeTransformer(PackTransformer::class), 'pack');
|
|
|
|
}
|
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
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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)
|
|
|
|
{
|
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_NESTS)) {
|
|
|
|
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
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_EGGS)) {
|
|
|
|
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-19 03:56:12 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $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)
|
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array with pack information for this server.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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)
|
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a generic array with pack information for this server.
|
|
|
|
*
|
2018-01-19 03:56:12 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @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)
|
|
|
|
{
|
2018-01-19 03:56:12 +00:00
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
|
|
|
*/
|
|
|
|
public function includeDatabases(Server $server)
|
|
|
|
{
|
|
|
|
if (! $this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) {
|
|
|
|
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
|
|
|
}
|