api(application): v2 backport

This commit is contained in:
Matthew Penner 2022-12-14 17:05:46 -07:00
parent 4cd0bee231
commit 67bf3e342e
No known key found for this signature in database
172 changed files with 2922 additions and 1579 deletions

68
app/Models/AdminRole.php Normal file
View file

@ -0,0 +1,68 @@
<?php
namespace Pterodactyl\Models;
/**
* @property int $id
* @property string $name
* @property string|null $description
* @property int $sort_id
* @property array $permissions
*/
class AdminRole extends Model
{
/**
* The resource name for this model when it is transformed into an
* API representation using fractal.
*/
public const RESOURCE_NAME = 'admin_role';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'admin_roles';
/**
* Fields that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'description',
'sort_id',
];
/**
* Cast values to correct type.
*
* @var array
*/
protected $casts = [
'sort_id' => 'int',
'permissions' => 'array',
];
public static array $validationRules = [
'name' => 'required|string|max:64',
'description' => 'nullable|string|max:255',
'sort_id' => 'sometimes|numeric',
];
/**
* @var bool
*/
public $timestamps = false;
/**
* Gets the permissions associated with a admin role.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function permissions()
{
return $this->hasMany(Permission::class);
}
}

View file

@ -1,80 +0,0 @@
<?php
namespace Pterodactyl\Models;
use Ramsey\Uuid\Uuid;
use Illuminate\Http\Request;
use Illuminate\Container\Container;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @deprecated this class will be dropped in a future version, use the activity log
*/
class AuditLog extends Model
{
public const UPDATED_AT = null;
public static array $validationRules = [
'uuid' => 'required|uuid',
'action' => 'required|string|max:191',
'subaction' => 'nullable|string|max:191',
'device' => 'array',
'device.ip_address' => 'ip',
'device.user_agent' => 'string',
'metadata' => 'array',
];
protected $table = 'audit_logs';
protected bool $immutableDates = true;
protected $casts = [
'is_system' => 'bool',
'device' => 'array',
'metadata' => 'array',
];
protected $guarded = [
'id',
'created_at',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function server(): BelongsTo
{
return $this->belongsTo(Server::class);
}
/**
* Creates a new AuditLog model and returns it, attaching device information and the
* currently authenticated user if available. This model is not saved at this point, so
* you can always make modifications to it as needed before saving.
*
* @deprecated
*/
public static function instance(string $action, array $metadata, bool $isSystem = false): self
{
/** @var \Illuminate\Http\Request $request */
$request = Container::getInstance()->make('request');
if ($isSystem || !$request instanceof Request) {
$request = null;
}
return (new self())->fill([
'uuid' => Uuid::uuid4()->toString(),
'is_system' => $isSystem,
'user_id' => ($request && $request->user()) ? $request->user()->id : null,
'server_id' => null,
'action' => $action,
'device' => $request ? [
'ip_address' => $request->getClientIp() ?? '127.0.0.1',
'user_agent' => $request->userAgent() ?? '',
] : [],
'metadata' => $metadata,
]);
}
}

View file

@ -22,7 +22,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property \Carbon\CarbonImmutable $updated_at
* @property \Carbon\CarbonImmutable|null $deleted_at
* @property \Pterodactyl\Models\Server $server
* @property \Pterodactyl\Models\AuditLog[] $audits
*/
class Backup extends Model
{

View file

@ -35,7 +35,7 @@ use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
* @property int $allocation_id
* @property int $nest_id
* @property int $egg_id
* @property string $startup
* @property string|null $startup
* @property string $image
* @property int|null $allocation_limit
* @property int|null $database_limit
@ -55,6 +55,7 @@ use Pterodactyl\Exceptions\Http\Server\ServerStateConflictException;
* @property \Pterodactyl\Models\Egg|null $egg
* @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Mount[] $mounts
* @property int|null $mounts_count
* @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\Nest $nest
* @property \Pterodactyl\Models\Node $node
* @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications

View file

@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Builder;
use Pterodactyl\Models\Traits\HasAccessTokens;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Pterodactyl\Traits\Helpers\AvailableLanguages;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\Access\Authorizable;
@ -29,11 +30,10 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
* @property string $uuid
* @property string $username
* @property string $email
* @property string|null $name_first
* @property string|null $name_last
* @property string $password
* @property string|null $remember_token
* @property string $language
* @property int|null $admin_role_id
* @property bool $root_admin
* @property bool $use_totp
* @property string|null $totp_secret
@ -119,8 +119,6 @@ class User extends Model implements
'external_id',
'username',
'email',
'name_first',
'name_last',
'password',
'language',
'use_totp',
@ -169,8 +167,6 @@ class User extends Model implements
'email' => 'required|email|between:1,191|unique:users,email',
'external_id' => 'sometimes|nullable|string|max:191|unique:users,external_id',
'username' => 'required|between:1,191|unique:users,username',
'name_first' => 'required|string|between:1,191',
'name_last' => 'required|string|between:1,191',
'password' => 'sometimes|nullable|string',
'root_admin' => 'boolean',
'language' => 'string',
@ -193,11 +189,15 @@ class User extends Model implements
}
/**
* Return the user model in a format that can be passed over to Vue templates.
* Return the user model in a format that can be passed over to React templates.
*/
public function toVueObject(): array
public function toReactObject(): array
{
return Collection::make($this->toArray())->except(['id', 'external_id'])->toArray();
$object = Collection::make($this->toArray())->except(['id', 'external_id'])->toArray();
$object['avatar_url'] = $this->avatarURL();
$object['role_name'] = $this->adminRoleName();
return $object;
}
/**
@ -231,6 +231,29 @@ class User extends Model implements
return trim($this->name_first . ' ' . $this->name_last);
}
public function avatarURL(): string
{
return 'https://www.gravatar.com/avatar/' . md5($this->email) . '.jpg';
}
/**
* Gets the name of the role assigned to a user.
*/
public function adminRoleName(): ?string
{
$role = $this->adminRole;
if (is_null($role)) {
return $this->root_admin ? 'None' : null;
}
return $role->name;
}
public function adminRole(): HasOne
{
return $this->hasOne(AdminRole::class, 'id', 'admin_role_id');
}
/**
* Returns all servers that a user owns.
*/