2018-08-22 04:47:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2018-08-25 21:43:21 +00:00
|
|
|
use Pterodactyl\Contracts\Extensions\HashidsInterface;
|
2018-08-22 04:47:01 +00:00
|
|
|
|
|
|
|
class DatabaseTransformer extends BaseClientTransformer
|
|
|
|
{
|
|
|
|
protected $availableIncludes = ['password'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
|
|
|
|
2018-08-25 21:43:21 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface
|
|
|
|
*/
|
|
|
|
private $hashids;
|
|
|
|
|
2018-08-22 04:47:01 +00:00
|
|
|
/**
|
|
|
|
* Handle dependency injection.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
2018-08-25 21:43:21 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
|
2018-08-22 04:47:01 +00:00
|
|
|
*/
|
2018-08-25 21:43:21 +00:00
|
|
|
public function handle(Encrypter $encrypter, HashidsInterface $hashids)
|
2018-08-22 04:47:01 +00:00
|
|
|
{
|
|
|
|
$this->encrypter = $encrypter;
|
2018-08-25 21:43:21 +00:00
|
|
|
$this->hashids = $hashids;
|
2018-08-22 04:47:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Database::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Pterodactyl\Models\Database $model
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function transform(Database $model): array
|
|
|
|
{
|
|
|
|
$model->loadMissing('host');
|
|
|
|
|
|
|
|
return [
|
2018-08-25 21:43:21 +00:00
|
|
|
'id' => $this->hashids->encode($model->id),
|
2018-08-22 04:47:01 +00:00
|
|
|
'host' => [
|
|
|
|
'address' => $model->getRelation('host')->host,
|
|
|
|
'port' => $model->getRelation('host')->port,
|
|
|
|
],
|
|
|
|
'name' => $model->database,
|
|
|
|
'username' => $model->username,
|
|
|
|
'connections_from' => $model->remote,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include the database password in the request.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Database $model
|
|
|
|
* @return \League\Fractal\Resource\Item
|
|
|
|
*/
|
|
|
|
public function includePassword(Database $model): Item
|
|
|
|
{
|
|
|
|
return $this->item($model, function (Database $model) {
|
|
|
|
return [
|
|
|
|
'password' => $this->encrypter->decrypt($model->password),
|
|
|
|
];
|
|
|
|
}, 'database_password');
|
|
|
|
}
|
|
|
|
}
|