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.
|
|
|
|
*
|
2018-01-26 04:34:53 +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
|
2018-01-26 03:26:06 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function index(GetServerDatabasesRequest $request): array
|
2018-01-26 03:26:06 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
$databases = $this->repository->getDatabasesForServer($request->getModel(Server::class)->id);
|
2018-01-26 03:26:06 +00:00
|
|
|
|
|
|
|
return $this->fractal->collection($databases)
|
|
|
|
->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
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function view(GetServerDatabaseRequest $request): array
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
return $this->fractal->item($request->getModel(Database::class))
|
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
|
2018-01-26 04:34:53 +00:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function resetPassword(ServerDatabaseWriteRequest $request): Response
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
$this->databasePasswordService->handle($request->getModel(Database::class), str_random(24));
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database on the Panel for a given server.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Application\Servers\Databases\StoreServerDatabaseRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2018-01-27 18:38:56 +00:00
|
|
|
public function store(StoreServerDatabaseRequest $request): JsonResponse
|
2018-01-26 04:34:53 +00:00
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
$server = $request->getModel(Server::class);
|
2018-01-26 04:34:53 +00:00
|
|
|
$database = $this->databaseManagementService->create($server->id, $request->validated());
|
|
|
|
|
|
|
|
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,
|
|
|
|
]),
|
|
|
|
])
|
2018-01-26 04:34:53 +00:00
|
|
|
->respond(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2018-01-27 18:38:56 +00:00
|
|
|
$this->databaseManagementService->delete($request->getModel(Database::class)->id);
|
2018-01-26 04:34:53 +00:00
|
|
|
|
|
|
|
return response('', 204);
|
|
|
|
}
|
2018-01-26 03:26:06 +00:00
|
|
|
}
|