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\Contracts\Repository\DatabaseRepositoryInterface;
|
|
|
|
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
|
|
|
|
{
|
2021-03-05 17:03:12 +00:00
|
|
|
private DatabaseManagementService $databaseManagementService;
|
|
|
|
private DatabasePasswordService $databasePasswordService;
|
|
|
|
private DatabaseRepositoryInterface $repository;
|
2018-01-26 03:26:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*/
|
2018-01-26 04:34:53 +00:00
|
|
|
public function __construct(
|
|
|
|
DatabaseManagementService $databaseManagementService,
|
|
|
|
DatabasePasswordService $databasePasswordService,
|
|
|
|
DatabaseRepositoryInterface $repository
|
|
|
|
) {
|
2018-01-26 03:26:06 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
2018-01-26 04:34:53 +00:00
|
|
|
$this->databaseManagementService = $databaseManagementService;
|
|
|
|
$this->databasePasswordService = $databasePasswordService;
|
2018-01-26 03:26:06 +00:00
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a listing of all databases currently available to a single
|
|
|
|
* server.
|
2021-03-05 17:03:12 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-26 03:26:06 +00:00
|
|
|
*/
|
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)
|
2021-08-07 21:32:40 +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.
|
2021-03-05 17:03:12 +00:00
|
|
|
*
|
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException
|
2018-01-26 04:34:53 +00:00
|
|
|
*/
|
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)
|
2021-08-07 21:32:40 +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
|
|
|
*/
|
2021-03-05 17:03:12 +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
|
|
|
|
2021-03-05 17:03:12 +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)
|
2021-08-07 21:32:40 +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.
|
2018-01-26 04:34:53 +00:00
|
|
|
*
|
2021-03-05 17:03:12 +00:00
|
|
|
* @throws \Exception
|
2018-01-26 04:34:53 +00:00
|
|
|
*/
|
2021-03-05 17:03:12 +00:00
|
|
|
public function delete(ServerDatabaseWriteRequest $request, Database $database): Response
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2021-01-01 22:55:30 +00:00
|
|
|
$this->databaseManagementService->delete($database);
|
2018-01-26 04:34:53 +00:00
|
|
|
|
2021-03-05 17:03:12 +00:00
|
|
|
return $this->returnNoContent();
|
2018-01-26 04:34:53 +00:00
|
|
|
}
|
2018-01-26 03:26:06 +00:00
|
|
|
}
|