misc_pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/DatabaseController.php
2022-02-13 19:04:11 -05:00

96 lines
3.3 KiB
PHP

<?php
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Database;
use Pterodactyl\Services\Databases\DatabasePasswordService;
use Pterodactyl\Transformers\Api\Client\DatabaseTransformer;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\RotatePasswordRequest;
class DatabaseController extends ClientApiController
{
private DeployServerDatabaseService $deployDatabaseService;
private DatabaseManagementService $managementService;
private DatabasePasswordService $passwordService;
/**
* DatabaseController constructor.
*/
public function __construct(
DatabaseManagementService $managementService,
DatabasePasswordService $passwordService,
DeployServerDatabaseService $deployDatabaseService
) {
parent::__construct();
$this->deployDatabaseService = $deployDatabaseService;
$this->managementService = $managementService;
$this->passwordService = $passwordService;
}
/**
* Return all of the databases that belong to the given server.
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function index(GetDatabasesRequest $request, Server $server): array
{
return $this->fractal->collection($server->databases)
->transformWith(DatabaseTransformer::class)
->toArray();
}
/**
* Create a new database for the given server and return it.
*
* @throws \Throwable
* @throws \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
*/
public function store(StoreDatabaseRequest $request, Server $server): array
{
$database = $this->deployDatabaseService->handle($server, $request->validated());
return $this->fractal->item($database)
->parseIncludes(['password'])
->transformWith(DatabaseTransformer::class)
->toArray();
}
/**
* Rotates the password for the given server model and returns a fresh instance to
* the caller.
*
* @throws \Throwable
*/
public function rotatePassword(RotatePasswordRequest $request, Server $server, Database $database): array
{
$this->passwordService->handle($database);
$database->refresh();
return $this->fractal->item($database)
->parseIncludes(['password'])
->transformWith(DatabaseTransformer::class)
->toArray();
}
/**
* Removes a database from the server.
*
* @throws \Exception
*/
public function delete(DeleteDatabaseRequest $request, Server $server, Database $database): Response
{
$this->managementService->delete($database);
return $this->returnNoContent();
}
}