Files
pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/DatabaseController.php
T

132 lines
6.2 KiB
PHP
Raw Normal View History

2018-08-21 21:47:01 -07:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2018-08-21 21:47:01 -07:00
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
2018-08-25 14:43:21 -07:00
use Illuminate\Http\Response;
2026-10-05 18:10:18 -04:00
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
use Knuckles\Scribe\Attributes\QueryParam;
use Knuckles\Scribe\Attributes\Response as ScribeResponse;
use Knuckles\Scribe\Attributes\Subgroup;
use Pterodactyl\Contracts\Databases\DeletesDatabases;
use Pterodactyl\Contracts\Databases\DeploysServerDatabases;
use Pterodactyl\Contracts\Databases\RotatesDatabasePasswords;
use Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException;
use Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException;
use Pterodactyl\Extensions\Scribe\Attributes\ResponseFromTransformer;
use Pterodactyl\Facades\Activity;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Facades\Fractal;
2018-08-21 21:47:01 -07:00
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
2018-08-25 14:43:21 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest;
2019-07-27 15:17:50 -07:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\RotatePasswordRequest;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\Server;
use Pterodactyl\Transformers\Api\Client\DatabaseTransformer;
use Throwable;
2018-08-21 21:47:01 -07:00
2026-10-05 18:10:18 -04:00
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Server Databases', 'Create, list, rotate, and delete databases for an accessible server.')]
2018-08-21 21:47:01 -07:00
class DatabaseController extends ClientApiController
{
2026-10-05 18:10:18 -04:00
private const array DATABASE_LIMIT_ERROR = [
'errors' => [
[
'code' => 'DisplayException',
'status' => '400',
'detail' => 'Cannot create additional databases on this server: limit has been reached.',
],
],
];
2018-08-21 21:47:01 -07:00
/**
2022-10-14 10:59:20 -06:00
* Return all the databases that belong to the given server.
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
2018-08-21 21:47:01 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('List server databases', 'Returns databases attached to a server visible to the authenticated user.')]
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "password" when the user can view database passwords.', required: false, example: 'password', enum: ['password'])]
#[ResponseFromTransformer(DatabaseTransformer::class, Database::class, description: 'Server databases returned.', collection: true, resourceKey: 'server_database')]
public function index(GetDatabasesRequest $request, Server $server): array
2018-08-21 21:47:01 -07:00
{
2026-10-05 18:10:18 -04:00
return Fractal::collection($server->databases)
2018-08-21 21:47:01 -07:00
->transformWith($this->getTransformer(DatabaseTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
2018-08-21 21:47:01 -07:00
}
/**
* Create a new database for the given server and return it.
*
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*
* @throws Throwable
* @throws TooManyDatabasesException
* @throws DatabaseClientFeatureNotEnabledException
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Create server database', 'Creates a database for a server on an automatically selected database host.')]
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "password" when the user can view database passwords.', required: false, example: 'password', enum: ['password'])]
#[ResponseFromTransformer(DatabaseTransformer::class, Database::class, description: 'Database created.', resourceKey: 'server_database', include: ['password'])]
#[ScribeResponse(self::DATABASE_LIMIT_ERROR, status: 400, description: 'The server has reached its configured database limit or client database creation is disabled.')]
public function store(StoreDatabaseRequest $request, DeploysServerDatabases $deployDatabase, Server $server): array
{
2026-10-05 18:10:18 -04:00
$database = $deployDatabase->deploy($server, $request->payload());
2026-10-05 18:10:18 -04:00
Activity::event('server:database.create')->subject($database)->property('name', $database->database)->log();
2026-01-06 02:05:38 +02:00
2026-10-05 18:10:18 -04:00
return Fractal::item($database)
->parseIncludes(['password'])
->transformWith($this->getTransformer(DatabaseTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
}
2018-08-25 14:43:21 -07:00
2019-07-27 15:17:50 -07:00
/**
* Rotates the password for the given server model and returns a fresh instance to
* the caller.
*
2026-10-05 18:10:18 -04:00
*
* @return ApiPayload
*
* @throws Throwable
2019-07-27 15:17:50 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Rotate server database password', 'Rotates the password for a database attached to a server and returns the updated database.')]
#[QueryParam('include', 'string', 'Comma-separated relationships to include. Supports "password" when the user can view database passwords.', required: false, example: 'password', enum: ['password'])]
#[ResponseFromTransformer(DatabaseTransformer::class, Database::class, description: 'Database password rotated.', resourceKey: 'server_database', include: ['password'])]
public function rotatePassword(RotatePasswordRequest $request, RotatesDatabasePasswords $passwordService, Server $server, Database $database): array
2019-07-27 15:17:50 -07:00
{
2026-10-08 17:40:00 -04:00
// The action commits its own transaction around the remote user change, so the
// activity is logged afterwards instead of wrapping a change it cannot undo.
$passwordService->rotate($database);
2022-06-12 15:16:48 -04:00
Activity::event('server:database.rotate-password')
->subject($database)
->property('name', $database->database)
2026-10-08 17:40:00 -04:00
->log();
2026-01-06 02:05:38 +02:00
2026-10-05 18:10:18 -04:00
return Fractal::item($database->refresh())
2019-07-27 15:17:50 -07:00
->parseIncludes(['password'])
->transformWith($this->getTransformer(DatabaseTransformer::class))
2026-10-05 18:10:18 -04:00
->toResponseArray();
2019-07-27 15:17:50 -07:00
}
2018-08-25 14:43:21 -07:00
/**
* Removes a database from the server.
2018-08-25 14:43:21 -07:00
*/
2026-10-05 18:10:18 -04:00
#[Endpoint('Delete server database', 'Deletes a database attached to a server.')]
#[ScribeResponse(status: 204, description: 'Database deleted.')]
public function delete(DeleteDatabaseRequest $request, DeletesDatabases $deleteDatabase, Server $server, Database $database): Response
2018-08-25 14:43:21 -07:00
{
2026-10-05 18:10:18 -04:00
$deleteDatabase->delete($database);
2018-08-26 14:01:00 -07:00
Activity::event('server:database.delete')
->subject($database)
->property('name', $database->database)
->log();
2022-10-14 10:59:20 -06:00
return new Response('', Response::HTTP_NO_CONTENT);
2018-08-25 14:43:21 -07:00
}
2018-08-21 21:47:01 -07:00
}