From 2bd691efad22419c094cc71bbdcc9902c997b75f Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Thu, 25 Jan 2018 21:26:06 -0600 Subject: [PATCH] Add database list endpoint, add more resource name magic --- .../Spatie/Fractalistic/Fractal.php | 25 ++--- .../Application/ApplicationApiController.php | 2 +- .../Servers/DatabaseController.php | 44 ++++++++ app/Models/APILog.php | 7 -- app/Models/Allocation.php | 13 +-- app/Models/ApiKey.php | 6 +- app/Models/Checksum.php | 38 ------- app/Models/DaemonKey.php | 22 ---- app/Models/Database.php | 13 +-- app/Models/DatabaseHost.php | 13 +-- app/Models/Egg.php | 13 +-- app/Models/EggVariable.php | 13 +-- app/Models/Location.php | 6 + app/Models/Nest.php | 13 +-- app/Models/Node.php | 13 +-- app/Models/Pack.php | 13 +-- app/Models/Permission.php | 13 +-- app/Models/Schedule.php | 13 +-- app/Models/Server.php | 7 -- app/Models/ServerVariable.php | 13 +-- app/Models/Subuser.php | 13 +-- app/Models/Task.php | 13 +-- app/Models/User.php | 6 + app/Services/Acl/Api/AdminAcl.php | 3 +- .../Api/Application/AllocationTransformer.php | 10 ++ .../Api/Application/BaseTransformer.php | 7 ++ .../Application/DatabaseHostTransformer.php | 69 ++++++++++++ .../Application/EggVariableTransformer.php | 11 ++ .../Api/Application/LocationTransformer.php | 10 ++ .../Api/Application/NodeTransformer.php | 10 ++ .../Application/ServerDatabaseTransformer.php | 104 ++++++++++++++++++ .../Api/Application/ServerTransformer.php | 10 ++ .../Application/ServerVariableTransformer.php | 10 ++ .../Api/Application/UserTransformer.php | 10 ++ ...1_11_213943_AddApiKeyPermissionColumns.php | 6 +- routes/api-application.php | 11 ++ 36 files changed, 416 insertions(+), 187 deletions(-) create mode 100644 app/Http/Controllers/Api/Application/Servers/DatabaseController.php delete mode 100644 app/Models/Checksum.php create mode 100644 app/Transformers/Api/Application/DatabaseHostTransformer.php create mode 100644 app/Transformers/Api/Application/ServerDatabaseTransformer.php diff --git a/app/Extensions/Spatie/Fractalistic/Fractal.php b/app/Extensions/Spatie/Fractalistic/Fractal.php index 9ac0d588c..753cd4758 100644 --- a/app/Extensions/Spatie/Fractalistic/Fractal.php +++ b/app/Extensions/Spatie/Fractalistic/Fractal.php @@ -2,7 +2,7 @@ namespace Pterodactyl\Extensions\Spatie\Fractalistic; -use Illuminate\Database\Eloquent\Model; +use League\Fractal\TransformerAbstract; use League\Fractal\Serializer\JsonApiSerializer; use Spatie\Fractalistic\Fractal as SpatieFractal; use Illuminate\Contracts\Pagination\LengthAwarePaginator; @@ -31,21 +31,14 @@ class Fractal extends SpatieFractal $this->paginator = new IlluminatePaginatorAdapter($this->data); } - // Automatically set the resource name if the response data is a model - // and the resource name is available on the model. - if (is_null($this->resourceName) && $this->data instanceof Model) { - if (defined(get_class($this->data) . '::RESOURCE_NAME')) { - $this->resourceName = constant(get_class($this->data) . '::RESOURCE_NAME'); - } - } - - if (is_null($this->resourceName) && $this->data instanceof LengthAwarePaginator) { - $item = collect($this->data->items())->first(); - if ($item instanceof Model) { - if (defined(get_class($item) . '::RESOURCE_NAME')) { - $this->resourceName = constant(get_class($item) . '::RESOURCE_NAME'); - } - } + // If the resource name is not set attempt to pull it off the transformer + // itself and set it automatically. + if ( + is_null($this->resourceName) + && $this->transformer instanceof TransformerAbstract + && method_exists($this->transformer, 'getResourceName') + ) { + $this->resourceName = $this->transformer->getResourceName(); } return parent::createData(); diff --git a/app/Http/Controllers/Api/Application/ApplicationApiController.php b/app/Http/Controllers/Api/Application/ApplicationApiController.php index 3a6473553..c932c3644 100644 --- a/app/Http/Controllers/Api/Application/ApplicationApiController.php +++ b/app/Http/Controllers/Api/Application/ApplicationApiController.php @@ -16,7 +16,7 @@ abstract class ApplicationApiController extends Controller private $request; /** - * @var \Spatie\Fractalistic\Fractal + * @var \Pterodactyl\Extensions\Spatie\Fractalistic\Fractal */ protected $fractal; diff --git a/app/Http/Controllers/Api/Application/Servers/DatabaseController.php b/app/Http/Controllers/Api/Application/Servers/DatabaseController.php new file mode 100644 index 000000000..e1fec4943 --- /dev/null +++ b/app/Http/Controllers/Api/Application/Servers/DatabaseController.php @@ -0,0 +1,44 @@ +repository = $repository; + } + + /** + * Return a listing of all databases currently available to a single + * server. + * + * @param \Pterodactyl\Models\Server $server + * @return array + */ + public function index(Server $server): array + { + $databases = $this->repository->getDatabasesForServer($server->id); + + return $this->fractal->collection($databases) + ->transformWith($this->getTransformer(ServerDatabaseTransformer::class)) + ->toArray(); + } +} diff --git a/app/Models/APILog.php b/app/Models/APILog.php index a0dd5f508..359daa4ed 100644 --- a/app/Models/APILog.php +++ b/app/Models/APILog.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; diff --git a/app/Models/Allocation.php b/app/Models/Allocation.php index dfa84c017..5921c0a2b 100644 --- a/app/Models/Allocation.php +++ b/app/Models/Allocation.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Allocation extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'allocation'; + /** * The table associated with the model. * diff --git a/app/Models/ApiKey.php b/app/Models/ApiKey.php index 8379134ca..bd05454dd 100644 --- a/app/Models/ApiKey.php +++ b/app/Models/ApiKey.php @@ -50,7 +50,8 @@ class ApiKey extends Model implements CleansAttributes, ValidableContract 'user_id' => 'int', 'r_' . AdminAcl::RESOURCE_USERS => 'int', 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'int', - 'r_' . AdminAcl::RESOURCE_DATABASES => 'int', + 'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'int', + 'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'int', 'r_' . AdminAcl::RESOURCE_EGGS => 'int', 'r_' . AdminAcl::RESOURCE_LOCATIONS => 'int', 'r_' . AdminAcl::RESOURCE_NESTS => 'int', @@ -108,7 +109,8 @@ class ApiKey extends Model implements CleansAttributes, ValidableContract 'last_used_at' => 'nullable|date', 'r_' . AdminAcl::RESOURCE_USERS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_ALLOCATIONS => 'integer|min:0|max:3', - 'r_' . AdminAcl::RESOURCE_DATABASES => 'integer|min:0|max:3', + 'r_' . AdminAcl::RESOURCE_DATABASE_HOSTS => 'integer|min:0|max:3', + 'r_' . AdminAcl::RESOURCE_SERVER_DATABASES => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_EGGS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_LOCATIONS => 'integer|min:0|max:3', 'r_' . AdminAcl::RESOURCE_NESTS => 'integer|min:0|max:3', diff --git a/app/Models/Checksum.php b/app/Models/Checksum.php deleted file mode 100644 index 231b07c7f..000000000 --- a/app/Models/Checksum.php +++ /dev/null @@ -1,38 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Models; - -use Illuminate\Database\Eloquent\Model; - -class Checksum extends Model -{ - /** - * The table associated with the model. - * - * @var string - */ - protected $table = 'checksums'; - - /** - * Fields that are not mass assignable. - * - * @var array - */ - protected $guarded = ['id', 'created_at', 'updated_at']; - - /** - * Cast values to correct type. - * - * @var array - */ - protected $casts = [ - 'service' => 'integer', - ]; -} diff --git a/app/Models/DaemonKey.php b/app/Models/DaemonKey.php index 59cab2354..c4c2940a9 100644 --- a/app/Models/DaemonKey.php +++ b/app/Models/DaemonKey.php @@ -1,26 +1,4 @@ . - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ namespace Pterodactyl\Models; diff --git a/app/Models/Database.php b/app/Models/Database.php index 17d79c753..9ff1d8c17 100644 --- a/app/Models/Database.php +++ b/app/Models/Database.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Database extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'server_database'; + /** * The table associated with the model. * diff --git a/app/Models/DatabaseHost.php b/app/Models/DatabaseHost.php index 2fb339add..f42f2650d 100644 --- a/app/Models/DatabaseHost.php +++ b/app/Models/DatabaseHost.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class DatabaseHost extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'database_host'; + /** * The table associated with the model. * diff --git a/app/Models/Egg.php b/app/Models/Egg.php index 1c1b9e815..c9b2d9767 100644 --- a/app/Models/Egg.php +++ b/app/Models/Egg.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Egg extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'egg'; + /** * The table associated with the model. * diff --git a/app/Models/EggVariable.php b/app/Models/EggVariable.php index 5341dd0dd..44b074bd1 100644 --- a/app/Models/EggVariable.php +++ b/app/Models/EggVariable.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class EggVariable extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'egg_variable'; + /** * Reserved environment variable names. * diff --git a/app/Models/Location.php b/app/Models/Location.php index 30808c0dd..c680a54da 100644 --- a/app/Models/Location.php +++ b/app/Models/Location.php @@ -12,6 +12,12 @@ class Location extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'location'; + /** * The table associated with the model. * diff --git a/app/Models/Nest.php b/app/Models/Nest.php index 3631bc6e3..a6ab112e6 100644 --- a/app/Models/Nest.php +++ b/app/Models/Nest.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Nest extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'nest'; + /** * The table associated with the model. * diff --git a/app/Models/Node.php b/app/Models/Node.php index cc22a724e..ad8a9fe64 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -20,6 +13,12 @@ class Node extends Model implements CleansAttributes, ValidableContract { use Eloquence, Notifiable, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'node'; + const DAEMON_SECRET_LENGTH = 36; /** diff --git a/app/Models/Pack.php b/app/Models/Pack.php index 5d172c252..657d2f1d0 100644 --- a/app/Models/Pack.php +++ b/app/Models/Pack.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Pack extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'pack'; + /** * The table associated with the model. * diff --git a/app/Models/Permission.php b/app/Models/Permission.php index 61b67e487..6c2d94ab1 100644 --- a/app/Models/Permission.php +++ b/app/Models/Permission.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Permission extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'subuser_permission'; + /** * Should timestamps be used on this model. * diff --git a/app/Models/Schedule.php b/app/Models/Schedule.php index 215bb6d9c..83971d797 100644 --- a/app/Models/Schedule.php +++ b/app/Models/Schedule.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -19,6 +12,12 @@ class Schedule extends Model implements CleansAttributes, ValidableContract { use Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'server_schedule'; + /** * The table associated with the model. * diff --git a/app/Models/Server.php b/app/Models/Server.php index d09291e56..0d029cc61 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; diff --git a/app/Models/ServerVariable.php b/app/Models/ServerVariable.php index a17683ed6..f706d5942 100644 --- a/app/Models/ServerVariable.php +++ b/app/Models/ServerVariable.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -13,6 +6,12 @@ use Illuminate\Database\Eloquent\Model; class ServerVariable extends Model { + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'server_variable'; + /** * The table associated with the model. * diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index bcf5837fb..93c62217a 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -20,6 +13,12 @@ class Subuser extends Model implements CleansAttributes, ValidableContract { use Eloquence, Notifiable, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'server_subuser'; + /** * The table associated with the model. * diff --git a/app/Models/Task.php b/app/Models/Task.php index 82323e075..28c8e3237 100644 --- a/app/Models/Task.php +++ b/app/Models/Task.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Models; @@ -20,6 +13,12 @@ class Task extends Model implements CleansAttributes, ValidableContract { use BelongsToThrough, Eloquence, Validable; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'schedule_task'; + /** * The table associated with the model. * diff --git a/app/Models/User.php b/app/Models/User.php index 82cec4ddc..29754eff8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -37,6 +37,12 @@ class User extends Model implements const FILTER_LEVEL_ADMIN = 2; const FILTER_LEVEL_SUBUSER = 3; + /** + * The resource name for this model when it is transformed into an + * API representation using fractal. + */ + const RESOURCE_NAME = 'user'; + /** * Level of servers to display when using access() on a user. * diff --git a/app/Services/Acl/Api/AdminAcl.php b/app/Services/Acl/Api/AdminAcl.php index 701264405..54bd594fd 100644 --- a/app/Services/Acl/Api/AdminAcl.php +++ b/app/Services/Acl/Api/AdminAcl.php @@ -32,7 +32,8 @@ class AdminAcl const RESOURCE_LOCATIONS = 'locations'; const RESOURCE_NESTS = 'nests'; const RESOURCE_EGGS = 'eggs'; - const RESOURCE_DATABASES = 'databases'; + const RESOURCE_DATABASE_HOSTS = 'database_hosts'; + const RESOURCE_SERVER_DATABASES = 'server_databases'; const RESOURCE_PACKS = 'packs'; /** diff --git a/app/Transformers/Api/Application/AllocationTransformer.php b/app/Transformers/Api/Application/AllocationTransformer.php index 10d08def6..cce6acc86 100644 --- a/app/Transformers/Api/Application/AllocationTransformer.php +++ b/app/Transformers/Api/Application/AllocationTransformer.php @@ -14,6 +14,16 @@ class AllocationTransformer extends BaseTransformer */ protected $availableIncludes = ['node', 'server']; + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return Allocation::RESOURCE_NAME; + } + /** * Return a generic transformed allocation array. * diff --git a/app/Transformers/Api/Application/BaseTransformer.php b/app/Transformers/Api/Application/BaseTransformer.php index 8046df1e6..c2fdf6137 100644 --- a/app/Transformers/Api/Application/BaseTransformer.php +++ b/app/Transformers/Api/Application/BaseTransformer.php @@ -17,6 +17,13 @@ abstract class BaseTransformer extends TransformerAbstract */ private $key; + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + abstract public function getResourceName(): string; + /** * BaseTransformer constructor. */ diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php new file mode 100644 index 000000000..ef7d575bf --- /dev/null +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -0,0 +1,69 @@ + $model->id, + 'name' => $model->name, + 'host' => $model->host, + 'port' => $model->port, + 'username' => $model->username, + 'node' => $model->node_id, + 'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->created_at) + ->setTimezone(config('app.timezone')) + ->toIso8601String(), + 'updated_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->updated_at) + ->setTimezone(config('app.timezone')) + ->toIso8601String(), + ]; + } + + /** + * Include the databases associated with this host. + * + * @param \Pterodactyl\Models\DatabaseHost $model + * @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource + */ + public function includeDatabases(DatabaseHost $model) + { + if (! $this->authorize(AdminAcl::RESOURCE_SERVER_DATABASES)) { + return $this->null(); + } + + $model->loadMissing('databases'); + + return $this->collection($model->getRelation('databases'), $this->makeTransformer(ServerDatabaseTransformer::class), Database::RESOURCE_NAME); + } +} diff --git a/app/Transformers/Api/Application/EggVariableTransformer.php b/app/Transformers/Api/Application/EggVariableTransformer.php index 357f7de1b..decb038ab 100644 --- a/app/Transformers/Api/Application/EggVariableTransformer.php +++ b/app/Transformers/Api/Application/EggVariableTransformer.php @@ -2,10 +2,21 @@ namespace Pterodactyl\Transformers\Api\Application; +use Pterodactyl\Models\Egg; use Pterodactyl\Models\EggVariable; class EggVariableTransformer extends BaseTransformer { + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return Egg::RESOURCE_NAME; + } + public function transform(EggVariable $model) { return $model->toArray(); diff --git a/app/Transformers/Api/Application/LocationTransformer.php b/app/Transformers/Api/Application/LocationTransformer.php index 0c29f1801..d003053d6 100644 --- a/app/Transformers/Api/Application/LocationTransformer.php +++ b/app/Transformers/Api/Application/LocationTransformer.php @@ -14,6 +14,16 @@ class LocationTransformer extends BaseTransformer */ protected $availableIncludes = ['nodes', 'servers']; + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return Location::RESOURCE_NAME; + } + /** * Return a generic transformed pack array. * diff --git a/app/Transformers/Api/Application/NodeTransformer.php b/app/Transformers/Api/Application/NodeTransformer.php index 8a52a8687..d47183fc7 100644 --- a/app/Transformers/Api/Application/NodeTransformer.php +++ b/app/Transformers/Api/Application/NodeTransformer.php @@ -14,6 +14,16 @@ class NodeTransformer extends BaseTransformer */ protected $availableIncludes = ['allocations', 'location', 'servers']; + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return Node::RESOURCE_NAME; + } + /** * Return a node transformed into a format that can be consumed by the * external administrative API. diff --git a/app/Transformers/Api/Application/ServerDatabaseTransformer.php b/app/Transformers/Api/Application/ServerDatabaseTransformer.php new file mode 100644 index 000000000..c374798eb --- /dev/null +++ b/app/Transformers/Api/Application/ServerDatabaseTransformer.php @@ -0,0 +1,104 @@ +encrypter = $encrypter; + } + + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return Database::RESOURCE_NAME; + } + + /** + * Transform a database model in a representation for the application API. + * + * @param \Pterodactyl\Models\Database $model + * @return array + */ + public function transform(Database $model): array + { + return [ + 'id' => $model->id, + 'server' => $model->server_id, + 'host' => $model->database_host_id, + 'database' => $model->database, + 'username' => $model->username, + 'remote' => $model->remote, + 'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->created_at) + ->setTimezone(config('app.timezone')) + ->toIso8601String(), + 'updated_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->updated_at) + ->setTimezone(config('app.timezone')) + ->toIso8601String(), + ]; + } + + /** + * Include the database password in the request. + * + * @param \Pterodactyl\Models\Database $model + * @return \League\Fractal\Resource\Item + */ + public function includePassword(Database $model): Item + { + return $this->item($model, function (Database $model) { + return [ + 'id' => $model->id, + 'password' => $this->encrypter->decrypt($model->password), + ]; + }, 'database_password'); + } + + /** + * Return the database host relationship for this server database. + * + * @param \Pterodactyl\Models\Database $model + * @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource + */ + public function includeHost(Database $model) + { + if (! $this->authorize(AdminAcl::RESOURCE_DATABASE_HOSTS)) { + return $this->null(); + } + + $model->loadMissing('host'); + + return $this->item( + $model->getRelation('host'), + $this->makeTransformer(DatabaseHostTransformer::class), + DatabaseHost::RESOURCE_NAME + ); + } +} diff --git a/app/Transformers/Api/Application/ServerTransformer.php b/app/Transformers/Api/Application/ServerTransformer.php index e3c8cc56e..914449ef2 100644 --- a/app/Transformers/Api/Application/ServerTransformer.php +++ b/app/Transformers/Api/Application/ServerTransformer.php @@ -41,6 +41,16 @@ class ServerTransformer extends BaseTransformer $this->environmentService = $environmentService; } + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return Server::RESOURCE_NAME; + } + /** * Return a generic transformed server array. * diff --git a/app/Transformers/Api/Application/ServerVariableTransformer.php b/app/Transformers/Api/Application/ServerVariableTransformer.php index b965bc861..5ce592b5f 100644 --- a/app/Transformers/Api/Application/ServerVariableTransformer.php +++ b/app/Transformers/Api/Application/ServerVariableTransformer.php @@ -14,6 +14,16 @@ class ServerVariableTransformer extends BaseTransformer */ protected $availableIncludes = ['parent']; + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return ServerVariable::RESOURCE_NAME; + } + /** * Return a generic transformed server variable array. * diff --git a/app/Transformers/Api/Application/UserTransformer.php b/app/Transformers/Api/Application/UserTransformer.php index b6aac29b9..d3d680ed7 100644 --- a/app/Transformers/Api/Application/UserTransformer.php +++ b/app/Transformers/Api/Application/UserTransformer.php @@ -14,6 +14,16 @@ class UserTransformer extends BaseTransformer */ protected $availableIncludes = ['servers']; + /** + * Return the resource name for the JSONAPI output. + * + * @return string + */ + public function getResourceName(): string + { + return User::RESOURCE_NAME; + } + /** * Return a generic transformed subuser array. * diff --git a/database/migrations/2018_01_11_213943_AddApiKeyPermissionColumns.php b/database/migrations/2018_01_11_213943_AddApiKeyPermissionColumns.php index 3948972c4..cd6b60e10 100644 --- a/database/migrations/2018_01_11_213943_AddApiKeyPermissionColumns.php +++ b/database/migrations/2018_01_11_213943_AddApiKeyPermissionColumns.php @@ -23,7 +23,8 @@ class AddApiKeyPermissionColumns extends Migration $table->unsignedTinyInteger('r_locations')->default(0); $table->unsignedTinyInteger('r_nests')->default(0); $table->unsignedTinyInteger('r_eggs')->default(0); - $table->unsignedTinyInteger('r_databases')->default(0); + $table->unsignedTinyInteger('r_database_hosts')->default(0); + $table->unsignedTinyInteger('r_server_databases')->default(0); $table->unsignedTinyInteger('r_packs')->default(0); }); } @@ -52,7 +53,8 @@ class AddApiKeyPermissionColumns extends Migration 'r_locations', 'r_nests', 'r_eggs', - 'r_databases', + 'r_database_hosts', + 'r_server_databases', 'r_packs', ]); }); diff --git a/routes/api-application.php b/routes/api-application.php index 72576b920..1b72ef2da 100644 --- a/routes/api-application.php +++ b/routes/api-application.php @@ -82,4 +82,15 @@ Route::group(['prefix' => '/servers'], function () { Route::delete('/{server}', 'Servers\ServerController@delete'); Route::delete('/{server}/{force?}', 'Servers\ServerController@delete'); + + // Database Management Endpoint + Route::group(['prefix' => '/{server}/databases'], function () { + Route::get('/', 'Servers\DatabaseController@index')->name('api.application.servers.databases'); + Route::get('/{database}', 'Servers\DatabaseController@view')->name('api.application.servers.databases.view'); + + Route::post('/', 'Servers\DatabaseController@store'); + Route::patch('/{database}', 'Servers\DatabaseController@update'); + + Route::delete('/{database}', 'Servers\DatabaseController@delete'); + }); });