2018-01-26 03:26:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
2021-08-07 20:25:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2018-01-26 03:26:06 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
class ServerDatabaseTransformer extends Transformer
|
2018-01-26 03:26:06 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $availableIncludes = ['password', 'host'];
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
protected Encrypter $encrypter;
|
2018-01-26 03:26:06 +00:00
|
|
|
|
|
|
|
public function handle(Encrypter $encrypter)
|
|
|
|
{
|
|
|
|
$this->encrypter = $encrypter;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Database::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-08-07 20:25:06 +00:00
|
|
|
'created_at' => self::formatTimestamp($model->created_at),
|
|
|
|
'updated_at' => self::formatTimestamp($model->updated_at),
|
2018-01-26 03:26:06 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
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.
|
|
|
|
*
|
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
|
|
|
*/
|
|
|
|
public function includeHost(Database $model)
|
|
|
|
{
|
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();
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:25:06 +00:00
|
|
|
return $this->item($model->host, new DatabaseHostTransformer());
|
2018-01-26 03:26:06 +00:00
|
|
|
}
|
|
|
|
}
|