Massively simplify API binding logic

Changes the API internals to use normal Laravel binding which automatically supports nested-models and can determine their relationships. This removes a lot of confusingly complex internal logic and replaces it with standard Laravel code.

This also removes a deprecated "getModel" method and fully replaces it with a "parameter" method that does stricter type-checking.
This commit is contained in:
DaneEveritt 2022-05-22 14:10:01 -04:00
parent f1235c7f88
commit e313dff674
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
53 changed files with 290 additions and 604 deletions

View file

@ -80,6 +80,14 @@ class Allocation extends Model
'notes' => 'nullable|string|max:256',
];
/**
* {@inheritDoc}
*/
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Return a hashid encoded string to represent the ID of the allocation.
*

View file

@ -2,6 +2,9 @@
namespace Pterodactyl\Models;
use Illuminate\Container\Container;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
/**
* @property int $id
* @property int $server_id
@ -71,6 +74,36 @@ class Database extends Model
'password' => 'string',
];
/**
* {@inheritDoc}
*/
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Resolves the database using the ID by checking if the value provided is a HashID
* string value, or just the ID to the database itself.
*
* @param mixed $value
* @param string|null $field
*
* @return \Illuminate\Database\Eloquent\Model|null
*
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function resolveRouteBinding($value, $field = null)
{
if (is_scalar($value) && ($field ?? $this->getRouteKeyName()) === 'id') {
$value = ctype_digit((string) $value)
? $value
: Container::getInstance()->make(HashidsInterface::class)->decodeFirst($value);
}
return $this->where($field ?? $this->getRouteKeyName(), $value)->firstOrFail();
}
/**
* Gets the host database server associated with a database.
*

View file

@ -43,6 +43,14 @@ class Location extends Model
'long' => 'string|nullable|between:1,191',
];
/**
* {@inheritDoc}
*/
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Gets the nodes in a specified location.
*

View file

@ -68,6 +68,20 @@ abstract class Model extends IlluminateModel
});
}
/**
* Returns the model key to use for route model binding. By default we'll
* assume every model uses a UUID field for this. If the model does not have
* a UUID and is using a different key it should be specified on the model
* itself.
*
* You may also optionally override this on a per-route basis by declaring
* the key name in the URL definition, like "{user:id}".
*/
public function getRouteKeyName(): string
{
return 'uuid';
}
/**
* Set the model to skip validation when saving.
*

View file

@ -123,6 +123,14 @@ class Schedule extends Model
'next_run_at' => 'nullable|date',
];
/**
* {@inheritDoc}
*/
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Returns the schedule's execution crontab entry as a string.
*

View file

@ -9,6 +9,8 @@ use Znck\Eloquent\Traits\BelongsToThrough;
use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
/**
* Pterodactyl\Models\Server.
*
* @property int $id
* @property string|null $external_id
* @property string $uuid
@ -24,33 +26,75 @@ use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
* @property int $disk
* @property int $io
* @property int $cpu
* @property string $threads
* @property string|null $threads
* @property bool $oom_disabled
* @property int $allocation_id
* @property int $nest_id
* @property int $egg_id
* @property string $startup
* @property string $image
* @property int $allocation_limit
* @property int $database_limit
* @property int|null $allocation_limit
* @property int|null $database_limit
* @property int $backup_limit
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
* @property \Pterodactyl\Models\User $user
* @property \Pterodactyl\Models\Subuser[]|\Illuminate\Database\Eloquent\Collection $subusers
* @property \Pterodactyl\Models\Allocation $allocation
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
* @property \Pterodactyl\Models\Node $node
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Pterodactyl\Models\Allocation|null $allocation
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Allocation[] $allocations
* @property int|null $allocations_count
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\AuditLog[] $audits
* @property int|null $audits_count
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Backup[] $backups
* @property int|null $backups_count
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Database[] $databases
* @property int|null $databases_count
* @property \Pterodactyl\Models\Egg|null $egg
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Mount[] $mounts
* @property int|null $mounts_count
* @property \Pterodactyl\Models\Nest $nest
* @property \Pterodactyl\Models\Egg $egg
* @property \Pterodactyl\Models\EggVariable[]|\Illuminate\Database\Eloquent\Collection $variables
* @property \Pterodactyl\Models\Schedule[]|\Illuminate\Database\Eloquent\Collection $schedule
* @property \Pterodactyl\Models\Database[]|\Illuminate\Database\Eloquent\Collection $databases
* @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\ServerTransfer $transfer
* @property \Pterodactyl\Models\Backup[]|\Illuminate\Database\Eloquent\Collection $backups
* @property \Pterodactyl\Models\Mount[]|\Illuminate\Database\Eloquent\Collection $mounts
* @property \Pterodactyl\Models\AuditLog[] $audits
* @property \Pterodactyl\Models\Node $node
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
* @property int|null $notifications_count
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Schedule[] $schedules
* @property int|null $schedules_count
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Subuser[] $subusers
* @property int|null $subusers_count
* @property \Pterodactyl\Models\ServerTransfer|null $transfer
* @property \Pterodactyl\Models\User $user
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\EggVariable[] $variables
* @property int|null $variables_count
*
* @method static \Database\Factories\ServerFactory factory(...$parameters)
* @method static \Illuminate\Database\Eloquent\Builder|Server newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Server newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Server query()
* @method static \Illuminate\Database\Eloquent\Builder|Server whereAllocationId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereAllocationLimit($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereBackupLimit($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereCpu($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereDatabaseLimit($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereDisk($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereEggId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereExternalId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereImage($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereIo($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereMemory($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereNestId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereNodeId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereOomDisabled($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereOwnerId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereSkipScripts($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereStartup($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereSwap($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereThreads($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereUuid($value)
* @method static \Illuminate\Database\Eloquent\Builder|Server whereUuidShort($value)
* @mixin \Eloquent
*/
class Server extends Model
{
@ -273,7 +317,7 @@ class Server extends Model
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function schedule()
public function schedules()
{
return $this->hasMany(Schedule::class);
}

View file

@ -105,6 +105,14 @@ class Task extends Model
'continue_on_failure' => 'boolean',
];
/**
* {@inheritDoc}
*/
public function getRouteKeyName(): string
{
return $this->getKeyName();
}
/**
* Return a hashid encoded string to represent the ID of the task.
*

View file

@ -201,7 +201,7 @@ class User extends Model implements
*/
public function toVueObject(): array
{
return (new Collection($this->toArray()))->except(['id', 'external_id'])->toArray();
return Collection::make($this->toArray())->except(['id', 'external_id'])->toArray();
}
/**

View file

@ -17,6 +17,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Pterodactyl\Models\User $user
*
* @method static \Illuminate\Database\Eloquent\Builder|UserSSHKey newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserSSHKey newQuery()
* @method static \Illuminate\Database\Query\Builder|UserSSHKey onlyTrashed()
@ -32,6 +33,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @method static \Illuminate\Database\Query\Builder|UserSSHKey withTrashed()
* @method static \Illuminate\Database\Query\Builder|UserSSHKey withoutTrashed()
* @mixin \Eloquent
*
* @method static \Database\Factories\UserSSHKeyFactory factory(...$parameters)
*/
class UserSSHKey extends Model