2018-01-26 03:26:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Application\Servers;
|
|
|
|
|
2018-01-26 04:34:53 +00:00
|
|
|
use Illuminate\Http\Response;
|
2018-01-26 03:26:06 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2018-01-26 04:34:53 +00:00
|
|
|
use Pterodactyl\Models\Database;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Pterodactyl\Services\Databases\DatabasePasswordService;
|
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
2018-01-26 03:26:06 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\ServerDatabaseTransformer;
|
|
|
|
use Pterodactyl\Http\Controllers\Api\Application\ApplicationApiController;
|
2018-01-26 04:34:53 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\Databases\GetServerDatabaseRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\Databases\GetServerDatabasesRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\Databases\ServerDatabaseWriteRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Application\Servers\Databases\StoreServerDatabaseRequest;
|
2018-01-26 03:26:06 +00:00
|
|
|
|
|
|
|
class DatabaseController extends ApplicationApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*/
|
2018-01-26 04:34:53 +00:00
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private DatabaseManagementService $databaseManagementService,
|
|
|
|
private DatabasePasswordService $databasePasswordService
|
2018-01-26 04:34:53 +00:00
|
|
|
) {
|
2018-01-26 03:26:06 +00:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a listing of all databases currently available to a single
|
|
|
|
* server.
|
|
|
|
*/
|
2020-06-24 03:07:37 +00:00
|
|
|
public function index(GetServerDatabasesRequest $request, Server $server): array
|
2018-01-26 03:26:06 +00:00
|
|
|
{
|
2020-06-24 03:07:37 +00:00
|
|
|
return $this->fractal->collection($server->databases)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(ServerDatabaseTransformer::class)
|
2018-01-26 03:26:06 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a single server database.
|
|
|
|
*/
|
2020-06-24 03:07:37 +00:00
|
|
|
public function view(GetServerDatabaseRequest $request, Server $server, Database $database): array
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2020-06-24 03:07:37 +00:00
|
|
|
return $this->fractal->item($database)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(ServerDatabaseTransformer::class)
|
2018-01-26 04:34:53 +00:00
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the password for a specific server database.
|
|
|
|
*
|
2019-08-03 21:42:32 +00:00
|
|
|
* @throws \Throwable
|
2018-01-26 04:34:53 +00:00
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function resetPassword(ServerDatabaseWriteRequest $request, Server $server, Database $database): Response
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2020-06-24 03:07:37 +00:00
|
|
|
$this->databasePasswordService->handle($database);
|
2018-01-26 04:34:53 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->returnNoContent();
|
2018-01-26 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database on the Panel for a given server.
|
|
|
|
*
|
2020-06-24 03:07:37 +00:00
|
|
|
* @throws \Throwable
|
2018-01-26 04:34:53 +00:00
|
|
|
*/
|
2020-06-24 03:07:37 +00:00
|
|
|
public function store(StoreServerDatabaseRequest $request, Server $server): JsonResponse
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2020-10-11 18:59:46 +00:00
|
|
|
$database = $this->databaseManagementService->create($server, array_merge($request->validated(), [
|
|
|
|
'database' => $request->databaseName(),
|
|
|
|
]));
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($database)
|
2022-12-15 00:05:46 +00:00
|
|
|
->transformWith(ServerDatabaseTransformer::class)
|
2020-06-24 03:07:37 +00:00
|
|
|
->respond(Response::HTTP_CREATED);
|
2018-01-26 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-27 18:38:56 +00:00
|
|
|
* Handle a request to delete a specific server database from the Panel.
|
2022-12-15 00:05:46 +00:00
|
|
|
*
|
|
|
|
* @throws \Exception
|
2018-01-26 04:34:53 +00:00
|
|
|
*/
|
2022-12-15 00:05:46 +00:00
|
|
|
public function delete(ServerDatabaseWriteRequest $request, Database $database): Response
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2022-05-22 18:10:01 +00:00
|
|
|
$this->databaseManagementService->delete($database);
|
2018-01-26 04:34:53 +00:00
|
|
|
|
2022-12-15 00:05:46 +00:00
|
|
|
return $this->returnNoContent();
|
2018-01-26 04:34:53 +00:00
|
|
|
}
|
2018-01-26 03:26:06 +00:00
|
|
|
}
|