Push beginning of DB deletion stuff

This commit is contained in:
Dane Everitt 2018-08-25 14:43:21 -07:00
parent c28e9c1ab7
commit 9be2aa4ca9
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
9 changed files with 120 additions and 16 deletions

View File

@ -2,13 +2,16 @@
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Transformers\Api\Client\DatabaseTransformer;
use Pterodactyl\Services\Databases\DatabaseManagementService;
use Pterodactyl\Services\Databases\DeployServerDatabaseService;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest;
use Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest;
class DatabaseController extends ClientApiController
{
@ -22,18 +25,28 @@ class DatabaseController extends ClientApiController
*/
private $repository;
/**
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
*/
private $managementService;
/**
* DatabaseController constructor.
*
* @param \Pterodactyl\Services\Databases\DatabaseManagementService $managementService
* @param \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface $repository
* @param \Pterodactyl\Services\Databases\DeployServerDatabaseService $deployDatabaseService
*/
public function __construct(DatabaseRepositoryInterface $repository, DeployServerDatabaseService $deployDatabaseService)
{
public function __construct(
DatabaseManagementService $managementService,
DatabaseRepositoryInterface $repository,
DeployServerDatabaseService $deployDatabaseService
) {
parent::__construct();
$this->deployDatabaseService = $deployDatabaseService;
$this->repository = $repository;
$this->managementService = $managementService;
}
/**
@ -66,4 +79,16 @@ class DatabaseController extends ClientApiController
->transformWith($this->getTransformer(DatabaseTransformer::class))
->toArray();
}
/**
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Databases\DeleteDatabaseRequest $request
* @return \Illuminate\Http\Response
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function delete(DeleteDatabaseRequest $request): Response
{
return Response::create('', Response::HTTP_NO_CONTENT);
// $this->managementService->delete($request->input('database'));
}
}

View File

@ -4,9 +4,11 @@ namespace Pterodactyl\Http\Middleware\Api\Client;
use Closure;
use Illuminate\Container\Container;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
class SubstituteClientApiBindings extends ApiSubstituteBindings
{
@ -34,6 +36,20 @@ class SubstituteClientApiBindings extends ApiSubstituteBindings
}
});
$this->router->bind('database', function ($value) use ($request) {
try {
$id = Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
return Container::getInstance()->make(DatabaseRepositoryInterface::class)->findFirstWhere([
['id', '=', $id],
]);
} catch (RecordNotFoundException $exception) {
$request->attributes->set('is_missing_model', true);
return null;
}
});
return parent::handle($request, $next);
}
}

View File

@ -126,10 +126,6 @@ abstract class ApplicationApiRequest extends FormRequest
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
/**
* @return bool
*/
protected function passesAuthorization()
{
// If we have already validated we do not need to call this function

View File

@ -0,0 +1,32 @@
<?php
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Databases;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Database;
use Pterodactyl\Contracts\Http\ClientPermissionsRequest;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
class DeleteDatabaseRequest extends ClientApiRequest implements ClientPermissionsRequest
{
/**
* @return string
*/
public function permission(): string
{
return 'delete-database';
}
/**
* Determine if the provided database even belongs to this server instance.
*
* @return bool
*/
public function resourceExists(): bool
{
$server = $this->getModel(Server::class);
$database = $this->getModel(Database::class);
return $database->server_id === $server->id;
}
}

View File

@ -5,6 +5,7 @@ namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Database;
use League\Fractal\Resource\Item;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
class DatabaseTransformer extends BaseClientTransformer
{
@ -15,14 +16,21 @@ class DatabaseTransformer extends BaseClientTransformer
*/
private $encrypter;
/**
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface
*/
private $hashids;
/**
* Handle dependency injection.
*
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param \Pterodactyl\Contracts\Extensions\HashidsInterface $hashids
*/
public function handle(Encrypter $encrypter)
public function handle(Encrypter $encrypter, HashidsInterface $hashids)
{
$this->encrypter = $encrypter;
$this->hashids = $hashids;
}
/**
@ -42,6 +50,7 @@ class DatabaseTransformer extends BaseClientTransformer
$model->loadMissing('host');
return [
'id' => $this->hashids->encode($model->id),
'host' => [
'address' => $model->getRelation('host')->host,
'port' => $model->getRelation('host')->port,

View File

@ -12,7 +12,7 @@
</div>
</div>
<div v-else>
<div class="content-box mb-6" v-for="database in databases" :key="database.name">
<div class="content-box mb-6 hover:border-grey" v-for="database in databases" :key="database.name">
<div class="flex items-center text-grey-darker">
<database-icon class="flex-none text-green"></database-icon>
<div class="flex-1 px-4">
@ -38,11 +38,16 @@
<p class="uppercase text-xs text-grey pb-1 select-none">Server</p>
<p><code class="text-sm">{{database.host.address}}:{{database.host.port}}</code></p>
</div>
<div class="flex-none px-4">
<button class="btn btn-xs btn-secondary btn-red" v-on:click="deleteDatabase(database)">
<trash2-icon class="w-3 h-3 mx-1"/>
</button>
</div>
</div>
</div>
<div>
<button class="btn btn-blue btn-lg" v-on:click="showCreateModal = true">Create new database</button>
</div>
</div>
<div>
<button class="btn btn-blue btn-lg" v-on:click="showCreateModal = true">Create new database</button>
</div>
<modal :show="showCreateModal" v-on:close="showCreateModal = false">
<create-database-modal
@ -55,14 +60,14 @@
</template>
<script>
import { DatabaseIcon, LockIcon } from 'vue-feather-icons';
import { DatabaseIcon, LockIcon, Trash2Icon } from 'vue-feather-icons';
import map from 'lodash/map';
import Modal from '../../core/Modal';
import CreateDatabaseModal from '../components/CreateDatabaseModal';
export default {
name: 'databases-page',
components: {CreateDatabaseModal, Modal, DatabaseIcon, LockIcon },
components: {CreateDatabaseModal, Modal, DatabaseIcon, LockIcon, Trash2Icon },
data: function () {
return {
@ -136,6 +141,15 @@
database.showPassword = !database.showPassword;
},
deleteDatabase: function (database) {
window.axios.delete(this.route('api.client.servers.databases.delete', {
server: this.$route.params.id,
database: database.id,
}))
.then(response => console.log(response))
.catch(err => console.error(err.response));
},
}
};
</script>

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
.btn {
@apply .rounded .p-2;
transition: all 100ms ease-in-out;
/**
* Button Colors
@ -21,7 +22,9 @@
}
&.btn-red {
@apply .bg-red .border-red-dark .border .text-white;
&:not(.btn-secondary) {
@apply .bg-red .border-red-dark .border .text-white;
}
&:hover:enabled {
@apply .bg-red-dark .border-red-darker;
@ -34,6 +37,10 @@
&:hover:enabled {
@apply .border-grey .text-grey-darker;
}
&.btn-red:hover:enabled {
@apply .bg-red-dark .border-red-darker .text-white;
}
}
/**
@ -51,6 +58,10 @@
@apply .px-6 .py-3 .uppercase .tracking-wide .text-sm;
}
&.btn-xs {
@apply .py-2 .px-2 .uppercase .text-xs;
}
&:disabled, &.disabled {
opacity: 0.55;
cursor: default;

View File

@ -38,5 +38,6 @@ Route::group(['prefix' => '/servers/{server}', 'middleware' => [AuthenticateClie
Route::group(['prefix' => '/databases'], function () {
Route::get('/', 'Servers\DatabaseController@index')->name('api.client.servers.databases');
Route::post('/', 'Servers\DatabaseController@store');
Route::delete('/{database}', 'Servers\DatabaseController@delete')->name('api.client.servers.databases.delete');
});
});