More work on the API utilizing Laravel 5.5 exception rendering

Also corrects API format to maintain JSONAPI spec
This commit is contained in:
Dane Everitt 2017-12-17 14:57:05 -06:00
parent f30f4b45ba
commit 54b6fb5ebd
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
28 changed files with 464 additions and 391 deletions

View file

@ -0,0 +1,31 @@
<?php
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Transformers\Api\ApiTransformer;
class AllocationTransformer extends ApiTransformer
{
/**
* Return a generic transformed allocation array.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return array
*/
public function transform(Allocation $allocation)
{
return $this->transformWithFilter($allocation);
}
/**
* Determine which transformer filter to apply.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return array
*/
protected function transformWithFilter(Allocation $allocation)
{
return $allocation->toArray();
}
}

View file

@ -0,0 +1,69 @@
<?php
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\Location;
use Pterodactyl\Transformers\Api\ApiTransformer;
class LocationTransformer extends ApiTransformer
{
/**
* 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 bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeServers(Location $location)
{
if (! $this->authorize('server-list')) {
return false;
}
if (! $location->relationLoaded('servers')) {
$location->load('servers');
}
return $this->collection($location->getRelation('servers'), new ServerTransformer($this->getRequest()), 'server');
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Location $location
* @return bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeNodes(Location $location)
{
if (! $this->authorize('node-list')) {
return false;
}
if (! $location->relationLoaded('nodes')) {
$location->load('nodes');
}
return $this->collection($location->getRelation('nodes'), new NodeTransformer($this->getRequest()), 'node');
}
}

View file

@ -0,0 +1,84 @@
<?php
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\Node;
use Pterodactyl\Transformers\Api\ApiTransformer;
class NodeTransformer extends ApiTransformer
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['allocations', 'location', 'servers'];
/**
* Return a generic transformed pack array.
*
* @param \Pterodactyl\Models\Node $node
* @return array
*/
public function transform(Node $node): array
{
return $node->toArray();
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Node $node
* @return \League\Fractal\Resource\Collection
*/
public function includeAllocations(Node $node)
{
if (! $node->relationLoaded('allocations')) {
$node->load('allocations');
}
return $this->collection($node->getRelation('allocations'), new AllocationTransformer($this->getRequest()), 'allocation');
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Node $node
* @return bool|\League\Fractal\Resource\Item
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeLocation(Node $node)
{
if (! $this->authorize('location-list')) {
return false;
}
if (! $node->relationLoaded('location')) {
$node->load('location');
}
return $this->item($node->getRelation('location'), new LocationTransformer($this->getRequest()), 'location');
}
/**
* Return the nodes associated with this location.
*
* @param \Pterodactyl\Models\Node $node
* @return bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeServers(Node $node)
{
if (! $this->authorize('server-list')) {
return false;
}
if (! $node->relationLoaded('servers')) {
$node->load('servers');
}
return $this->collection($node->getRelation('servers'), new ServerTransformer($this->getRequest()), '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,184 @@
<?php
namespace Pterodactyl\Transformers\Api\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use League\Fractal\TransformerAbstract;
class ServerTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'allocations',
'user',
'subusers',
'pack',
'service',
'option',
'variables',
'location',
'node',
];
/**
* 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 array.
*
* @return array
*/
public function transform(Server $server)
{
return collect($server->toArray())->only($server->getTableColumns())->toArray();
}
/**
* Return a generic array of allocations for this server.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeAllocations(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($server->allocations, new AllocationTransformer($this->request, 'server'), 'allocation');
}
/**
* Return a generic array of data about subusers for this server.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeSubusers(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($server->subusers, new SubuserTransformer($this->request), 'subuser');
}
/**
* Return a generic array of data about subusers for this server.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeUser(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('user-view')) {
return;
}
return $this->item($server->user, new UserTransformer($this->request), 'user');
}
/**
* Return a generic array with pack information for this server.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includePack(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('pack-view')) {
return;
}
return $this->item($server->pack, new PackTransformer($this->request), 'pack');
}
/**
* Return a generic array with service information for this server.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeService(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('service-view')) {
return;
}
return $this->item($server->service, new ServiceTransformer($this->request), 'service');
}
/**
* Return a generic array with service option information for this server.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeOption(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->item($server->option, new OptionTransformer($this->request), 'option');
}
/**
* Return a generic array of data about subusers for this server.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeVariables(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-view')) {
return;
}
return $this->collection($server->variables, new ServerVariableTransformer($this->request), 'server_variable');
}
/**
* Return a generic array with pack information for this server.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeLocation(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('location-list')) {
return;
}
return $this->item($server->location, new LocationTransformer($this->request), 'location');
}
/**
* Return a generic array with pack information for this server.
*
* @return \Leauge\Fractal\Resource\Item|void
*/
public function includeNode(Server $server)
{
if ($this->request && ! $this->request->apiKeyHasPermission('node-view')) {
return;
}
return $this->item($server->node, new NodeTransformer($this->request), 'node');
}
}

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\ServerVariable;
use League\Fractal\TransformerAbstract;
class ServerVariableTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = ['parent'];
/**
* 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(ServerVariable $variable)
{
return $variable->toArray();
}
/**
* Return the parent service variable data.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeParent(ServerVariable $variable)
{
if ($this->request && ! $this->request->apiKeyHasPermission('option-view')) {
return;
}
return $this->item($variable->variable, new ServiceVariableTransformer($this->request), '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,48 @@
<?php
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\User;
use Pterodactyl\Transformers\Api\ApiTransformer;
class UserTransformer extends ApiTransformer
{
/**
* 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 bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
*/
public function includeServers(User $user)
{
if ($this->authorize('server-list')) {
return false;
}
if (! $user->relationLoaded('servers')) {
$user->load('servers');
}
return $this->collection($user->getRelation('servers'), new ServerTransformer($this->getRequest()), 'server');
}
}