Path: blob/1.0-develop/app/Transformers/Api/Client/DatabaseTransformer.php
10283 views
<?php12namespace Pterodactyl\Transformers\Api\Client;34use Pterodactyl\Models\Database;5use League\Fractal\Resource\Item;6use Pterodactyl\Models\Permission;7use League\Fractal\Resource\NullResource;8use Illuminate\Contracts\Encryption\Encrypter;9use Pterodactyl\Contracts\Extensions\HashidsInterface;1011class DatabaseTransformer extends BaseClientTransformer12{13protected array $availableIncludes = ['password'];1415private Encrypter $encrypter;1617private HashidsInterface $hashids;1819/**20* Handle dependency injection.21*/22public function handle(Encrypter $encrypter, HashidsInterface $hashids)23{24$this->encrypter = $encrypter;25$this->hashids = $hashids;26}2728public function getResourceName(): string29{30return Database::RESOURCE_NAME;31}3233public function transform(Database $model): array34{35$model->loadMissing('host');3637return [38'id' => $this->hashids->encode($model->id),39'host' => [40'address' => $model->getRelation('host')->host,41'port' => $model->getRelation('host')->port,42],43'name' => $model->database,44'username' => $model->username,45'connections_from' => $model->remote,46'max_connections' => $model->max_connections,47];48}4950/**51* Include the database password in the request.52*/53public function includePassword(Database $database): Item|NullResource54{55if (!$this->request->user()->can(Permission::ACTION_DATABASE_VIEW_PASSWORD, $database->server)) {56return $this->null();57}5859return $this->item($database, function (Database $model) {60return [61'password' => $this->encrypter->decrypt($model->password),62];63}, 'database_password');64}65}666768