Use a standardized transformer base; replace all client transformers to call that base

This commit is contained in:
Dane Everitt
2021-08-07 13:06:45 -07:00
parent 2203a4d87e
commit cf500a1a54
25 changed files with 255 additions and 379 deletions

View File

@@ -3,23 +3,19 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\User;
use Pterodactyl\Transformers\Api\Transformer;
class AccountTransformer extends BaseClientTransformer
class AccountTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return 'user';
return User::RESOURCE_NAME;
}
/**
* Return basic information about the currently logged in user.
*
* @return array
*/
public function transform(User $model)
public function transform(User $model): array
{
return [
'id' => $model->id,

View File

@@ -3,23 +3,16 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Transformers\Api\Transformer;
class AllocationTransformer extends BaseClientTransformer
class AllocationTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return 'allocation';
return Allocation::RESOURCE_NAME;
}
/**
* Return basic information about the currently logged in user.
*
* @return array
*/
public function transform(Allocation $model)
public function transform(Allocation $model): array
{
return [
'id' => $model->id,

View File

@@ -3,18 +3,16 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Backup;
use Pterodactyl\Transformers\Api\Transformer;
class BackupTransformer extends BaseClientTransformer
class BackupTransformer extends Transformer
{
public function getResourceName(): string
{
return Backup::RESOURCE_NAME;
}
/**
* @return array
*/
public function transform(Backup $backup)
public function transform(Backup $backup): array
{
return [
'uuid' => $backup->uuid,
@@ -24,8 +22,8 @@ class BackupTransformer extends BaseClientTransformer
'ignored_files' => $backup->ignored_files,
'checksum' => $backup->checksum,
'bytes' => $backup->bytes,
'created_at' => $backup->created_at->toIso8601String(),
'completed_at' => $backup->completed_at ? $backup->completed_at->toIso8601String() : null,
'created_at' => self::formatTimestamp($backup->created_at),
'completed_at' => self::formatTimestamp($backup->completed_at),
];
}
}

View File

@@ -1,69 +0,0 @@
<?php
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\User;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Illuminate\Container\Container;
use Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException;
use Pterodactyl\Transformers\Api\Application\BaseTransformer as BaseApplicationTransformer;
abstract class BaseClientTransformer extends BaseApplicationTransformer
{
/**
* @var \Pterodactyl\Models\User
*/
private $user;
/**
* Return the user model of the user requesting this transformation.
*/
public function getUser(): User
{
return $this->user;
}
/**
* Set the user model of the user requesting this transformation.
*/
public function setUser(User $user)
{
$this->user = $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.
*
* @param \Pterodactyl\Models\Server $server
*/
protected function authorize(string $ability, Server $server = null): bool
{
Assert::isInstanceOf($server, Server::class);
return $this->getUser()->can($ability, [$server]);
}
/**
* Create a new instance of the transformer and pass along the currently
* set API key.
*
* @return self
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
protected function makeTransformer(string $abstract, array $parameters = [])
{
/** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */
$transformer = Container::getInstance()->makeWith($abstract, $parameters);
// $transformer->setKey($this->getKey());
if (!$transformer instanceof self) {
throw new InvalidTransformerLevelException('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__);
}
return $transformer;
}
}

View File

@@ -4,26 +4,18 @@ namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Database;
use Pterodactyl\Models\Permission;
use Pterodactyl\Transformers\Api\Transformer;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Contracts\Extensions\HashidsInterface;
class DatabaseTransformer extends BaseClientTransformer
class DatabaseTransformer extends Transformer
{
protected $availableIncludes = ['password'];
/**
* @var \Illuminate\Contracts\Encryption\Encrypter
*/
private $encrypter;
protected Encrypter $encrypter;
/**
* @var \Pterodactyl\Contracts\Extensions\HashidsInterface
*/
private $hashids;
protected HashidsInterface $hashids;
/**
* Handle dependency injection.
*/
public function handle(Encrypter $encrypter, HashidsInterface $hashids)
{
$this->encrypter = $encrypter;
@@ -37,13 +29,11 @@ class DatabaseTransformer extends BaseClientTransformer
public function transform(Database $model): array
{
$model->loadMissing('host');
return [
'id' => $this->hashids->encode($model->id),
'host' => [
'address' => $model->getRelation('host')->host,
'port' => $model->getRelation('host')->port,
'address' => $model->host->host,
'port' => $model->host->port,
],
'name' => $model->database,
'username' => $model->username,
@@ -59,7 +49,7 @@ class DatabaseTransformer extends BaseClientTransformer
*/
public function includePassword(Database $database)
{
if (!$this->getUser()->can(Permission::ACTION_DATABASE_VIEW_PASSWORD, $database->server)) {
if ($this->user()->cannot(Permission::ACTION_DATABASE_VIEW_PASSWORD, $database->server)) {
return $this->null();
}

View File

@@ -3,21 +3,16 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Egg;
use Pterodactyl\Transformers\Api\Transformer;
class EggTransformer extends BaseClientTransformer
class EggTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Egg::RESOURCE_NAME;
}
/**
* @return array
*/
public function transform(Egg $egg)
public function transform(Egg $egg): array
{
return [
'uuid' => $egg->uuid,

View File

@@ -4,18 +4,16 @@ namespace Pterodactyl\Transformers\Api\Client;
use BadMethodCallException;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Transformers\Api\Transformer;
class EggVariableTransformer extends BaseClientTransformer
class EggVariableTransformer extends Transformer
{
public function getResourceName(): string
{
return EggVariable::RESOURCE_NAME;
}
/**
* @return array
*/
public function transform(EggVariable $variable)
public function transform(EggVariable $variable): array
{
// This guards against someone incorrectly retrieving variables (haha, me) and then passing
// them into the transformer and along to the user. Just throw an exception and break the entire

View File

@@ -3,30 +3,24 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\PersonalAccessToken;
use Pterodactyl\Transformers\Api\Transformer;
class PersonalAccessTokenTransformer extends BaseClientTransformer
class PersonalAccessTokenTransformer extends Transformer
{
/**
* @return string
*/
public function getResourceName(): string
{
return PersonalAccessToken::RESOURCE_NAME;
}
/**
* @param \Pterodactyl\Models\PersonalAccessToken $model
* @return array
*/
public function transform(PersonalAccessToken $model): array
{
return [
'token_id' => $model->token_id,
'description' => $model->description,
'abilities' => $model->abilities ?? [],
'last_used_at' => $model->last_used_at ? $model->last_used_at->toIso8601String() : null,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
'last_used_at' => self::formatTimestamp($model->last_used_at),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
}

View File

@@ -2,11 +2,10 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\Schedule;
use Illuminate\Database\Eloquent\Model;
use Pterodactyl\Transformers\Api\Transformer;
class ScheduleTransformer extends BaseClientTransformer
class ScheduleTransformer extends Transformer
{
/**
* @var array
@@ -18,20 +17,12 @@ class ScheduleTransformer extends BaseClientTransformer
*/
protected $defaultIncludes = ['tasks'];
/**
* {@inheritdoc}
*/
public function getResourceName(): string
{
return Schedule::RESOURCE_NAME;
}
/**
* Returns a transformed schedule model such that a client can view the information.
*
* @return array
*/
public function transform(Schedule $model)
public function transform(Schedule $model): array
{
return [
'id' => $model->id,
@@ -46,10 +37,10 @@ class ScheduleTransformer extends BaseClientTransformer
'is_active' => $model->is_active,
'is_processing' => $model->is_processing,
'only_when_online' => $model->only_when_online,
'last_run_at' => $model->last_run_at ? $model->last_run_at->toIso8601String() : null,
'next_run_at' => $model->next_run_at ? $model->next_run_at->toIso8601String() : null,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
'last_run_at' => self::formatTimestamp($model->last_run_at),
'next_run_at' => self::formatTimestamp($model->next_run_at),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
@@ -57,15 +48,9 @@ class ScheduleTransformer extends BaseClientTransformer
* Allows attaching the tasks specific to the schedule in the response.
*
* @return \League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeTasks(Schedule $model)
{
return $this->collection(
$model->tasks,
$this->makeTransformer(TaskTransformer::class),
Task::RESOURCE_NAME
);
return $this->collection($model->tasks, new TaskTransformer());
}
}

View File

@@ -2,16 +2,12 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Permission;
use Illuminate\Container\Container;
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Transformers\Api\Transformer;
use Pterodactyl\Services\Servers\StartupCommandService;
class ServerTransformer extends BaseClientTransformer
class ServerTransformer extends Transformer
{
/**
* @var string[]
@@ -23,23 +19,22 @@ class ServerTransformer extends BaseClientTransformer
*/
protected $availableIncludes = ['egg', 'subusers'];
protected StartupCommandService $service;
public function getResourceName(): string
{
return Server::RESOURCE_NAME;
}
/**
* Transform a server model into a representation that can be returned
* to a client.
*/
public function handle(StartupCommandService $service)
{
$this->service = $service;
}
public function transform(Server $server): array
{
/** @var \Pterodactyl\Services\Servers\StartupCommandService $service */
$service = Container::getInstance()->make(StartupCommandService::class);
return [
'server_owner' => true,
// 'server_owner' => $this->getKey()->user_id === $server->owner_id,
'server_owner' => $this->user()->id === $server->owner_id,
'identifier' => $server->uuidShort,
'internal_id' => $server->id,
'uuid' => $server->uuid,
@@ -57,7 +52,7 @@ class ServerTransformer extends BaseClientTransformer
'io' => $server->io,
'cpu' => $server->cpu,
],
'invocation' => $service->handle($server, !$this->getUser()->can(Permission::ACTION_STARTUP_READ, $server)),
'invocation' => $this->service->handle($server, $this->user()->cannot(Permission::ACTION_STARTUP_READ, $server)),
'docker_image' => $server->image,
'egg_features' => $server->egg->inherit_features,
'feature_limits' => [
@@ -66,10 +61,6 @@ class ServerTransformer extends BaseClientTransformer
'backups' => $server->backup_limit,
],
'status' => $server->status,
// This field is deprecated, please use "status".
'is_suspended' => $server->isSuspended(),
// This field is deprecated, please use "status".
'is_installing' => !$server->isInstalled(),
'is_transferring' => !is_null($server->transfer),
];
}
@@ -77,14 +68,10 @@ class ServerTransformer extends BaseClientTransformer
/**
* Returns the allocations associated with this server.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
* @return \League\Fractal\Resource\Collection
*/
public function includeAllocations(Server $server)
{
$transformer = $this->makeTransformer(AllocationTransformer::class);
// While we include this permission, we do need to actually handle it slightly different here
// for the purpose of keeping things functionally working. If the user doesn't have read permissions
// for the allocations we'll only return the primary server allocation, and any notes associated
@@ -92,59 +79,49 @@ class ServerTransformer extends BaseClientTransformer
//
// This allows us to avoid too much permission regression, without also hiding information that
// is generally needed for the frontend to make sense when browsing or searching results.
if (!$this->getUser()->can(Permission::ACTION_ALLOCATION_READ, $server)) {
if ($this->user()->cannot(Permission::ACTION_ALLOCATION_READ, $server)) {
$primary = clone $server->allocation;
$primary->notes = null;
return $this->collection([$primary], $transformer, Allocation::RESOURCE_NAME);
return $this->collection([$primary], new AllocationTransformer());
}
return $this->collection($server->allocations, $transformer, Allocation::RESOURCE_NAME);
return $this->collection($server->allocations, new AllocationTransformer());
}
/**
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeVariables(Server $server)
{
if (!$this->getUser()->can(Permission::ACTION_STARTUP_READ, $server)) {
if ($this->user()->cannot(Permission::ACTION_STARTUP_READ, $server)) {
return $this->null();
}
return $this->collection(
$server->variables->where('user_viewable', true),
$this->makeTransformer(EggVariableTransformer::class),
EggVariable::RESOURCE_NAME
);
return $this->collection($server->variables->where('user_viewable', true), new EggVariableTransformer());
}
/**
* Returns the egg associated with this server.
*
* @return \League\Fractal\Resource\Item
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeEgg(Server $server)
{
return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
return $this->item($server->egg, new EggTransformer());
}
/**
* Returns the subusers associated with this server.
*
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeSubusers(Server $server)
{
if (!$this->getUser()->can(Permission::ACTION_USER_READ, $server)) {
if ($this->user()->cannot(Permission::ACTION_USER_READ, $server)) {
return $this->null();
}
return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);
return $this->collection($server->subusers, new SubuserTransformer());
}
}

View File

@@ -3,21 +3,16 @@
namespace Pterodactyl\Transformers\Api\Client;
use Illuminate\Support\Arr;
use Pterodactyl\Transformers\Api\Transformer;
class StatsTransformer extends BaseClientTransformer
class StatsTransformer extends Transformer
{
public function getResourceName(): string
{
return 'stats';
}
/**
* Transform stats from the daemon into a result set that can be used in
* the client API.
*
* @return array
*/
public function transform(array $data)
public function transform(array $data): array
{
return [
'current_state' => Arr::get($data, 'state', 'stopped'),

View File

@@ -3,28 +3,19 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Subuser;
use Pterodactyl\Transformers\Api\Transformer;
class SubuserTransformer extends BaseClientTransformer
class SubuserTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return Subuser::RESOURCE_NAME;
}
/**
* Transforms a subuser into a model that can be shown to a front-end user.
*
* @return array|void
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function transform(Subuser $model)
public function transform(Subuser $model): array
{
return array_merge(
$this->makeTransformer(UserTransformer::class)->transform($model->user),
(new UserTransformer())->transform($model->user),
['permissions' => $model->permissions]
);
}

View File

@@ -3,23 +3,16 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Task;
use Pterodactyl\Transformers\Api\Transformer;
class TaskTransformer extends BaseClientTransformer
class TaskTransformer extends Transformer
{
/**
* {@inheritdoc}
*/
public function getResourceName(): string
{
return Task::RESOURCE_NAME;
}
/**
* Transforms a schedule's task into a client viewable format.
*
* @return array
*/
public function transform(Task $model)
public function transform(Task $model): array
{
return [
'id' => $model->id,
@@ -29,8 +22,8 @@ class TaskTransformer extends BaseClientTransformer
'time_offset' => $model->time_offset,
'is_queued' => $model->is_queued,
'continue_on_failure' => $model->continue_on_failure,
'created_at' => $model->created_at->toIso8601String(),
'updated_at' => $model->updated_at->toIso8601String(),
'created_at' => self::formatTimestamp($model->created_at),
'updated_at' => self::formatTimestamp($model->updated_at),
];
}
}

View File

@@ -3,27 +3,25 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\UserSSHKey;
use Pterodactyl\Transformers\Api\Transformer;
class UserSSHKeyTransformer extends BaseClientTransformer
class UserSSHKeyTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return 'user_ssh_key';
return UserSSHKey::RESOURCE_NAME;
}
/**
* Return basic information about the currently logged in user.
*/
public function transform(UserSSHKey $model): array
{
return [
'id' => $model->id,
'name' => $model->name,
'public_key' => $model->public_key,
'created_at' => $model->created_at->toIso8601String(),
'created_at' => self::formatTimestamp($model->created_at),
];
}
}

View File

@@ -4,24 +4,16 @@ namespace Pterodactyl\Transformers\Api\Client;
use Illuminate\Support\Str;
use Pterodactyl\Models\User;
use Pterodactyl\Transformers\Api\Transformer;
class UserTransformer extends BaseClientTransformer
class UserTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return User::RESOURCE_NAME;
}
/**
* Transforms a User model into a representation that can be shown to regular
* users of the API.
*
* @return array
*/
public function transform(User $model)
public function transform(User $model): array
{
return [
'uuid' => $model->uuid,
@@ -29,7 +21,7 @@ class UserTransformer extends BaseClientTransformer
'email' => $model->email,
'image' => 'https://gravatar.com/avatar/' . md5(Str::lower($model->email)),
'2fa_enabled' => $model->use_totp,
'created_at' => $model->created_at->toIso8601String(),
'created_at' => self::formatTimestamp($model->created_at),
];
}
}

View File

@@ -3,15 +3,16 @@
namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\WebauthnKey;
use Pterodactyl\Transformers\Api\Transformer;
class WebauthnKeyTransformer extends BaseClientTransformer
class WebauthnKeyTransformer extends Transformer
{
/**
* Return the resource name for the JSONAPI output.
*/
public function getResourceName(): string
{
return 'webauthn_key';
return WebauthnKey::RESOURCE_NAME;
}
/**
@@ -22,8 +23,7 @@ class WebauthnKeyTransformer extends BaseClientTransformer
return [
'id' => $model->id,
'name' => $model->name,
'created_at' => $model->created_at->toIso8601String(),
'last_used_at' => now()->toIso8601String(),
'created_at' => self::formatTimestamp($model->created_at),
];
}
}