2018-08-22 05:47:01 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
|
|
|
|
2018-08-25 22:43:21 +01:00
|
|
|
use Illuminate\Http\Response;
|
2018-08-22 05:47:01 +01:00
|
|
|
use Pterodactyl\Models\Server;
|
2018-08-26 22:01:00 +01:00
|
|
|
use Pterodactyl\Models\Database;
|
2022-05-30 00:26:28 +01:00
|
|
|
use Pterodactyl\Facades\Activity;
|
2019-07-27 23:17:50 +01:00
|
|
|
use Pterodactyl\Services\Databases\DatabasePasswordService;
|
2018-08-22 05:47:01 +01:00
|
|
|
use Pterodactyl\Transformers\Api\Client\DatabaseTransformer;
|
2018-08-25 22:43:21 +01:00
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
2018-08-23 06:29:20 +01:00
|
|
|
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
|
2018-08-22 05:47:01 +01:00
|
|
|
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
2018-08-23 06:29:20 +01:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest;
|
2018-08-25 22:43:21 +01:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest;
|
2019-07-27 23:17:50 +01:00
|
|
|
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\RotatePasswordRequest;
|
2018-08-22 05:47:01 +01:00
|
|
|
|
|
|
|
class DatabaseController extends ClientApiController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* DatabaseController constructor.
|
|
|
|
*/
|
2018-08-25 22:43:21 +01:00
|
|
|
public function __construct(
|
2022-10-14 17:59:20 +01:00
|
|
|
private DeployServerDatabaseService $deployDatabaseService,
|
|
|
|
private DatabaseManagementService $managementService,
|
|
|
|
private DatabasePasswordService $passwordService
|
2018-08-25 22:43:21 +01:00
|
|
|
) {
|
2018-08-22 05:47:01 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-14 17:59:20 +01:00
|
|
|
* Return all the databases that belong to the given server.
|
2018-08-22 05:47:01 +01:00
|
|
|
*/
|
2019-09-06 05:31:12 +01:00
|
|
|
public function index(GetDatabasesRequest $request, Server $server): array
|
2018-08-22 05:47:01 +01:00
|
|
|
{
|
2020-06-24 04:07:37 +01:00
|
|
|
return $this->fractal->collection($server->databases)
|
2018-08-22 05:47:01 +01:00
|
|
|
->transformWith($this->getTransformer(DatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-08-23 06:29:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new database for the given server and return it.
|
|
|
|
*
|
2020-06-24 04:07:37 +01:00
|
|
|
* @throws \Throwable
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Database\TooManyDatabasesException
|
2018-08-23 06:29:20 +01:00
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException
|
|
|
|
*/
|
2019-09-06 05:31:12 +01:00
|
|
|
public function store(StoreDatabaseRequest $request, Server $server): array
|
2018-08-23 06:29:20 +01:00
|
|
|
{
|
2019-09-06 05:31:12 +01:00
|
|
|
$database = $this->deployDatabaseService->handle($server, $request->validated());
|
2018-08-23 06:29:20 +01:00
|
|
|
|
2022-05-30 00:26:28 +01:00
|
|
|
Activity::event('server:database.create')
|
|
|
|
->subject($database)
|
|
|
|
->property('name', $database->database)
|
|
|
|
->log();
|
|
|
|
|
2018-08-23 06:29:20 +01:00
|
|
|
return $this->fractal->item($database)
|
|
|
|
->parseIncludes(['password'])
|
|
|
|
->transformWith($this->getTransformer(DatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
2018-08-25 22:43:21 +01:00
|
|
|
|
2019-07-27 23:17:50 +01:00
|
|
|
/**
|
|
|
|
* Rotates the password for the given server model and returns a fresh instance to
|
|
|
|
* the caller.
|
|
|
|
*
|
2019-09-06 05:31:12 +01:00
|
|
|
* @throws \Throwable
|
2019-07-27 23:17:50 +01:00
|
|
|
*/
|
2022-10-14 17:59:20 +01:00
|
|
|
public function rotatePassword(RotatePasswordRequest $request, Server $server, Database $database): array
|
2019-07-27 23:17:50 +01:00
|
|
|
{
|
2019-09-06 05:31:12 +01:00
|
|
|
$this->passwordService->handle($database);
|
2019-07-27 23:17:50 +01:00
|
|
|
$database->refresh();
|
|
|
|
|
2022-06-12 20:16:48 +01:00
|
|
|
Activity::event('server:database.rotate-password')
|
|
|
|
->subject($database)
|
|
|
|
->property('name', $database->database)
|
|
|
|
->log();
|
2022-05-30 00:26:28 +01:00
|
|
|
|
2019-07-27 23:17:50 +01:00
|
|
|
return $this->fractal->item($database)
|
|
|
|
->parseIncludes(['password'])
|
|
|
|
->transformWith($this->getTransformer(DatabaseTransformer::class))
|
|
|
|
->toArray();
|
|
|
|
}
|
|
|
|
|
2018-08-25 22:43:21 +01:00
|
|
|
/**
|
2019-09-06 05:31:12 +01:00
|
|
|
* Removes a database from the server.
|
|
|
|
*
|
2018-08-25 22:43:21 +01:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
|
|
|
*/
|
2019-09-06 05:31:12 +01:00
|
|
|
public function delete(DeleteDatabaseRequest $request, Server $server, Database $database): Response
|
2018-08-25 22:43:21 +01:00
|
|
|
{
|
2020-10-09 05:08:55 +01:00
|
|
|
$this->managementService->delete($database);
|
2018-08-26 22:01:00 +01:00
|
|
|
|
2022-05-30 00:26:28 +01:00
|
|
|
Activity::event('server:database.delete')
|
|
|
|
->subject($database)
|
|
|
|
->property('name', $database->database)
|
|
|
|
->log();
|
|
|
|
|
2022-10-14 17:59:20 +01:00
|
|
|
return new Response('', Response::HTTP_NO_CONTENT);
|
2018-08-25 22:43:21 +01:00
|
|
|
}
|
2018-08-22 05:47:01 +01:00
|
|
|
}
|