Fix up subuser API response for clients

This commit is contained in:
Dane Everitt 2019-12-28 12:03:19 -08:00
parent 51defae917
commit 5f13531c69
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
11 changed files with 86 additions and 41 deletions

View file

@ -36,9 +36,9 @@ class SubuserController extends ClientApiController
*/ */
public function index(GetSubuserRequest $request, Server $server) public function index(GetSubuserRequest $request, Server $server)
{ {
$users = $this->repository->getSubusersForServer($server->id); $server->subusers->load('user');
return $this->fractal->collection($users) return $this->fractal->collection($server->subusers)
->transformWith($this->getTransformer(SubuserTransformer::class)) ->transformWith($this->getTransformer(SubuserTransformer::class))
->toArray(); ->toArray();
} }

View file

@ -33,9 +33,9 @@ namespace Pterodactyl\Models;
* @property string|null $inherit_config_stop * @property string|null $inherit_config_stop
* *
* @property \Pterodactyl\Models\Nest $nest * @property \Pterodactyl\Models\Nest $nest
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Server[] $servers * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\EggVariable[] $variables * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\EggVariable[] $variables
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Pack[] $packs * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs
* @property \Pterodactyl\Models\Egg|null $scriptFrom * @property \Pterodactyl\Models\Egg|null $scriptFrom
* @property \Pterodactyl\Models\Egg|null $configFrom * @property \Pterodactyl\Models\Egg|null $configFrom
*/ */

View file

