Make server listing and single server view API endpoints work

This commit is contained in:
Dane Everitt 2018-01-19 21:47:06 -06:00
parent 74bdbea6a4
commit a497a3d153
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
39 changed files with 367 additions and 176 deletions

View file

@ -0,0 +1,71 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class AllocationTransformer extends BaseTransformer
{
/**
* Relationships that can be loaded onto allocation transformations.
*
* @var array
*/
protected $availableIncludes = ['node', 'server'];
/**
* Return a generic transformed allocation array.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return array
*/
public function transform(Allocation $allocation)
{
return [
'id' => $allocation->id,
'ip' => $allocation->ip,
'alias' => $allocation->ip_alias,
'port' => $allocation->port,
'assigned' => ! is_null($allocation->server_id),
];
}
/**
* Load the node relationship onto a given transformation.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeNode(Allocation $allocation)
{
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$allocation->loadMissing('node');
return $this->item(
$allocation->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node'
);
}
/**
* Load the server relationship onto a given transformation.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeServer(Allocation $allocation)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$allocation->loadMissing('server');
return $this->item(
$allocation->getRelation('server'), $this->makeTransformer(ServerTransformer::class), 'server'
);
}
}

View file

@ -0,0 +1,96 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\ApiKey;
use Illuminate\Container\Container;
use League\Fractal\TransformerAbstract;
use Pterodactyl\Services\Acl\Api\AdminAcl;
abstract class BaseTransformer extends TransformerAbstract
{
const RESPONSE_TIMEZONE = 'UTC';
/**
* @var \Pterodactyl\Models\ApiKey
*/
private $key;
/**
* BaseTransformer constructor.
*/
public function __construct()
{
// Transformers allow for dependency injection on the handle method.
if (method_exists($this, 'handle')) {
Container::getInstance()->call([$this, 'handle']);
}
}
/**
* Set the HTTP request class being used for this request.
*
* @param \Pterodactyl\Models\ApiKey $key
* @return $this
*/
public function setKey(ApiKey $key)
{
$this->key = $key;
return $this;
}
/**
* Return the request instance being used for this transformer.
*
* @return \Pterodactyl\Models\ApiKey
*/
public function getKey(): ApiKey
{
return $this->key;
}
/**
* 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 string $resource
* @return bool
*/
protected function authorize(string $resource): bool
{
return AdminAcl::check($this->getKey(), $resource, AdminAcl::READ);
}
/**
* Create a new instance of the transformer and pass along the currently
* set API key.
*
* @param string $abstract
* @param array $parameters
* @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
*/
protected function makeTransformer(string $abstract, array $parameters = []): self
{
/** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */
$transformer = Container::getInstance()->makeWith($abstract, $parameters);
$transformer->setKey($this->getKey());
return $transformer;
}
/**
* Return an ISO-8601 formatted timestamp to use in the API response.
*
* @param string $timestamp
* @return string
*/
protected function formatTimestamp(string $timestamp): string
{
return Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $timestamp)
->setTimezone(self::RESPONSE_TIMEZONE)
->toIso8601String();
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\EggVariable;
class EggVariableTransformer extends BaseTransformer
{
public function transform(EggVariable $model)
{
return $model->toArray();
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Location;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class LocationTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['nodes', 'servers'];
/**
* Return a generic transformed pack array.
*
* @param \Pterodactyl\Models\Location $location
* @return array
*/
public function transform(Location $location): array
{
return $location->toArray();
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Location $location
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeServers(Location $location)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$location->loadMissing('servers');
return $this->collection($location->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Location $location
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeNodes(Location $location)
{
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$location->loadMissing('nodes');
return $this->collection($location->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), 'node');
}
}

View file

@ -0,0 +1,96 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\Node;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class NodeTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['allocations', 'location', 'servers'];
/**
* Return a node transformed into a format that can be consumed by the
* external administrative API.
*
* @param \Pterodactyl\Models\Node $node
* @return array
*/
public function transform(Node $node): array
{
$response = collect($node->toArray())->mapWithKeys(function ($value, $key) {
// I messed up early in 2016 when I named this column as poorly
// as I did. This is the tragic result of my mistakes.
$key = ($key === 'daemonSFTP') ? 'daemonSftp' : $key;
return [snake_case($key) => $value];
})->toArray();
$response[$node->getUpdatedAtColumn()] = $this->formatTimestamp($node->updated_at);
$response[$node->getCreatedAtColumn()] = $this->formatTimestamp($node->created_at);
return $response;
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Node $node
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeAllocations(Node $node)
{
if (! $this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
return $this->null();
}
$node->loadMissing('allocations');
return $this->collection(
$node->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation'
);
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Node $node
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeLocation(Node $node)
{
if (! $this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
return $this->null();
}
$node->loadMissing('location');
return $this->item(
$node->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location'
);
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Node $node
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeServers(Node $node)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$node->loadMissing('servers');
return $this->collection(
$node->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server'
);
}
}

View file

@ -0,0 +1,116 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Transformers\Admin;
use Pterodactyl\Models\Egg;
use Illuminate\Http\Request;
use League\Fractal\TransformerAbstract;
class OptionTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'service',
'packs',
'servers',
'variables',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed service option array.
*
* @return array
*/
public function transform(Egg $option)
{
return $option->toArray();
}
/**
* Return the parent service for this service option.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeService(Egg $option)
{
if ($this->request && ! $this->request->apiKeyHasPermission('service-view')) {
return;
}
return $this->item($option->service, new ServiceTransformer($this->request), 'service');
}
/**
* Return the packs associated with this service option.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includePacks(Egg $option)
{
if ($this->request && ! $this->request->apiKeyHasPermission('pack-list')) {
return;
}
return $this->collection($option->packs, new PackTransformer($this->request), 'pack');
}
/**
* Return the servers associated with this service option.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeServers(Egg $option)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
return;
}
return $this->collection($option->servers, new ServerTransformer($this->request), 'server');
}
/**
* Return the variables for this service option.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeVariables(Egg $option)
{
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->collection($option->variables, new ServiceVariableTransformer($this->request), 'variable');
}
}

View file

@ -0,0 +1,90 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Pack;
use League\Fractal\TransformerAbstract;
class PackTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'option',
'servers',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed pack array.
*
* @return array
*/
public function transform($pack)
{
if (! $pack instanceof Pack) {
return ['id' => null];
}
return $pack->toArray();
}
/**
* Return the packs associated with this service.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeOption(Pack $pack)
{
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->item($pack->option, new OptionTransformer($this->request), 'option');
}
/**
* Return the packs associated with this service.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeServers(Pack $pack)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
return;
}
return $this->collection($pack->servers, new ServerTransformer($this->request), 'server');
}
}

View file

@ -0,0 +1,237 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Services\Servers\EnvironmentService;
class ServerTransformer extends BaseTransformer
{
/**
* @var \Pterodactyl\Services\Servers\EnvironmentService
*/
private $environmentService;
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'allocations',
'user',
'subusers',
'pack',
'nest',
'egg',
'variables',
'location',
'node',
];
/**
* Perform dependency injection.
*
* @param \Pterodactyl\Services\Servers\EnvironmentService $environmentService
*/
public function handle(EnvironmentService $environmentService)
{
$this->environmentService = $environmentService;
}
/**
* Return a generic transformed server array.
*
* @param \Pterodactyl\Models\Server $server
* @return array
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function transform(Server $server): array
{
return [
'id' => $server->getKey(),
'uuid' => $server->uuid,
'identifier' => $server->uuidShort,
'name' => $server->name,
'description' => $server->description,
'suspended' => (bool) $server->suspended,
'limits' => [
'memory' => $server->memory,
'swap' => $server->swap,
'disk' => $server->disk,
'io' => $server->io,
'cpu' => $server->cpu,
],
'user' => $server->owner_id,
'node' => $server->node_id,
'allocation' => $server->allocation_id,
'nest' => $server->nest_id,
'egg' => $server->egg_id,
'pack' => $server->pack_id,
'container' => [
'startup_command' => $server->startup,
'image' => $server->image,
'installed' => (int) $server->installed === 1,
'environment' => $this->environmentService->handle($server),
],
'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $server->created_at)->setTimezone('UTC')->toIso8601String(),
'updated_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $server->updated_at)->setTimezone('UTC')->toIso8601String(),
];
}
/**
* Return a generic array of allocations for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeAllocations(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
return $this->null();
}
$server->loadMissing('allocations');
return $this->collection($server->getRelation('allocations'), $this->makeTransformer(AllocationTransformer::class), 'allocation');
}
/**
* Return a generic array of data about subusers for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeSubusers(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_USERS)) {
return $this->null();
}
$server->loadMissing('subusers');
return $this->collection($server->getRelation('subusers'), $this->makeTransformer(UserTransformer::class), 'user');
}
/**
* Return a generic array of data about subusers for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeUser(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_USERS)) {
return $this->null();
}
$server->loadMissing('user');
return $this->item($server->getRelation('user'), $this->makeTransformer(UserTransformer::class), 'user');
}
/**
* Return a generic array with pack information for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
// public function includePack(Server $server)
// {
// if (! $this->authorize(AdminAcl::RESOURCE_PACKS)) {
// return $this->null();
// }
//
// $server->loadMissing('pack');
//
// return $this->item($server->getRelation('pack'), $this->makeTransformer(PackTransformer::class), 'pack');
// }
/**
* Return a generic array with nest information for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
// public function includeNest(Server $server)
// {
// if (! $this->authorize(AdminAcl::RESOURCE_NESTS)) {
// return $this->null();
// }
//
// $server->loadMissing('nest');
//
// return $this->item($server->getRelation('nest'), $this->makeTransformer(NestTransformer::class), 'nest');
// }
/**
* Return a generic array with service option information for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeOption(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_EGGS)) {
return $this->null();
}
$server->loadMissing('egg');
return $this->item($server->getRelation('egg'), $this->makeTransformer(EggVariableTransformer::class), 'egg');
}
/**
* Return a generic array of data about subusers for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeVariables(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$server->loadMissing('variables');
return $this->collection($server->getRelation('variables'), $this->makeTransformer(ServerVariableTransformer::class), 'server_variable');
}
/**
* Return a generic array with pack information for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeLocation(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
return $this->null();
}
$server->loadMissing('location');
return $this->item($server->getRelation('location'), $this->makeTransformer(LocationTransformer::class), 'location');
}
/**
* Return a generic array with pack information for this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeNode(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$server->loadMissing('node');
return $this->item($server->getRelation('node'), $this->makeTransformer(NodeTransformer::class), 'node');
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\ServerVariable;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class ServerVariableTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['parent'];
/**
* Return a generic transformed server variable array.
*
* @param \Pterodactyl\Models\ServerVariable $variable
* @return array
*/
public function transform(ServerVariable $variable)
{
return $variable->toArray();
}
/**
* Return the parent service variable data.
*
* @param \Pterodactyl\Models\ServerVariable $variable
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeParent(ServerVariable $variable)
{
if (! $this->authorize(AdminAcl::RESOURCE_EGGS)) {
return $this->null();
}
$variable->loadMissing('variable');
return $this->item($variable->getRelation('variable'), $this->makeTransformer(EggVariableTransformer::class), 'variable');
}
}

View file

@ -0,0 +1,101 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Nest;
use League\Fractal\TransformerAbstract;
class ServiceTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'options',
'servers',
'packs',
];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed service array.
*
* @return array
*/
public function transform(Nest $service)
{
return $service->toArray();
}
/**
* Return the the service options.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeOptions(Nest $service)
{
if ($this->request && ! $this->request->apiKeyHasPermission('option-list')) {
return;
}
return $this->collection($service->options, new OptionTransformer($this->request), 'option');
}
/**
* Return the servers associated with this service.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeServers(Nest $service)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
return;
}
return $this->collection($service->servers, new ServerTransformer($this->request), 'server');
}
/**
* Return the packs associated with this service.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includePacks(Nest $service)
{
if ($this->request && ! $this->request->apiKeyHasPermission('pack-list')) {
return;
}
return $this->collection($service->packs, new PackTransformer($this->request), 'pack');
}
}

View file

@ -0,0 +1,69 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\EggVariable;
use League\Fractal\TransformerAbstract;
class ServiceVariableTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['variables'];
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed server variable array.
*
* @return array
*/
public function transform(EggVariable $variable)
{
return $variable->toArray();
}
/**
* Return the server variables associated with this variable.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeVariables(EggVariable $variable)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($variable->serverVariable, new ServerVariableTransformer($this->request), 'server_variable');
}
}

View file

@ -0,0 +1,60 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Transformers\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Subuser;
use League\Fractal\TransformerAbstract;
class SubuserTransformer extends TransformerAbstract
{
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
*/
public function __construct($request = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
}
/**
* Return a generic transformed subuser array.
*
* @return array
*/
public function transform(Subuser $subuser)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return [
'id' => $subuser->id,
'username' => $subuser->user->username,
'email' => $subuser->user->email,
'2fa' => (bool) $subuser->user->use_totp,
'permissions' => $subuser->permissions->pluck('permission'),
'created_at' => $subuser->created_at,
'updated_at' => $subuser->updated_at,
];
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Pterodactyl\Transformers\Api\Application;
use Pterodactyl\Models\User;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class UserTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['servers'];
/**
* Return a generic transformed subuser array.
*
* @param \Pterodactyl\Models\User $user
* @return array
*/
public function transform(User $user): array
{
return $user->toArray();
}
/**
* Return the servers associated with this user.
*
* @param \Pterodactyl\Models\User $user
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeServers(User $user)
{
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$user->loadMissing('servers');
return $this->collection($user->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server');
}
}