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

89 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\DatabaseHost;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Illuminate\Contracts\Encryption\Encrypter;
class ServerDatabaseTransformer extends BaseTransformer
{
protected array $availableIncludes = ['password', 'host'];
/**
* @var Encrypter
*/
private $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,
'server' => $model->server_id,
'host' => $model->database_host_id,
'database' => $model->database,
'username' => $model->username,
'remote' => $model->remote,
'max_connections' => $model->max_connections,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
];
}
/**
* Include the database password in the request.
*
* @return \League\Fractal\Resource\Item
*/
2020-12-06 22:17:54 +00:00
public function includePassword(Database $model)
{
return $this->item($model, function (Database $model) {
return [
'password' => $this->encrypter->decrypt($model->password),
];
}, 'database_password');
}
/**
* Return the database host relationship for this server database.
*
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
2021-01-23 20:33:34 +00:00
*
2018-05-13 16:19:35 +00:00
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeHost(Database $model)
{
2021-01-23 20:33:34 +00: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
);
}
}