Finish putting permissions on the API

This commit is contained in:
Dane Everitt 2018-01-13 14:08:19 -06:00
parent d644a53951
commit 11c4f3f6f2
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
14 changed files with 434 additions and 82 deletions

View file

@ -3,7 +3,7 @@
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Transformers\Api\BaseTransformer;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class AllocationTransformer extends BaseTransformer
{
@ -12,10 +12,7 @@ class AllocationTransformer extends BaseTransformer
*
* @var array
*/
protected $availableIncludes = [
'node',
'server',
];
protected $availableIncludes = ['node', 'server'];
/**
* Return a generic transformed allocation array.
@ -38,37 +35,37 @@ class AllocationTransformer extends BaseTransformer
* Load the node relationship onto a given transformation.
*
* @param \Pterodactyl\Models\Allocation $allocation
* @return bool|\League\Fractal\Resource\Item
*
* @throws \Pterodactyl\Exceptions\PterodactylException
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeNode(Allocation $allocation)
{
if (! $this->authorize('node-view')) {
return false;
if (! $this->authorize(AdminAcl::RESOURCE_NODES)) {
return $this->null();
}
$allocation->loadMissing('node');
return $this->item($allocation->getRelation('node'), new NodeTransformer($this->getRequest()), '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 bool|\League\Fractal\Resource\Item
*
* @throws \Pterodactyl\Exceptions\PterodactylException
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeServer(Allocation $allocation)
{
if (! $this->authorize('server-view')) {
return false;
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
$allocation->loadMissing('server');
return $this->item($allocation->getRelation('server'), new ServerTransformer($this->getRequest()), 'server');
return $this->item(
$allocation->getRelation('server'), $this->makeTransformer(ServerTransformer::class), 'server'
);
}
}

View file

@ -2,6 +2,7 @@
namespace Pterodactyl\Transformers\Api\Admin;
use Cake\Chronos\Chronos;
use Pterodactyl\Models\APIKey;
use Illuminate\Container\Container;
use League\Fractal\TransformerAbstract;
@ -9,6 +10,8 @@ use Pterodactyl\Services\Acl\Api\AdminAcl;
abstract class BaseTransformer extends TransformerAbstract
{
const RESPONSE_TIMEZONE = 'UTC';
/**
* @var \Pterodactyl\Models\APIKey
*/
@ -66,4 +69,17 @@ abstract class BaseTransformer extends TransformerAbstract
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

@ -3,7 +3,7 @@
namespace Pterodactyl\Transformers\Api\Admin;
use Pterodactyl\Models\Node;
use Pterodactyl\Transformers\Api\BaseTransformer;
use Pterodactyl\Services\Acl\Api\AdminAcl;
class NodeTransformer extends BaseTransformer
{
@ -15,70 +15,82 @@ class NodeTransformer extends BaseTransformer
protected $availableIncludes = ['allocations', 'location', 'servers'];
/**
* Return a generic transformed pack array.
* 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
{
return $node->toArray();
$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
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeAllocations(Node $node)
{
if (! $node->relationLoaded('allocations')) {
$node->load('allocations');
if (! $this->authorize(AdminAcl::RESOURCE_ALLOCATIONS)) {
return $this->null();
}
return $this->collection($node->getRelation('allocations'), new AllocationTransformer($this->getRequest()), 'allocation');
$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 bool|\League\Fractal\Resource\Item
*
* @throws \Pterodactyl\Exceptions\PterodactylException
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*/
public function includeLocation(Node $node)
{
if (! $this->authorize('location-list')) {
return false;
if (! $this->authorize(AdminAcl::RESOURCE_LOCATIONS)) {
return $this->null();
}
if (! $node->relationLoaded('location')) {
$node->load('location');
}
$node->loadMissing('location');
return $this->item($node->getRelation('location'), new LocationTransformer($this->getRequest()), '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 bool|\League\Fractal\Resource\Collection
*
* @throws \Pterodactyl\Exceptions\PterodactylException
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*/
public function includeServers(Node $node)
{
if (! $this->authorize('server-list')) {
return false;
if (! $this->authorize(AdminAcl::RESOURCE_SERVERS)) {
return $this->null();
}
if (! $node->relationLoaded('servers')) {
$node->load('servers');
}
$node->loadMissing('servers');
return $this->collection($node->getRelation('servers'), new ServerTransformer($this->getRequest()), 'server');
return $this->collection(
$node->getRelation('servers'), $this->makeTransformer(ServerTransformer::class), 'server'
);
}
}