misc_pterodactyl-panel/app/Http/Controllers/Api/Client/Servers/DatabaseController.php

129 lines
4.2 KiB
PHP
Raw Normal View History

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