From 18bdde8b81c77e7c2cbccf1c17276b402cfff3e8 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sun, 24 Jan 2021 15:30:58 -0700 Subject: [PATCH] api(application): fix requests, again --- .../Application/ApplicationApiController.php | 4 +- .../Databases/GetServerDatabaseRequest.php | 14 +---- .../Databases/GetServerDatabasesRequest.php | 11 +--- .../Databases/ServerDatabaseWriteRequest.php | 5 +- .../Databases/StoreServerDatabaseRequest.php | 31 ++-------- .../Servers/GetExternalServerRequest.php | 23 +------- .../Application/Servers/GetServerRequest.php | 11 +--- .../Servers/ServerWriteRequest.php | 11 +--- .../Servers/StoreServerRequest.php | 33 ++--------- .../UpdateServerBuildConfigurationRequest.php | 2 +- .../Servers/UpdateServerDetailsRequest.php | 2 +- .../Servers/UpdateServerStartupRequest.php | 23 ++------ app/Models/DatabaseHost.php | 11 ---- app/Models/DatabaseHostNode.php | 27 --------- ...15_155057_add_database_host_node_table.php | 56 ------------------- 15 files changed, 27 insertions(+), 237 deletions(-) delete mode 100644 app/Models/DatabaseHostNode.php delete mode 100644 database/migrations/2021_01_15_155057_add_database_host_node_table.php diff --git a/app/Http/Controllers/Api/Application/ApplicationApiController.php b/app/Http/Controllers/Api/Application/ApplicationApiController.php index 5f9b23d8f..a72ccddf7 100644 --- a/app/Http/Controllers/Api/Application/ApplicationApiController.php +++ b/app/Http/Controllers/Api/Application/ApplicationApiController.php @@ -64,10 +64,8 @@ abstract class ApplicationApiController extends Controller { /** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */ $transformer = Container::getInstance()->make($abstract); - - $apiKey = $this->request->attributes->get('api_key'); - $transformer->setKey($this->request->attributes->get('api_key')); $transformer->setRootAdmin($this->request->user()->root_admin); + $transformer->setKey($this->request->attributes->get('api_key')); Assert::isInstanceOf($transformer, BaseTransformer::class); diff --git a/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabaseRequest.php b/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabaseRequest.php index 2dff1374e..66e5781a0 100644 --- a/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabaseRequest.php +++ b/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabaseRequest.php @@ -7,19 +7,9 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class GetServerDatabaseRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVER_DATABASES; + protected string $resource = AdminAcl::RESOURCE_SERVER_DATABASES; + protected int $permission = AdminAcl::READ; - /** - * @var int - */ - protected $permission = AdminAcl::READ; - - /** - * Determine if the requested server database exists. - */ public function resourceExists(): bool { $server = $this->route()->parameter('server'); diff --git a/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabasesRequest.php b/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabasesRequest.php index 3e6cfc6fe..22349eb6e 100644 --- a/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabasesRequest.php +++ b/app/Http/Requests/Api/Application/Servers/Databases/GetServerDatabasesRequest.php @@ -7,13 +7,6 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class GetServerDatabasesRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVER_DATABASES; - - /** - * @var int - */ - protected $permission = AdminAcl::READ; + protected string $resource = AdminAcl::RESOURCE_SERVER_DATABASES; + protected int $permission = AdminAcl::READ; } diff --git a/app/Http/Requests/Api/Application/Servers/Databases/ServerDatabaseWriteRequest.php b/app/Http/Requests/Api/Application/Servers/Databases/ServerDatabaseWriteRequest.php index 917a5313f..66cec82c3 100644 --- a/app/Http/Requests/Api/Application/Servers/Databases/ServerDatabaseWriteRequest.php +++ b/app/Http/Requests/Api/Application/Servers/Databases/ServerDatabaseWriteRequest.php @@ -6,8 +6,5 @@ use Pterodactyl\Services\Acl\Api\AdminAcl; class ServerDatabaseWriteRequest extends GetServerDatabasesRequest { - /** - * @var int - */ - protected $permission = AdminAcl::WRITE; + protected int $permission = AdminAcl::WRITE; } diff --git a/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php b/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php index c37de870d..69413a14f 100644 --- a/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php +++ b/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php @@ -12,19 +12,9 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class StoreServerDatabaseRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVER_DATABASES; + protected string $resource = AdminAcl::RESOURCE_SERVER_DATABASES; + protected int $permission = AdminAcl::WRITE; - /** - * @var int - */ - protected $permission = AdminAcl::WRITE; - - /** - * Validation rules for database creation. - */ public function rules(): array { $server = $this->route()->parameter('server'); @@ -44,12 +34,7 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest ]; } - /** - * Return data formatted in the correct format for the service to consume. - * - * @return array - */ - public function validated() + public function validated(): array { return [ 'database' => $this->input('database'), @@ -58,12 +43,7 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest ]; } - /** - * Format error messages in a more understandable format for API output. - * - * @return array - */ - public function attributes() + public function attributes(): array { return [ 'host' => 'Database Host Server ID', @@ -72,9 +52,6 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest ]; } - /** - * Returns the database name in the expected format. - */ public function databaseName(): string { $server = $this->route()->parameter('server'); diff --git a/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php b/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php index 902bc60c5..bcbd87da0 100644 --- a/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php +++ b/app/Http/Requests/Api/Application/Servers/GetExternalServerRequest.php @@ -10,24 +10,10 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class GetExternalServerRequest extends ApplicationApiRequest { - /** - * @var \Pterodactyl\Models\Server - */ - private $serverModel; + private Server $serverModel; + protected string $resource = AdminAcl::RESOURCE_SERVERS; + protected int $permission = AdminAcl::READ; - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVERS; - - /** - * @var int - */ - protected $permission = AdminAcl::READ; - - /** - * Determine if the requested external user exists. - */ public function resourceExists(): bool { $repository = $this->container->make(ServerRepositoryInterface::class); @@ -43,9 +29,6 @@ class GetExternalServerRequest extends ApplicationApiRequest return true; } - /** - * Return the server model for the requested external server. - */ public function getServerModel(): Server { return $this->serverModel; diff --git a/app/Http/Requests/Api/Application/Servers/GetServerRequest.php b/app/Http/Requests/Api/Application/Servers/GetServerRequest.php index 82d12687c..adc79c2fd 100644 --- a/app/Http/Requests/Api/Application/Servers/GetServerRequest.php +++ b/app/Http/Requests/Api/Application/Servers/GetServerRequest.php @@ -7,13 +7,6 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class GetServerRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVERS; - - /** - * @var int - */ - protected $permission = AdminAcl::READ; + protected string $resource = AdminAcl::RESOURCE_SERVERS; + protected int $permission = AdminAcl::READ; } diff --git a/app/Http/Requests/Api/Application/Servers/ServerWriteRequest.php b/app/Http/Requests/Api/Application/Servers/ServerWriteRequest.php index 07c201336..2d5ab7d84 100644 --- a/app/Http/Requests/Api/Application/Servers/ServerWriteRequest.php +++ b/app/Http/Requests/Api/Application/Servers/ServerWriteRequest.php @@ -7,13 +7,6 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class ServerWriteRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVERS; - - /** - * @var int - */ - protected $permission = AdminAcl::WRITE; + protected string $resource = AdminAcl::RESOURCE_SERVERS; + protected int $permission = AdminAcl::WRITE; } diff --git a/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php b/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php index dd24e1efc..737dd54a3 100644 --- a/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php +++ b/app/Http/Requests/Api/Application/Servers/StoreServerRequest.php @@ -11,19 +11,9 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class StoreServerRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVERS; + protected string $resource = AdminAcl::RESOURCE_SERVERS; + protected int $permission = AdminAcl::WRITE; - /** - * @var int - */ - protected $permission = AdminAcl::WRITE; - - /** - * Rules to be applied to this request. - */ public function rules(): array { $rules = Server::getRules(); @@ -71,12 +61,7 @@ class StoreServerRequest extends ApplicationApiRequest ]; } - /** - * Normalize the data into a format that can be consumed by the service. - * - * @return array - */ - public function validated() + public function validated(): array { $data = parent::validated(); @@ -105,11 +90,6 @@ class StoreServerRequest extends ApplicationApiRequest ]; } - /* - * Run validation after the rules above have been applied. - * - * @param \Illuminate\Contracts\Validation\Validator $validator - */ public function withValidator(Validator $validator) { $validator->sometimes('allocation.default', [ @@ -139,12 +119,7 @@ class StoreServerRequest extends ApplicationApiRequest }); } - /** - * Return a deployment object that can be passed to the server creation service. - * - * @return \Pterodactyl\Models\Objects\DeploymentObject|null - */ - public function getDeploymentObject() + public function getDeploymentObject(): ?DeploymentObject { if (is_null($this->input('deploy'))) { return null; diff --git a/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php b/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php index 3f534b08a..27f831e9f 100644 --- a/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php +++ b/app/Http/Requests/Api/Application/Servers/UpdateServerBuildConfigurationRequest.php @@ -12,7 +12,7 @@ class UpdateServerBuildConfigurationRequest extends ServerWriteRequest */ public function rules(): array { - $rules = Server::getRulesForUpdate($this->getModel(Server::class)); + $rules = Server::getRulesForUpdate($this->route()->parameter('server')->id); return [ 'allocation' => $rules['allocation_id'], diff --git a/app/Http/Requests/Api/Application/Servers/UpdateServerDetailsRequest.php b/app/Http/Requests/Api/Application/Servers/UpdateServerDetailsRequest.php index 06bd22f5f..226f69d18 100644 --- a/app/Http/Requests/Api/Application/Servers/UpdateServerDetailsRequest.php +++ b/app/Http/Requests/Api/Application/Servers/UpdateServerDetailsRequest.php @@ -11,7 +11,7 @@ class UpdateServerDetailsRequest extends ServerWriteRequest */ public function rules(): array { - $rules = Server::getRulesForUpdate($this->getModel(Server::class)); + $rules = Server::getRulesForUpdate($this->route()->parameter('server')->id); return [ 'external_id' => $rules['external_id'], diff --git a/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php b/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php index a873e0198..7cfb0ff37 100644 --- a/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php +++ b/app/Http/Requests/Api/Application/Servers/UpdateServerStartupRequest.php @@ -8,22 +8,12 @@ use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; class UpdateServerStartupRequest extends ApplicationApiRequest { - /** - * @var string - */ - protected $resource = AdminAcl::RESOURCE_SERVERS; + protected string $resource = AdminAcl::RESOURCE_SERVERS; + protected int $permission = AdminAcl::WRITE; - /** - * @var int - */ - protected $permission = AdminAcl::WRITE; - - /** - * Validation rules to run the input against. - */ public function rules(): array { - $data = Server::getRulesForUpdate($this->getModel(Server::class)); + $data = Server::getRulesForUpdate($this->route()->parameter('server')->id); return [ 'startup' => $data['startup'], @@ -34,12 +24,7 @@ class UpdateServerStartupRequest extends ApplicationApiRequest ]; } - /** - * Return the validated data in a format that is expected by the service. - * - * @return array - */ - public function validated() + public function validated(): array { $data = parent::validated(); diff --git a/app/Models/DatabaseHost.php b/app/Models/DatabaseHost.php index 19f212aa9..f7dd60f86 100644 --- a/app/Models/DatabaseHost.php +++ b/app/Models/DatabaseHost.php @@ -10,7 +10,6 @@ namespace Pterodactyl\Models; * @property string $username * @property string $password * @property int|null $max_databases - * @property int|null $node_id * @property \Carbon\CarbonImmutable $created_at * @property \Carbon\CarbonImmutable $updated_at */ @@ -82,14 +81,4 @@ class DatabaseHost extends Model { return $this->hasMany(Database::class); } - - /** - * Gets the nodes associated with a database host. - * - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ - public function nodes() - { - return $this->belongsToMany(Node::class); - } } diff --git a/app/Models/DatabaseHostNode.php b/app/Models/DatabaseHostNode.php deleted file mode 100644 index d682a3562..000000000 --- a/app/Models/DatabaseHostNode.php +++ /dev/null @@ -1,27 +0,0 @@ -integer('node_id')->unique(); - $table->integer('database_host_id'); - - $table->unique(['node_id', 'database_host_id']); - }); - - DB::transaction(function () { - foreach (DB::select('SELECT id, node_id FROM database_hosts') as $datum) { - if (! is_null($datum->node_id)) { - DB::insert('INSERT INTO database_host_node (node_id, database_host_id) VALUES (?, ?)', [ - $datum->node_id, - $datum->id - ]); - } - } - }); - - Schema::table('database_hosts', function (Blueprint $table) { - $table->dropForeign(['node_id']); - $table->dropColumn('node_id'); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - Schema::table('database_hosts', function (Blueprint $table) { - $table->integer('node_id')->unsigned()->nullable()->after('max_databases'); - - $table->foreign('node_id')->references('id')->on('nodes')->onDelete('set null'); - }); - - Schema::dropIfExists('database_host_node'); - } -}