misc_pterodactyl-panel/app/Transformers/Api/Application/ServerDatabaseTransformer.php

76 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Database;
use League\Fractal\Resource\Item;
use League\Fractal\Resource\NullResource;
use Pterodactyl\Services\Acl\Api\AdminAcl;
2022-12-15 00:05:46 +00:00
use Pterodactyl\Transformers\Api\Transformer;
use Illuminate\Contracts\Encryption\Encrypter;
2022-12-15 00:05:46 +00:00
class ServerDatabaseTransformer extends Transformer
{
2022-12-15 00:05:46 +00:00
protected array $availableIncludes = ['host', 'password'];
private Encrypter $encrypter;
/**
* Perform dependency injection.
*/
public function handle(Encrypter $encrypter)
{
$this->encrypter = $encrypter;
}
/**
* 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.
*/
public function transform(Database $model): array
{
return [
'id' => $model->id,
2022-12-15 00:05:46 +00:00
'database_host_id' => $model->database_host_id,
'server_id' => $model->server_id,
'name' => $model->database,
'username' => $model->username,
'remote' => $model->remote,
'max_connections' => $model->max_connections,
2022-12-15 00:05:46 +00:00
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
/**
* Return the database host relationship for this server database.
*/
public function includeHost(Database $model): Item|NullResource
{
2021-01-23 20:33:34 +00:00
if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {
return $this->null();
}
2022-12-15 00:05:46 +00:00
return $this->item($model->host, new DatabaseHostTransformer());
}
2022-12-15 00:05:46 +00:00
/**
* Include the database password in the request.
*/
public function includePassword(Database $model): Item
{
return $this->item($model, function (Database $model) {
return [
'password' => $this->encrypter->decrypt($model->password),
];
}, 'database_password');
}
}