@ -11,9 +11,9 @@ namespace Pterodactyl\Models;
* @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* *
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Server[] $servers * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Egg[] $eggs * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Egg[] $eggs
* @property \Illuminate\Support\Collection|\Pterodactyl\Models\Pack[] $packs * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs
*/ */
class Nest extends Validable class Nest extends Validable
{ {

View file

@ -29,8 +29,8 @@ use Pterodactyl\Models\Traits\Searchable;
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* *
* @property \Pterodactyl\Models\Location $location * @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\Server[]|\Illuminate\Support\Collection $servers * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Support\Collection $allocations * @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
*/ */
class Node extends Validable class Node extends Validable
{ {

View file

@ -160,7 +160,7 @@ class Permission extends Validable
* Returns all of the permissions available on the system for a user to * Returns all of the permissions available on the system for a user to
* have when controlling a server. * have when controlling a server.
* *
* @return \Illuminate\Support\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public static function permissions(): Collection public static function permissions(): Collection
{ {
@ -229,7 +229,7 @@ class Permission extends Validable
* Return a collection of permissions available. * Return a collection of permissions available.
* *
* @param bool $array * @param bool $array
* @return array|\Illuminate\Support\Collection * @return array|\Illuminate\Database\Eloquent\Collection
* @deprecated * @deprecated
*/ */
public static function getPermissions($array = false) public static function getPermissions($array = false)

View file

@ -37,19 +37,19 @@ use Znck\Eloquent\Traits\BelongsToThrough;
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* *
* @property \Pterodactyl\Models\User $user * @property \Pterodactyl\Models\User $user
* @property \Pterodactyl\Models\User[]|\Illuminate\Support\Collection $subusers * @property \Pterodactyl\Models\User[]|\Illuminate\Database\Eloquent\Collection $subusers
* @property \Pterodactyl\Models\Allocation $allocation * @property \Pterodactyl\Models\Allocation $allocation
* @property \Pterodactyl\Models\Allocation[]|\Illuminate\Support\Collection $allocations * @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations
* @property \Pterodactyl\Models\Pack|null $pack * @property \Pterodactyl\Models\Pack|null $pack
* @property \Pterodactyl\Models\Node $node * @property \Pterodactyl\Models\Node $node
* @property \Pterodactyl\Models\Nest $nest * @property \Pterodactyl\Models\Nest $nest
* @property \Pterodactyl\Models\Egg $egg * @property \Pterodactyl\Models\Egg $egg
* @property \Pterodactyl\Models\ServerVariable[]|\Illuminate\Support\Collection $variables * @property \Pterodactyl\Models\ServerVariable[]|\Illuminate\Database\Eloquent\Collection $variables
* @property \Pterodactyl\Models\Schedule[]|\Illuminate\Support\Collection $schedule * @property \Pterodactyl\Models\Schedule[]|\Illuminate\Database\Eloquent\Collection $schedule
* @property \Pterodactyl\Models\Database[]|\Illuminate\Support\Collection $databases * @property \Pterodactyl\Models\Database[]|\Illuminate\Database\Eloquent\Collection $databases
* @property \Pterodactyl\Models\Location $location * @property \Pterodactyl\Models\Location $location
* @property \Pterodactyl\Models\DaemonKey $key * @property \Pterodactyl\Models\DaemonKey $key
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Support\Collection $keys * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
*/ */
class Server extends Validable class Server extends Validable
{ {

View file

@ -13,7 +13,7 @@ use Illuminate\Notifications\Notifiable;
* *
* @property \Pterodactyl\Models\User $user * @property \Pterodactyl\Models\User $user
* @property \Pterodactyl\Models\Server $server * @property \Pterodactyl\Models\Server $server
* @property \Pterodactyl\Models\Permission[]|\Illuminate\Support\Collection $permissions * @property \Pterodactyl\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
*/ */
class Subuser extends Validable class Subuser extends Validable
{ {

View file

@ -36,10 +36,10 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification;
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* *
* @property string $name * @property string $name
* @property \Pterodactyl\Models\Permission[]|\Illuminate\Support\Collection $permissions * @property \Pterodactyl\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
* @property \Pterodactyl\Models\Server[]|\Illuminate\Support\Collection $servers * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers
* @property \Pterodactyl\Models\Subuser[]|\Illuminate\Support\Collection $subuserOf * @property \Pterodactyl\Models\Subuser[]|\Illuminate\Database\Eloquent\Collection $subuserOf
* @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Support\Collection $keys * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys
*/ */
class User extends Validable implements class User extends Validable implements
AuthenticatableContract, AuthenticatableContract,

View file

@ -4,13 +4,14 @@ namespace Pterodactyl\Transformers\Api\Client;
use Pterodactyl\Models\Egg; use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
class ServerTransformer extends BaseClientTransformer class ServerTransformer extends BaseClientTransformer
{ {
/** /**
* @var array * @var array
*/ */
protected $availableIncludes = ['egg']; protected $availableIncludes = ['egg', 'subusers'];
/** /**
* @return string * @return string
@ -69,4 +70,16 @@ class ServerTransformer extends BaseClientTransformer
{ {
return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME); return $this->item($server->egg, $this->makeTransformer(EggTransformer::class), Egg::RESOURCE_NAME);
} }
/**
* Returns the subusers associated with this server.
*
* @param \Pterodactyl\Models\Server $server
* @return \League\Fractal\Resource\Collection
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/
public function includeSubusers(Server $server)
{
return $this->collection($server->subusers, $this->makeTransformer(SubuserTransformer::class), Subuser::RESOURCE_NAME);
}
} }

View file

@ -2,12 +2,15 @@
namespace Pterodactyl\Transformers\Api\Client; namespace Pterodactyl\Transformers\Api\Client;
use Illuminate\Support\Str; use Pterodactyl\Models\User;
use Pterodactyl\Models\Subuser; use Pterodactyl\Models\Subuser;
class SubuserTransformer extends BaseClientTransformer class SubuserTransformer extends BaseClientTransformer
{ {
protected $availableIncludes = ['permissions']; /**
* @var array
*/
protected $defaultIncludes = ['user'];
/** /**
* Return the resource name for the JSONAPI output. * Return the resource name for the JSONAPI output.
@ -20,23 +23,15 @@ class SubuserTransformer extends BaseClientTransformer
} }
/** /**
* Transforms a User model into a representation that can be shown to regular * Transforms a subuser into a model that can be shown to a front-end user.
* users of the API.
* *
* @param \Pterodactyl\Models\Subuser $model * @param \Pterodactyl\Models\Subuser $model
* @return array * @return array|void
*/ */
public function transform(Subuser $model) public function transform(Subuser $model)
{ {
$user = $model->user;
return [ return [
'uuid' => $user->uuid, 'permissions' => $model->permissions->pluck('permission'),
'username' => $user->username,
'email' => $user->email,
'image' => 'https://gravatar.com/avatar/' . md5(Str::lower($user->email)),
'2fa_enabled' => $user->use_totp,
'created_at' => $model->created_at->toIso8601String(),
]; ];
} }
@ -45,11 +40,10 @@ class SubuserTransformer extends BaseClientTransformer
* *
* @param \Pterodactyl\Models\Subuser $model * @param \Pterodactyl\Models\Subuser $model
* @return \League\Fractal\Resource\Item * @return \League\Fractal\Resource\Item
* @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
*/ */
public function includePermissions(Subuser $model) public function includeUser(Subuser $model)
{ {
return $this->item($model, function (Subuser $model) { return $this->item($model->user, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME);
return ['permissions' => $model->permissions->pluck('permission')];
});
} }
} }

View file

@ -0,0 +1,38 @@
<?php
namespace Pterodactyl\Transformers\Api\Client;
use Illuminate\Support\Str;
use Pterodactyl\Models\User;
class UserTransformer extends BaseClientTransformer
{
/**
* Return the resource name for the JSONAPI output.
*
* @return string
*/
public function getResourceName(): string
{
return User::RESOURCE_NAME;
}
/**
* Transforms a User model into a representation that can be shown to regular
* users of the API.
*
* @param \Pterodactyl\Models\User $model
* @return array
*/
public function transform(User $model)
{
return [
'uuid' => $model->uuid,
'username' => $model->username,
'email' => $model->email,
'image' => 'https://gravatar.com/avatar/' . md5(Str::lower($model->email)),
'2fa_enabled' => $model->use_totp,
'created_at' => $model->created_at->toIso8601String(),
];
}
}