Add pack transformer support

This commit is contained in:
Dane Everitt 2018-03-01 18:43:39 -06:00
parent 0a39a9b6bf
commit ab2973c4d1
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
5 changed files with 62 additions and 194 deletions

View file

@ -11,6 +11,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
### Added
* Adds back client API for sending commands or power toggles to a server though the Panel API: `/api/client/servers/<identifier>`
* Added proper transformer for Packs and re-enabled missing includes on server.
## v0.7.3 (Derelict Dermodactylus)
### Fixed

View file

@ -1,21 +1,9 @@
<?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\Providers;
use File;
use Cache;
use Carbon;
use Request;
use Pterodactyl\Models\ApiKey;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;
use Pterodactyl\Services\ApiKeyService;
class MacroServiceProvider extends ServiceProvider
{
@ -36,35 +24,5 @@ class MacroServiceProvider extends ServiceProvider
return round($size, ($i < 2) ? 0 : $precision) . ' ' . $units[$i];
});
Request::macro('apiKey', function () {
if (! Request::bearerToken()) {
return false;
}
$parts = explode('.', Request::bearerToken());
if (count($parts) === 2 && strlen($parts[0]) === ApiKeyService::PUB_CRYPTO_BYTES * 2) {
// Because the key itself isn't changing frequently, we simply cache this for
// 15 minutes to speed up the API and keep requests flowing.
return Cache::tags([
'ApiKeyMacro',
'ApiKeyMacro:Key:' . $parts[0],
])->remember('ApiKeyMacro.' . $parts[0], Carbon::now()->addMinutes(15), function () use ($parts) {
return ApiKey::where('public', $parts[0])->first();
});
}
return false;
});
Request::macro('apiKeyHasPermission', function ($permission) {
$key = Request::apiKey();
if (! $key) {
return false;
}
return Request::user()->can($permission, $key);
});
}
}

View file

@ -1,90 +1,40 @@
<?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\Application;
use Illuminate\Http\Request;
use Pterodactyl\Models\Pack;
use League\Fractal\TransformerAbstract;
class PackTransformer extends TransformerAbstract
class PackTransformer extends BaseTransformer
{
/**
* List of resources that can be included.
* Return the resource name for the JSONAPI output.
*
* @var array
* @return string
*/
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)
public function getResourceName(): string
{
if (! $request instanceof Request && $request !== false) {
throw new DisplayException('Request passed to constructor must be of type Request or false.');
}
$this->request = $request;
return Pack::RESOURCE_NAME;
}
/**
* Return a generic transformed pack array.
* Return a transformed User model that can be consumed by external services.
*
* @param \Pterodactyl\Models\Pack $pack
* @return array
*/
public function transform($pack)
public function transform(Pack $pack): array
{
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');
return [
'id' => $pack->id,
'uuid' => $pack->uuid,
'egg' => $pack->egg_id,
'name' => $pack->name,
'description' => $pack->description,
'is_selectable' => (bool) $pack->selectable,
'is_visible' => (bool) $pack->visible,
'is_locked' => (bool) $pack->locked,
'created_at' => $this->formatTimestamp($pack->created_at),
'updated_at' => $this->formatTimestamp($pack->updated_at),
];
}
}

View file

@ -116,6 +116,8 @@ class ServerTransformer extends BaseTransformer
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeSubusers(Server $server)
{
@ -133,6 +135,8 @@ class ServerTransformer extends BaseTransformer
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeUser(Server $server)
{
@ -150,40 +154,49 @@ class ServerTransformer extends BaseTransformer
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
// 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');
// }
public function includePack(Server $server)
{
if (! $this->authorize(AdminAcl::RESOURCE_PACKS)) {
return $this->null();
}
$server->loadMissing('pack');
if (is_null($server->getRelation('pack'))) {
return $this->null();
}
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
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
// 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');
// }
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
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeOption(Server $server)
{
@ -201,6 +214,8 @@ class ServerTransformer extends BaseTransformer
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeVariables(Server $server)
{
@ -218,6 +233,8 @@ class ServerTransformer extends BaseTransformer
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeLocation(Server $server)
{
@ -235,6 +252,8 @@ class ServerTransformer extends BaseTransformer
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Item|\League\Fractal\Resource\NullResource
*
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeNode(Server $server)
{

View file

@ -1,60 +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\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,
];
}
}