Files
pterodactyl-panel/app/Transformers/Api/Application/ServerDatabaseTransformer.php
T

86 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Transformers\Api\Application;
2026-10-05 18:10:18 -04:00
use Illuminate\Support\Facades\Crypt;
2022-10-14 10:59:20 -06:00
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseField;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Services\Acl\Api\AdminAcl;
2026-10-05 18:10:18 -04:00
#[ResponseField('max_connections', 'integer', example: 0, nullable: true)]
class ServerDatabaseTransformer extends BaseTransformer
{
2026-10-05 18:10:18 -04:00
protected array $includeRelations = [
'host' => ['relation' => 'host', 'transformer' => DatabaseHostTransformer::class, 'ability' => AdminAcl::RESOURCE_DATABASE_HOSTS],
];
/**
2026-10-05 18:10:18 -04:00
* @var list<string>
*/
2026-10-05 18:10:18 -04:00
protected array $availableIncludes = ['password', 'host'];
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Database::RESOURCE_NAME;
}
/**
* Transform a database model in a representation for the application API.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*/
public function transform(Database $model): array
{
return [
'id' => $model->id,
'server' => $model->server_id,
'host' => $model->database_host_id,
'database' => $model->database,
'username' => $model->username,
'remote' => $model->remote,
'max_connections' => $model->max_connections,
2022-10-14 10:59:20 -06:00
'created_at' => $model->created_at->toAtomString(),
'updated_at' => $model->updated_at->toAtomString(),
];
}
/**
* Include the database password in the request.
*/
2022-10-14 10:59:20 -06:00
public function includePassword(Database $model): Item
{
2026-10-05 18:10:18 -04:00
return $this->item($model, fn (Database $model): array => [
'password' => Crypt::decrypt($model->password),
], 'database_password');
}
/**
* Return the database host relationship for this server database.
*
2026-10-05 18:10:18 -04:00
* @throws InvalidTransformerLevelException
*/
2022-10-14 10:59:20 -06:00
public function includeHost(Database $model): Item|NullResource
{
2026-10-05 18:10:18 -04:00
if (! $this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {
return $this->null();
}
$model->loadMissing('host');
return $this->item(
$model->getRelation('host'),
$this->makeTransformer(DatabaseHostTransformer::class),
DatabaseHost::RESOURCE_NAME
);
}
}