2018-02-27 21:28:43 -06:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Transformers\Api\Client;
|
2018-02-27 21:28:43 -06:00
|
|
|
|
2025-12-11 14:34:27 +01:00
|
|
|
use App\Enums\SubuserPermission;
|
2024-09-29 00:35:57 +02:00
|
|
|
use App\Models\Allocation;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Egg;
|
2024-09-29 00:35:57 +02:00
|
|
|
use App\Models\EggVariable;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Server;
|
|
|
|
|
use App\Models\Subuser;
|
2024-09-29 00:35:57 +02:00
|
|
|
use App\Services\Servers\StartupCommandService;
|
2020-08-22 15:43:28 -07:00
|
|
|
use Illuminate\Container\Container;
|
2022-10-14 10:59:20 -06:00
|
|
|
use League\Fractal\Resource\Collection;
|
2024-09-29 00:35:57 +02:00
|
|
|
use League\Fractal\Resource\Item;
|
2022-10-14 10:59:20 -06:00
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-02-27 21:28:43 -06:00
|
|
|
|
|
|
|
|
class ServerTransformer extends BaseClientTransformer
|
|
|
|
|
{
|
2022-05-04 19:35:10 -04:00
|
|
|
protected array $defaultIncludes = ['allocations', 'variables'];
|
2020-07-09 19:17:24 -07:00
|
|
|
|
2022-05-04 19:11:42 -04:00
|
|
|
protected array $availableIncludes = ['egg', 'subusers'];
|
2019-09-22 15:30:53 -07:00
|
|
|
|
2018-02-27 21:28:43 -06:00
|
|
|
public function getResourceName(): string
|
|
|
|
|
{
|
|
|
|
|
return Server::RESOURCE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-03-03 14:41:19 -05:00
|
|
|
* @param Server $server
|
2018-02-27 21:28:43 -06:00
|
|
|
*/
|
2025-03-03 14:41:19 -05:00
|
|
|
public function transform($server): array
|
2018-02-27 21:28:43 -06:00
|
|
|
{
|
2025-09-08 13:12:33 -04:00
|
|
|
/** @var StartupCommandService $service */
|
2020-08-22 15:43:28 -07:00
|
|
|
$service = Container::getInstance()->make(StartupCommandService::class);
|
|
|
|
|
|
2022-05-22 15:37:39 -04:00
|
|
|
$user = $this->request->user();
|
|
|
|
|
|
2024-09-29 00:35:57 +02:00
|
|
|
$data = [
|
2022-05-22 15:37:39 -04:00
|
|
|
'server_owner' => $user->id === $server->owner_id,
|
2024-04-13 21:51:22 -04:00
|
|
|
'identifier' => $server->uuid_short,
|
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,
|
2022-11-07 00:02:30 +01:00
|
|
|
'is_node_under_maintenance' => $server->node->isUnderMaintenance(),
|
2019-12-07 15:58:37 -08:00
|
|
|
'sftp_details' => [
|
|
|
|
|
'ip' => $server->node->fqdn,
|
2024-06-04 09:17:36 +02:00
|
|
|
'alias' => $server->node->daemon_sftp_alias,
|
2024-04-13 21:51:22 -04:00
|
|
|
'port' => $server->node->daemon_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,
|
2024-05-08 09:51:08 +02:00
|
|
|
// This field is deprecated, please use "oom_killer".
|
|
|
|
|
'oom_disabled' => !$server->oom_killer,
|
|
|
|
|
'oom_killer' => $server->oom_killer,
|
2018-02-27 21:28:43 -06:00
|
|
|
],
|
2025-12-11 14:34:27 +01:00
|
|
|
'invocation' => $service->handle($server, hideAllValues: !$user->can(SubuserPermission::StartupRead, $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,
|
|
|
|
|
// This field is deprecated, please use "status".
|
2021-01-17 15:51:56 -08:00
|
|
|
'is_suspended' => $server->isSuspended(),
|
2021-01-17 16:02:11 -08:00
|
|
|
// This field is deprecated, please use "status".
|
2021-01-25 19:20:51 -08:00
|
|
|
'is_installing' => !$server->isInstalled(),
|
|
|
|
|
'is_transferring' => !is_null($server->transfer),
|
2018-02-27 21:28:43 -06:00
|
|
|
];
|
2024-09-29 00:35:57 +02:00
|
|
|
|
|
|
|
|
if (!config('panel.editable_server_descriptions')) {
|
|
|
|
|
unset($data['description']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
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.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeAllocations(Server $server): Collection
|
2020-07-09 19:17:24 -07:00
|
|
|
{
|
2020-11-07 09:57:53 -08:00
|
|
|
$transformer = $this->makeTransformer(AllocationTransformer::class);
|
|
|
|
|
|
2022-05-22 15:37:39 -04:00
|
|
|
$user = $this->request->user();
|
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.
|
2025-12-11 14:34:27 +01:00
|
|
|
if (!$user->can(SubuserPermission::AllocationRead, $server)) {
|
2020-11-07 09:57:53 -08:00
|
|
|
$primary = clone $server->allocation;
|
|
|
|
|
$primary->notes = null;
|
|
|
|
|
|
|
|
|
|
return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);
|
2020-08-22 16:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
2020-11-07 09:57:53 -08:00
|
|
|
return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);
|
2020-07-09 19:17:24 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeVariables(Server $server): Collection|NullResource
|
2020-08-22 15:43:28 -07:00
|
|
|
{
|
2025-12-11 14:34:27 +01:00
|
|
|
if (!$this->request->user()->can(SubuserPermission::StartupRead, $server)) {
|
2020-08-22 16:54:12 -07:00
|
|
|
return $this->null();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 15:43:28 -07:00
|
|
|
return $this->collection(
|
|
|
|
|
$server->variables->where('user_viewable', true),
|
|
|
|
|
$this->makeTransformer(EggVariableTransformer::class),
|
|
|
|
|
EggVariable::RESOURCE_NAME
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-22 15:30:53 -07:00
|
|
|
/**
|
|
|
|
|
* Returns the egg associated with this server.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeEgg(Server $server): Item
|
2019-09-22 15:30:53 -07:00
|
|
|
{
|
|
|
|
|
return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
|
|
|
|
|
}
|
2019-12-28 12:03:19 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the subusers associated with this server.
|
|
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function includeSubusers(Server $server): Collection|NullResource
|
2019-12-28 12:03:19 -08:00
|
|
|
{
|
2025-12-11 14:34:27 +01:00
|
|
|
if (!$this->request->user()->can(SubuserPermission::UserRead, $server)) {
|
2020-08-22 16:54:12 -07:00
|
|
|
return $this->null();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 12:03:19 -08:00
|
|
|
return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);
|
|
|
|
|
}
|
2018-02-27 21:28:43 -06:00
|
|
|
}
|