2018-08-22 04:47:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Transformers\Api\Client;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Database;
|
2020-08-22 23:54:12 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2021-08-07 20:06:45 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Transformer;
|
2018-08-22 04:47:01 +00:00
|
|
|
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
|
|
|
|
2021-08-07 20:06:45 +00:00
|
|
|
class DatabaseTransformer extends Transformer
|
2018-08-22 04:47:01 +00:00
|
|
|
{
|
|
|
|
protected $availableIncludes = ['password'];
|
|
|
|
|
2021-08-07 20:06:45 +00:00
|
|
|
protected Encrypter $encrypter;
|
2018-08-22 04:47:01 +00:00
|
|
|
|
2021-08-07 20:06:45 +00:00
|
|
|
protected HashidsInterface $hashids;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function getResourceName(): string
|
|
|
|
{
|
|
|
|
return Database::RESOURCE_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function transform(Database $model): array
|
|
|
|
{
|
|
|
|
return [
|
2018-08-25 21:43:21 +00:00
|
|
|
'id' => $this->hashids->encode($model->id),
|
2018-08-22 04:47:01 +00:00
|
|
|
'host' => [
|
2021-08-07 20:06:45 +00:00
|
|
|
'address' => $model->host->host,
|
|
|
|
'port' => $model->host->port,
|
2018-08-22 04:47:01 +00:00
|
|
|
],
|
|
|
|
'name' => $model->database,
|
|
|
|
'username' => $model->username,
|
|
|
|
'connections_from' => $model->remote,
|
2020-04-22 10:17:26 +00:00
|
|
|
'max_connections' => $model->max_connections,
|
2018-08-22 04:47:01 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Include the database password in the request.
|
|
|
|
*
|
2020-08-22 23:54:12 +00:00
|
|
|
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
|
2018-08-22 04:47:01 +00:00
|
|
|
*/
|
2020-12-06 22:17:54 +00:00
|
|
|
public function includePassword(Database $database)
|
2018-08-22 04:47:01 +00:00
|
|
|
{
|
2021-08-07 20:06:45 +00:00
|
|
|
if ($this->user()->cannot(Permission::ACTION_DATABASE_VIEW_PASSWORD, $database->server)) {
|
2020-08-22 23:54:12 +00:00
|
|
|
return $this->null();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->item($database, function (Database $model) {
|
2018-08-22 04:47:01 +00:00
|
|
|
return [
|
|
|
|
'password' => $this->encrypter->decrypt($model->password),
|
|
|
|
];
|
|
|
|
}, 'database_password');
|
|
|
|
}
|
|
|
|
}
|