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

@ -1,82 +0,0 @@
<?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\Allocation;
use League\Fractal\TransformerAbstract;
class AllocationTransformer extends TransformerAbstract
{
/**
* The filter to be applied to this transformer.
*
* @var bool|string
*/
protected $filter;
/**
* The Illuminate Request object if provided.
*
* @var \Illuminate\Http\Request|bool
*/
protected $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request|bool $request
* @param bool $filter
*/
public function __construct($request = false, $filter = false)
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
$this->filter = $filter;
}
/**
* Return a generic transformed allocation array.
*
* @return array
*/
public function transform(Allocation $allocation)
{
return $this->transformWithFilter($allocation);
}
/**
* Determine which transformer filter to apply.
*
* @return array
*/
protected function transformWithFilter(Allocation $allocation)
{
if ($this->filter === 'server') {
return $this->transformForServer($allocation);
}
return $allocation->toArray();
}
/**
* Transform the allocation to only return information not duplicated
* in the server response (discard node_id and server_id).
*
* @return array
*/
protected function transformForServer(Allocation $allocation)
{
return collect($allocation)->only('id', 'ip', 'ip_alias', 'port')->toArray();
}
}

View file

@ -1,86 +0,0 @@
<?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\Location;
use League\Fractal\TransformerAbstract;
class LocationTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'nodes',
'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(Location $location)
{
return $location->toArray();
}
/**
* Return the nodes associated with this location.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeServers(Location $location)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
return;
}
return $this->collection($location->servers, new ServerTransformer($this->request), 'server');
}
/**
* Return the nodes associated with this location.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeNodes(Location $location)
{
if ($this->request && ! $this->request->apiKeyHasPermission('node-list')) {
return;
}
return $this->collection($location->nodes, new NodeTransformer($this->request), 'node');
}
}

View file

@ -1,101 +0,0 @@
<?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\Node;
use League\Fractal\TransformerAbstract;
class NodeTransformer extends TransformerAbstract
{
/**
* List of resources that can be included.
*
* @var array
*/
protected $availableIncludes = [
'allocations',
'location',
'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(Node $node)
{
return $node->toArray();
}
/**
* Return the nodes associated with this location.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeAllocations(Node $node)
{
if ($this->request && ! $this->request->apiKeyHasPermission('node-view')) {
return;
}
return $this->collection($node->allocations, new AllocationTransformer($this->request), 'allocation');
}
/**
* Return the nodes associated with this location.
*
* @return \Leauge\Fractal\Resource\Item
*/
public function includeLocation(Node $node)
{
if ($this->request && ! $this->request->apiKeyHasPermission('location-list')) {
return;
}
return $this->item($node->location, new LocationTransformer($this->request), 'location');
}
/**
* Return the nodes associated with this location.
*
* @return \Leauge\Fractal\Resource\Collection
*/
public function includeServers(Node $node)
{
if ($this->request && ! $this->request->apiKeyHasPermission('server-list')) {
return;
}
return $this->collection($node->servers, new ServerTransformer($this->request), 'server');
}
}

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

@ -1,13 +1,6 @@
<?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;
namespace Pterodactyl\Transformers\Api\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;

View file

@ -1,10 +1,9 @@
<?php
namespace Pterodactyl\Transformers\Admin;
namespace Pterodactyl\Transformers\Api\Admin;
use Illuminate\Http\Request;
use Pterodactyl\Models\User;
use Pterodactyl\Transformers\ApiTransformer;
use Pterodactyl\Transformers\Api\ApiTransformer;
class UserTransformer extends ApiTransformer
{
@ -15,16 +14,6 @@ class UserTransformer extends ApiTransformer
*/
protected $availableIncludes = ['servers'];
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Return a generic transformed subuser array.
*
@ -54,6 +43,6 @@ class UserTransformer extends ApiTransformer
$user->load('servers');
}
return $this->collection($user->getRelation('servers'), new ServerTransformer($this->request), 'server');
return $this->collection($user->getRelation('servers'), new ServerTransformer($this->getRequest()), 'server');
}
}

View file

@ -1,7 +1,8 @@
<?php
namespace Pterodactyl\Transformers;
namespace Pterodactyl\Transformers\Api;
use Illuminate\Http\Request;
use League\Fractal\TransformerAbstract;
use Pterodactyl\Exceptions\PterodactylException;
@ -10,7 +11,27 @@ abstract class ApiTransformer extends TransformerAbstract
/**
* @var \Illuminate\Http\Request
*/
protected $request;
private $request;
/**
* Setup request object for transformer.
*
* @param \Illuminate\Http\Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
/**
* Return the request instance being used for this transformer.
*
* @return \Illuminate\Http\Request
*/
public function getRequest(): Request
{
return $this->request;
}
/**
* Determine if an API key from the request has permission to access