2026-10-11 18:41:30 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Resources\Api\Admin;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Crypt;
|
|
|
|
|
use Pterodactyl\Extensions\Scribe\Attributes\ResponseField;
|
|
|
|
|
use Pterodactyl\Facades\Hashids;
|
|
|
|
|
use Pterodactyl\Http\Resources\Api\ApiResource;
|
|
|
|
|
use Pterodactyl\Http\Resources\Api\Includes\IncludedResource;
|
|
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @extends AdminResource<Database>
|
|
|
|
|
*/
|
|
|
|
|
#[ResponseField('max_connections', 'integer', example: 0, nullable: true)]
|
|
|
|
|
class ServerDatabaseResource extends AdminResource
|
|
|
|
|
{
|
|
|
|
|
protected array $eagerLoads = ['host'];
|
|
|
|
|
|
|
|
|
|
protected array $availableIncludes = ['password'];
|
|
|
|
|
|
|
|
|
|
public function resourceName(): string
|
|
|
|
|
{
|
|
|
|
|
return Database::RESOURCE_NAME;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function toArray(Request $request): array
|
|
|
|
|
{
|
|
|
|
|
$this->resource->loadMissing('host');
|
|
|
|
|
$host = $this->resource->host;
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'id' => Hashids::encode($this->resource->id),
|
|
|
|
|
'host' => [
|
|
|
|
|
'address' => $host->host,
|
|
|
|
|
'port' => $host->port,
|
|
|
|
|
],
|
|
|
|
|
'name' => $this->resource->database,
|
|
|
|
|
'username' => $this->resource->username,
|
|
|
|
|
'connections_from' => $this->resource->remote,
|
|
|
|
|
'max_connections' => $this->resource->max_connections,
|
|
|
|
|
'relationships' => [],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function resolveInclude(string $include): ?IncludedResource
|
|
|
|
|
{
|
|
|
|
|
return match ($include) {
|
|
|
|
|
'password' => $this->includePassword(),
|
|
|
|
|
default => parent::resolveInclude($include),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* No per-server subuser permission check here; admin API access is gated on root-admin alone.
|
|
|
|
|
*
|
|
|
|
|
* @return ApiResource<Database>
|
|
|
|
|
*/
|
|
|
|
|
private function includePassword(): ApiResource
|
|
|
|
|
{
|
|
|
|
|
return $this->item($this->resource, fn (Database $model): array => [
|
2026-10-11 20:11:13 -04:00
|
|
|
'password' => Crypt::decrypt($model->password),
|
2026-10-11 18:41:30 -04:00
|
|
|
], 'database_password');
|
|
|
|
|
}
|
|
|
|
|
}
|