2018-01-12 04:49:46 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
namespace Pterodactyl\Transformers\Api\Application;
|
2018-01-12 04:49:46 +00:00
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
2022-05-22 19:37:39 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Webmozart\Assert\Assert;
|
2018-01-14 18:06:15 +00:00
|
|
|
use Pterodactyl\Models\ApiKey;
|
2018-01-12 04:49:46 +00:00
|
|
|
use Illuminate\Container\Container;
|
2018-03-04 22:30:16 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2018-01-12 04:49:46 +00:00
|
|
|
use League\Fractal\TransformerAbstract;
|
|
|
|
use Pterodactyl\Services\Acl\Api\AdminAcl;
|
|
|
|
|
2018-03-04 22:30:16 +00:00
|
|
|
/**
|
|
|
|
* @method array transform(Model $model)
|
|
|
|
*/
|
2018-01-12 04:49:46 +00:00
|
|
|
abstract class BaseTransformer extends TransformerAbstract
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
public const RESPONSE_TIMEZONE = 'UTC';
|
2018-01-13 20:08:19 +00:00
|
|
|
|
2022-05-22 19:37:39 +00:00
|
|
|
protected Request $request;
|
2018-01-26 03:26:06 +00:00
|
|
|
|
2018-01-20 03:47:06 +00:00
|
|
|
/**
|
|
|
|
* BaseTransformer constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// Transformers allow for dependency injection on the handle method.
|
|
|
|
if (method_exists($this, 'handle')) {
|
|
|
|
Container::getInstance()->call([$this, 'handle']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 04:49:46 +00:00
|
|
|
/**
|
2022-05-22 19:37:39 +00:00
|
|
|
* Return the resource name for the JSONAPI output.
|
|
|
|
*/
|
|
|
|
abstract public function getResourceName(): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the request on the instance.
|
2018-01-12 04:49:46 +00:00
|
|
|
*
|
2022-05-22 19:37:39 +00:00
|
|
|
* @return static
|
2018-01-12 04:49:46 +00:00
|
|
|
*/
|
2022-05-22 19:37:39 +00:00
|
|
|
public function setRequest(Request $request): self
|
2018-01-12 04:49:46 +00:00
|
|
|
{
|
2022-05-22 19:37:39 +00:00
|
|
|
$this->request = $request;
|
2018-01-12 04:49:46 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-22 19:37:39 +00:00
|
|
|
* Returns a new transformer instance with the request set on the instance.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
|
2018-01-12 04:49:46 +00:00
|
|
|
*/
|
2022-05-22 19:37:39 +00:00
|
|
|
public static function fromRequest(Request $request)
|
2018-01-12 04:49:46 +00:00
|
|
|
{
|
2022-05-22 19:37:39 +00:00
|
|
|
return app(static::class)->setRequest($request);
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2022-05-22 19:37:39 +00:00
|
|
|
*
|
|
|
|
* @deprecated — prefer $user->can/cannot methods
|
2018-01-12 04:49:46 +00:00
|
|
|
*/
|
|
|
|
protected function authorize(string $resource): bool
|
|
|
|
{
|
2022-05-22 19:37:39 +00:00
|
|
|
$token = $this->request->user()->currentAccessToken();
|
|
|
|
if (!$token instanceof ApiKey || $token->key_type !== ApiKey::TYPE_APPLICATION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AdminAcl::check($token, $resource, AdminAcl::READ);
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of the transformer and pass along the currently
|
|
|
|
* set API key.
|
|
|
|
*
|
2022-05-22 19:37:39 +00:00
|
|
|
* @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
|
|
|
|
*
|
|
|
|
* @param class-string<T> $abstract
|
|
|
|
*
|
|
|
|
* @return T
|
2018-02-25 21:30:56 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
|
2022-05-22 19:37:39 +00:00
|
|
|
*
|
|
|
|
* @noinspection PhpUndefinedClassInspection
|
|
|
|
* @noinspection PhpDocSignatureInspection
|
2018-01-12 04:49:46 +00:00
|
|
|
*/
|
2022-05-22 19:37:39 +00:00
|
|
|
protected function makeTransformer(string $abstract)
|
2018-01-12 04:49:46 +00:00
|
|
|
{
|
2022-05-22 19:37:39 +00:00
|
|
|
Assert::subclassOf($abstract, self::class);
|
2018-02-25 21:30:56 +00:00
|
|
|
|
2022-05-22 19:37:39 +00:00
|
|
|
return $abstract::fromRequest($this->request);
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|
2018-01-13 20:08:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an ISO-8601 formatted timestamp to use in the API response.
|
|
|
|
*/
|
|
|
|
protected function formatTimestamp(string $timestamp): string
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
return CarbonImmutable::createFromFormat(CarbonImmutable::DEFAULT_TO_STRING_FORMAT, $timestamp)
|
2018-01-13 20:08:19 +00:00
|
|
|
->setTimezone(self::RESPONSE_TIMEZONE)
|
|
|
|
->toIso8601String();
|
|
|
|
}
|
2018-01-12 04:49:46 +00:00
|
|
|
}
|