From c28e9c1ab7ea70d26b4e02c1327babc6182b9aa9 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Wed, 22 Aug 2018 22:29:20 -0700 Subject: [PATCH] Add ability to create new database through the UI --- .../Http/ClientPermissionsRequest.php | 15 ++++ .../Api/Client/Servers/DatabaseController.php | 33 +++++++- .../Requests/Api/Client/ClientApiRequest.php | 9 ++- .../Servers/Databases/GetDatabasesRequest.php | 17 ++++ .../Databases/StoreDatabaseRequest.php | 28 +++++++ .../Client/Servers/GetDatabasesRequest.php | 20 ----- resources/assets/scripts/components/Flash.vue | 16 ++-- .../assets/scripts/components/MessageBox.vue | 16 ++++ .../server/components/CreateDatabaseModal.vue | 81 +++++++++++++++++++ .../components/server/subpages/Databases.vue | 37 ++++++++- .../assets/styles/components/buttons.css | 4 + routes/api-client.php | 1 + 12 files changed, 240 insertions(+), 37 deletions(-) create mode 100644 app/Contracts/Http/ClientPermissionsRequest.php create mode 100644 app/Http/Requests/Api/Client/Servers/Databases/GetDatabasesRequest.php create mode 100644 app/Http/Requests/Api/Client/Servers/Databases/StoreDatabaseRequest.php delete mode 100644 app/Http/Requests/Api/Client/Servers/GetDatabasesRequest.php create mode 100644 resources/assets/scripts/components/MessageBox.vue create mode 100644 resources/assets/scripts/components/server/components/CreateDatabaseModal.vue diff --git a/app/Contracts/Http/ClientPermissionsRequest.php b/app/Contracts/Http/ClientPermissionsRequest.php new file mode 100644 index 000000000..6b863fce9 --- /dev/null +++ b/app/Contracts/Http/ClientPermissionsRequest.php @@ -0,0 +1,15 @@ +deployDatabaseService = $deployDatabaseService; $this->repository = $repository; } /** - * @param \Pterodactyl\Http\Requests\Api\Client\Servers\GetDatabasesRequest $request + * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Databases\GetDatabasesRequest $request * @return array */ public function index(GetDatabasesRequest $request): array @@ -39,4 +48,22 @@ class DatabaseController extends ClientApiController ->transformWith($this->getTransformer(DatabaseTransformer::class)) ->toArray(); } + + /** + * Create a new database for the given server and return it. + * + * @param \Pterodactyl\Http\Requests\Api\Client\Servers\Databases\StoreDatabaseRequest $request + * @return array + * + * @throws \Pterodactyl\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException + */ + public function store(StoreDatabaseRequest $request): array + { + $database = $this->deployDatabaseService->handle($request->getModel(Server::class), $request->validated()); + + return $this->fractal->item($database) + ->parseIncludes(['password']) + ->transformWith($this->getTransformer(DatabaseTransformer::class)) + ->toArray(); + } } diff --git a/app/Http/Requests/Api/Client/ClientApiRequest.php b/app/Http/Requests/Api/Client/ClientApiRequest.php index 92402e513..468b294f7 100644 --- a/app/Http/Requests/Api/Client/ClientApiRequest.php +++ b/app/Http/Requests/Api/Client/ClientApiRequest.php @@ -2,18 +2,23 @@ namespace Pterodactyl\Http\Requests\Api\Client; +use Pterodactyl\Models\Server; +use Pterodactyl\Contracts\Http\ClientPermissionsRequest; use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; abstract class ClientApiRequest extends ApplicationApiRequest { /** - * Determine if the current user is authorized to perform - * the requested action against the API. + * Determine if the current user is authorized to perform the requested action against the API. * * @return bool */ public function authorize(): bool { + if ($this instanceof ClientPermissionsRequest || method_exists($this, 'permission')) { + return $this->user()->can($this->permission(), $this->getModel(Server::class)); + } + return true; } } diff --git a/app/Http/Requests/Api/Client/Servers/Databases/GetDatabasesRequest.php b/app/Http/Requests/Api/Client/Servers/Databases/GetDatabasesRequest.php new file mode 100644 index 000000000..0e90d80bb --- /dev/null +++ b/app/Http/Requests/Api/Client/Servers/Databases/GetDatabasesRequest.php @@ -0,0 +1,17 @@ + 'required|alpha_dash|min:1|max:100', + 'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/', + ]; + } +} diff --git a/app/Http/Requests/Api/Client/Servers/GetDatabasesRequest.php b/app/Http/Requests/Api/Client/Servers/GetDatabasesRequest.php deleted file mode 100644 index 1c8b201ac..000000000 --- a/app/Http/Requests/Api/Client/Servers/GetDatabasesRequest.php +++ /dev/null @@ -1,20 +0,0 @@ -user()->can('view-databases', $this->getModel(Server::class)); - } -} diff --git a/resources/assets/scripts/components/Flash.vue b/resources/assets/scripts/components/Flash.vue index b42da3b1f..5a776c19b 100644 --- a/resources/assets/scripts/components/Flash.vue +++ b/resources/assets/scripts/components/Flash.vue @@ -1,22 +1,22 @@ diff --git a/resources/assets/scripts/components/server/components/CreateDatabaseModal.vue b/resources/assets/scripts/components/server/components/CreateDatabaseModal.vue new file mode 100644 index 000000000..81cefad1e --- /dev/null +++ b/resources/assets/scripts/components/server/components/CreateDatabaseModal.vue @@ -0,0 +1,81 @@ + + + + diff --git a/resources/assets/scripts/components/server/subpages/Databases.vue b/resources/assets/scripts/components/server/subpages/Databases.vue index 01cf69c30..a18af9888 100644 --- a/resources/assets/scripts/components/server/subpages/Databases.vue +++ b/resources/assets/scripts/components/server/subpages/Databases.vue @@ -3,7 +3,7 @@
-
+
@@ -12,7 +12,7 @@
-
+
@@ -40,22 +40,35 @@
+
+ +
+ + +