Path: blob/1.0-develop/app/Transformers/Api/Application/BaseTransformer.php
10280 views
<?php12namespace Pterodactyl\Transformers\Api\Application;34use Carbon\CarbonImmutable;5use Carbon\CarbonInterface;6use Illuminate\Http\Request;7use Webmozart\Assert\Assert;8use Pterodactyl\Models\ApiKey;9use Illuminate\Container\Container;10use Illuminate\Database\Eloquent\Model;11use League\Fractal\TransformerAbstract;12use Pterodactyl\Services\Acl\Api\AdminAcl;1314/**15* @method array transform(Model $model)16*/17abstract class BaseTransformer extends TransformerAbstract18{19public const RESPONSE_TIMEZONE = 'UTC';2021protected Request $request;2223/**24* BaseTransformer constructor.25*/26public function __construct()27{28// Transformers allow for dependency injection on the handle method.29if (method_exists($this, 'handle')) {30Container::getInstance()->call([$this, 'handle']);31}32}3334/**35* Return the resource name for the JSONAPI output.36*/37abstract public function getResourceName(): string;3839/**40* Sets the request on the instance.41*/42public function setRequest(Request $request): self43{44$this->request = $request;4546return $this;47}4849/**50* Returns a new transformer instance with the request set on the instance.51*/52public static function fromRequest(Request $request): static53{54return app(static::class)->setRequest($request);55}5657/**58* Determine if the API key loaded onto the transformer has permission59* to access a different resource. This is used when including other60* models on a transformation request.61*62* @deprecated — prefer $user->can/cannot methods63*/64protected function authorize(string $resource): bool65{66$allowed = [ApiKey::TYPE_ACCOUNT, ApiKey::TYPE_APPLICATION];6768$token = $this->request->user()?->currentAccessToken();69if (!$token instanceof ApiKey || !in_array($token->key_type, $allowed)) {70return false;71}7273// If this is not a deprecated application token type we can only check that74// the user is a root admin at the moment. In a future release we'll be rolling75// out more specific permissions for keys.76if ($token->key_type === ApiKey::TYPE_ACCOUNT) {77return $this->request->user()->root_admin;78}7980return AdminAcl::check($token, $resource);81}8283/**84* Create a new instance of the transformer and pass along the currently85* set API key.86*87* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer88*89* @param class-string<T> $abstract90*91* @return T92*93* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException94*95* @noinspection PhpDocSignatureInspection96*/97protected function makeTransformer(string $abstract)98{99Assert::subclassOf($abstract, self::class); // @phpstan-ignore staticMethod.alreadyNarrowedType100101return $abstract::fromRequest($this->request);102}103104/**105* Return an ISO-8601 formatted timestamp to use in the API response.106*/107protected function formatTimestamp(string $timestamp): string108{109return CarbonImmutable::createFromFormat(CarbonInterface::DEFAULT_TO_STRING_FORMAT, $timestamp)110->setTimezone(self::RESPONSE_TIMEZONE)111->toAtomString();112}113}114115116