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
|
|
|
|
{
|
2018-01-26 04:34:53 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
|
|
|
*/
|
|
|
|
private $databaseManagementService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Databases\DatabasePasswordService
|
|
|
|
*/
|
|
|
|
private $databasePasswordService;
|
|
|
|
|
2018-01-26 03:26:06 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $databaseManagementService
|
|
|
|
* @param \Pterodactyl\Services\Databases\DatabasePasswordService $databasePasswordService
|
2018-01-26 03:26:06 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
|
|
|
|
*/
|
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.
|
|
|
|
*
|
2018-01-26 04:34:53 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\GetServerDatabasesRequest $request
|
2020-06-24 03:07:37 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2018-01-26 03:26:06 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
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)
|
2018-01-26 03:26:06 +00:00
|
|
|
->transformWith($this->getTransformer(ServerDatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a single server database.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\GetServerDatabaseRequest $request
|
2020-06-24 03:07:37 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Database $database
|
2018-01-26 04:34:53 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
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)
|
2018-01-26 04:34:53 +00:00
|
|
|
->transformWith($this->getTransformer(ServerDatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the password for a specific server database.
|
|
|
|
*
|
2018-01-27 18:38:56 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\ServerDatabaseWriteRequest $request
|
2020-06-24 03:07:37 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @param \Pterodactyl\Models\Database $database
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2018-01-26 04:34:53 +00:00
|
|
|
*
|
2019-08-03 21:42:32 +00:00
|
|
|
* @throws \Throwable
|
2018-01-26 04:34:53 +00:00
|
|
|
*/
|
2020-06-24 03:07:37 +00:00
|
|
|
public function resetPassword(ServerDatabaseWriteRequest $request, Server $server, Database $database): JsonResponse
|
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
|
|
|
|
2020-06-24 03:07:37 +00:00
|
|
|
return JsonResponse::create([], JsonResponse::HTTP_NO_CONTENT);
|
2018-01-26 04:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database on the Panel for a given server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\StoreServerDatabaseRequest $request
|
2020-06-24 03:07:37 +00:00
|
|
|
* @param \Pterodactyl\Models\Server $server
|
2018-01-26 04:34:53 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
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-06-24 03:07:37 +00:00
|
|
|
$database = $this->databaseManagementService->create($server, $request->validated());
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
return $this->fractal->item($database)
|
|
|
|
->transformWith($this->getTransformer(ServerDatabaseTransformer::class))
|
2018-01-27 18:38:56 +00:00
|
|
|
->addMeta([
|
|
|
|
'resource' => route('api.application.servers.databases.view', [
|
|
|
|
'server' => $server->id,
|
|
|
|
'database' => $database->id,
|
|
|
|
]),
|
|
|
|
])
|
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
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\ServerDatabaseWriteRequest $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function delete(ServerDatabaseWriteRequest $request): Response
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2020-10-09 04:08:55 +00:00
|
|
|
$this->databaseManagementService->delete($request->getModel(Database::class));
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
2018-01-26 03:26:06 +00:00
|
|
|
}
|