2026-10-11 18:33:19 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Resources\Api\Client;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use InvalidArgumentException;
|
|
|
|
|
use Pterodactyl\Data\ServerState;
|
2026-10-11 22:11:08 -04:00
|
|
|
use Pterodactyl\Exceptions\Resource\InvalidResourceLevelException;
|
2026-10-11 18:33:19 -04:00
|
|
|
use Pterodactyl\Http\Resources\Api\ApiResource;
|
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
|
use Pterodactyl\Models\User;
|
|
|
|
|
use UnexpectedValueException;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-10-11 20:17:24 -04:00
|
|
|
* @template TResource of Model|ServerState|array<mixed>
|
2026-10-11 18:33:19 -04:00
|
|
|
*
|
|
|
|
|
* @extends ApiResource<TResource>
|
|
|
|
|
*/
|
|
|
|
|
abstract class ClientResource extends ApiResource
|
|
|
|
|
{
|
|
|
|
|
/** @var string|null */
|
|
|
|
|
public static $wrap;
|
|
|
|
|
|
|
|
|
|
public function getUser(): User
|
|
|
|
|
{
|
|
|
|
|
return $this->request()->user() ?? throw new UnexpectedValueException('Client API resources require an authenticated user.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
|
|
|
|
|
*/
|
|
|
|
|
protected function authorize(string $ability, ?Server $server = null): bool
|
|
|
|
|
{
|
|
|
|
|
throw_unless($server instanceof Server, InvalidArgumentException::class, 'Expected a server when authorizing client resource includes.');
|
|
|
|
|
|
|
|
|
|
return $this->getUser()->can($ability, [$server]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @template TNested of ApiResource<*>
|
|
|
|
|
*
|
|
|
|
|
* @param class-string<TNested> $resource
|
2026-10-11 20:17:24 -04:00
|
|
|
* @param Model|ServerState|array<mixed> $data
|
2026-10-11 18:33:19 -04:00
|
|
|
* @return TNested
|
|
|
|
|
*
|
2026-10-11 22:11:08 -04:00
|
|
|
* @throws InvalidResourceLevelException
|
2026-10-11 18:33:19 -04:00
|
|
|
*/
|
|
|
|
|
protected function makeResource(string $resource, Model|ServerState|array $data): ApiResource
|
|
|
|
|
{
|
2026-10-11 22:11:08 -04:00
|
|
|
throw_unless(is_subclass_of($resource, self::class), InvalidResourceLevelException::class, "Resource [$resource] must extend ".self::class.'.');
|
2026-10-11 18:33:19 -04:00
|
|
|
|
|
|
|
|
return parent::makeResource($resource, $data);
|
|
|
|
|
}
|
|
|
|
|
}
|