2018-02-27 21:28:43 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Server;
|
2020-08-22 16:54:12 -07:00
|
|
|
use Pterodactyl\Models\Permission;
|
2021-08-07 13:06:45 -07:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2020-08-22 15:43:28 -07:00
|
|
|
use Pterodactyl\Services\Servers\StartupCommandService;
|
2018-02-27 21:28:43 -06:00
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
class ServerTransformer extends Transformer
|
2018-02-27 21:28:43 -06:00
|
|
|
{
|
2020-07-09 19:17:24 -07:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2020-08-22 15:43:28 -07:00
|
|
|
protected $defaultIncludes = ['allocations', 'variables'];
|
2020-07-09 19:17:24 -07:00
|
|
|
|
2019-09-22 15:30:53 -07:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-12-28 12:03:19 -08:00
|
|
|
protected $availableIncludes = ['egg', 'subusers'];
|
2019-09-22 15:30:53 -07:00
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
protected StartupCommandService $service;
|
|
|
|
|
2018-02-27 21:28:43 -06:00
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Server::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
public function handle(StartupCommandService $service)
|
2018-02-27 21:28:43 -06:00
|
|
|
{
|
2021-08-07 13:06:45 -07:00
|
|
|
$this->service = $service;
|
|
|
|
}
|
2020-08-22 15:43:28 -07:00
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
public function transform(Server $server): array
|
|
|
|
{
|
2018-02-27 21:28:43 -06:00
|
|
|
return [
|
2021-08-07 13:06:45 -07:00
|
|
|
'server_owner' => $this->user()->id === $server->owner_id,
|
2018-02-27 21:28:43 -06:00
|
|
|
'identifier' => $server->uuidShort,
|
2020-11-02 00:14:02 -05:00
|
|
|
'internal_id' => $server->id,
|
2018-02-27 21:28:43 -06:00
|
|
|
'uuid' => $server->uuid,
|
|
|
|
'name' => $server->name,
|
2018-05-28 14:11:23 -07:00
|
|
|
'node' => $server->node->name,
|
2019-12-07 15:58:37 -08:00
|
|
|
'sftp_details' => [
|
|
|
|
'ip' => $server->node->fqdn,
|
2021-02-11 10:21:32 -07:00
|
|
|
'port' => $server->node->public_port_sftp,
|
2019-12-07 15:58:37 -08:00
|
|
|
],
|
2018-02-27 21:28:43 -06:00
|
|
|
'description' => $server->description,
|
|
|
|
'limits' => [
|
|
|
|
'memory' => $server->memory,
|
|
|
|
'swap' => $server->swap,
|
|
|
|
'disk' => $server->disk,
|
|
|
|
'io' => $server->io,
|
|
|
|
'cpu' => $server->cpu,
|
2021-09-13 21:02:12 -07:00
|
|
|
'threads' => $server->threads,
|
|
|
|
'oom_disabled' => $server->oom_disabled,
|
2018-02-27 21:28:43 -06:00
|
|
|
],
|
2021-08-07 13:06:45 -07:00
|
|
|
'invocation' => $this->service->handle($server, $this->user()->cannot(Permission::ACTION_STARTUP_READ, $server)),
|
2020-12-13 11:07:29 -08:00
|
|
|
'docker_image' => $server->image,
|
2020-11-02 20:52:41 -08:00
|
|
|
'egg_features' => $server->egg->inherit_features,
|
2018-03-01 20:08:27 -06:00
|
|
|
'feature_limits' => [
|
|
|
|
'databases' => $server->database_limit,
|
|
|
|
'allocations' => $server->allocation_limit,
|
2020-04-26 12:12:29 -07:00
|
|
|
'backups' => $server->backup_limit,
|
2018-03-01 20:08:27 -06:00
|
|
|
],
|
2021-01-17 16:02:11 -08:00
|
|
|
'status' => $server->status,
|
2021-01-25 19:20:51 -08:00
|
|
|
'is_transferring' => !is_null($server->transfer),
|
2018-02-27 21:28:43 -06:00
|
|
|
];
|
|
|
|
}
|
2019-09-22 15:30:53 -07:00
|
|
|
|
2020-07-09 19:17:24 -07:00
|
|
|
/**
|
|
|
|
* Returns the allocations associated with this server.
|
|
|
|
*
|
2021-08-07 13:06:45 -07:00
|
|
|
* @return \League\Fractal\Resource\Collection
|
2020-07-09 19:17:24 -07:00
|
|
|
*/
|
|
|
|
public function includeAllocations(Server $server)
|
|
|
|
{
|
2020-11-07 09:57:53 -08:00
|
|
|
// While we include this permission, we do need to actually handle it slightly different here
|
|
|
|
// for the purpose of keeping things functionally working. If the user doesn't have read permissions
|
|
|
|
// for the allocations we'll only return the primary server allocation, and any notes associated
|
|
|
|
// with it will be hidden.
|
|
|
|
//
|
|
|
|
// This allows us to avoid too much permission regression, without also hiding information that
|
|
|
|
// is generally needed for the frontend to make sense when browsing or searching results.
|
2021-08-07 13:06:45 -07:00
|
|
|
if ($this->user()->cannot(Permission::ACTION_ALLOCATION_READ, $server)) {
|
2020-11-07 09:57:53 -08:00
|
|
|
$primary = clone $server->allocation;
|
|
|
|
$primary->notes = null;
|
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
return $this->collection([$primary], new AllocationTransformer());
|
2020-08-22 16:54:12 -07:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
return $this->collection($server->allocations, new AllocationTransformer());
|
2020-07-09 19:17:24 -07:00
|
|
|
}
|
|
|
|
|
2020-08-22 15:43:28 -07:00
|
|
|
/**
|
2020-08-22 16:54:12 -07:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2020-08-22 15:43:28 -07:00
|
|
|
*/
|
|
|
|
public function includeVariables(Server $server)
|
|
|
|
{
|
2021-08-07 13:06:45 -07:00
|
|
|
if ($this->user()->cannot(Permission::ACTION_STARTUP_READ, $server)) {
|
2020-08-22 16:54:12 -07:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
return $this->collection($server->variables->where('user_viewable', true), new EggVariableTransformer());
|
2020-08-22 15:43:28 -07:00
|
|
|
}
|
|
|
|
|
2019-09-22 15:30:53 -07:00
|
|
|
/**
|
|
|
|
* Returns the egg associated with this server.
|
|
|
|
*
|
|
|
|
* @return \League\Fractal\Resource\Item
|
|
|
|
*/
|
|
|
|
public function includeEgg(Server $server)
|
|
|
|
{
|
2021-08-07 13:06:45 -07:00
|
|
|
return $this->item($server->egg, new EggTransformer());
|
2019-09-22 15:30:53 -07:00
|
|
|
}
|
2019-12-28 12:03:19 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the subusers associated with this server.
|
|
|
|
*
|
2020-08-22 16:54:12 -07:00
|
|
|
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
|
2019-12-28 12:03:19 -08:00
|
|
|
*/
|
|
|
|
public function includeSubusers(Server $server)
|
|
|
|
{
|
2021-08-07 13:06:45 -07:00
|
|
|
if ($this->user()->cannot(Permission::ACTION_USER_READ, $server)) {
|
2020-08-22 16:54:12 -07:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:06:45 -07:00
|
|
|
return $this->collection($server->subusers, new SubuserTransformer());
|
2019-12-28 12:03:19 -08:00
|
|
|
}
|
2018-02-27 21:28:43 -06:00
|
|
|
}
|