api(application): fix requests, again

This commit is contained in:
Matthew Penner 2021-01-24 15:30:58 -07:00
parent da3c7fa455
commit 18bdde8b81
15 changed files with 27 additions and 237 deletions

View file

@ -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);

View file

@ -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');

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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');

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;

View file

@ -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'],

View file

@ -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'],

View file

@ -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();

View file

@ -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);
}
}

View file

@ -1,27 +0,0 @@
<?php
namespace Pterodactyl\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $node_id
* @property int $database_host_id
*/
class DatabaseHostNode extends Model
{
/**
* @var string
*/
protected $table = 'database_host_node';
/**
* @var null
*/
protected $primaryKey = null;
/**
* @var bool
*/
public $incrementing = false;
}

View file

@ -1,56 +0,0 @@
<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddDatabaseHostNodeTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('database_host_node', function (Blueprint $table) {
$table->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');
}
}