2018-01-26 03:26:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Database;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\Item;
|
2018-01-26 03:26:06 +00:00
|
|
|
use Pterodactyl\Models\DatabaseHost;
|
2022-10-14 16:59:20 +00:00
|
|
|
use League\Fractal\Resource\NullResource;
|
2018-01-26 03:26:06 +00:00
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
|
|
|
|
class ServerDatabaseTransformer extends BaseTransformer
|
|
|
|
{
|
2022-05-04 23:11:42 +00:00
|
|
|
protected array $availableIncludes = ['password', 'host'];
|
2018-01-26 03:26:06 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private Encrypter $encrypter;
|
2018-01-26 03:26:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2020-04-22 10:00:04 +00:00
|
|
|
'max_connections' => $model->max_connections,
|
2022-10-14 16:59:20 +00:00
|
|
|
'created_at' => $model->created_at->toAtomString(),
|
|
|
|
'updated_at' => $model->updated_at->toAtomString(),
|
2018-01-26 03:26:06 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include the database password in the request.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includePassword(Database $model): Item
|
2018-01-26 03:26:06 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*
|
2018-05-13 16:19:35 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2018-01-26 03:26:06 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function includeHost(Database $model): Item|NullResource
|
2018-01-26 03:26:06 +00:00
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) {
|
2018-01-26 03:26:06 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->loadMissing('host');
|
|
|
|
|
|
|
|
return $this->item(
|
|
|
|
$model->getRelation('host'),
|
|
|
|
$this->makeTransformer(DatabaseHostTransformer::class),
|
|
|
|
DatabaseHost::RESOURCE_NAME
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|