Files
pterodactyl-panel/app/Transformers/Api/Client/BaseClientTransformer.php
T

51 lines
1.7 KiB
PHP
Raw Normal View History

<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
namespace Pterodactyl\Transformers\Api\Client;
2026-10-05 18:10:18 -04:00
use InvalidArgumentException;
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
use Pterodactyl\Models\Server;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Models\User;
use Pterodactyl\Transformers\Api\Application\BaseTransformer as BaseApplicationTransformer;
2026-10-05 18:10:18 -04:00
use UnexpectedValueException;
abstract class BaseClientTransformer extends BaseApplicationTransformer
{
/**
* Return the user model of the user requesting this transformation.
*/
public function getUser(): User
{
2026-10-05 18:10:18 -04:00
return $this->request()->user() ?? throw new UnexpectedValueException('Client API transformers require an authenticated user.');
}
/**
* Determine if the API key loaded onto the transformer has permission
* to access a different resource. This is used when including other
* models on a transformation request.
*
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
*/
2024-10-21 19:18:20 -06:00
protected function authorize(string $ability, ?Server $server = null): bool
{
2026-10-05 18:10:18 -04:00
throw_unless($server instanceof Server, InvalidArgumentException::class, 'Expected a server when authorizing client transformer includes.');
2026-10-05 18:10:18 -04:00
return $this->getUser()->can($ability, [$server]);
}
2026-10-05 18:10:18 -04:00
/**
* @template T of BaseClientTransformer
*
* @param class-string<T> $abstract
* @return T
*/
protected function makeTransformer(string $abstract): self
{
2026-10-05 18:10:18 -04:00
throw_unless(is_subclass_of($abstract, self::class), InvalidTransformerLevelException::class, "Transformer [$abstract] must extend ".self::class.'.');
return parent::makeTransformer($abstract);
}
}