From 5f13531c69e65fa3b76137b511f097a535616a66 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 28 Dec 2019 12:03:19 -0800 Subject: [PATCH] Fix up subuser API response for clients --- .../Api/Client/Servers/SubuserController.php | 4 +- app/Models/Egg.php | 6 +-- app/Models/Nest.php | 6 +-- app/Models/Node.php | 4 +- app/Models/Permission.php | 4 +- app/Models/Server.php | 12 +++--- app/Models/Subuser.php | 2 +- app/Models/User.php | 8 ++-- .../Api/Client/ServerTransformer.php | 15 +++++++- .../Api/Client/SubuserTransformer.php | 28 ++++++-------- .../Api/Client/UserTransformer.php | 38 +++++++++++++++++++ 11 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 app/Transformers/Api/Client/UserTransformer.php diff --git a/app/Http/Controllers/Api/Client/Servers/SubuserController.php b/app/Http/Controllers/Api/Client/Servers/SubuserController.php index 7ef619138..3b3746b04 100644 --- a/app/Http/Controllers/Api/Client/Servers/SubuserController.php +++ b/app/Http/Controllers/Api/Client/Servers/SubuserController.php @@ -36,9 +36,9 @@ class SubuserController extends ClientApiController */ 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)) ->toArray(); } diff --git a/app/Models/Egg.php b/app/Models/Egg.php index be02258a1..8db406799 100644 --- a/app/Models/Egg.php +++ b/app/Models/Egg.php @@ -33,9 +33,9 @@ namespace Pterodactyl\Models; * @property string|null $inherit_config_stop * * @property \Pterodactyl\Models\Nest $nest - * @property \Illuminate\Support\Collection|\Pterodactyl\Models\Server[] $servers - * @property \Illuminate\Support\Collection|\Pterodactyl\Models\EggVariable[] $variables - * @property \Illuminate\Support\Collection|\Pterodactyl\Models\Pack[] $packs + * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers + * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\EggVariable[] $variables + * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs * @property \Pterodactyl\Models\Egg|null $scriptFrom * @property \Pterodactyl\Models\Egg|null $configFrom */ diff --git a/app/Models/Nest.php b/app/Models/Nest.php index cd31a6b76..af77dc461 100644 --- a/app/Models/Nest.php +++ b/app/Models/Nest.php @@ -11,9 +11,9 @@ namespace Pterodactyl\Models; * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at * - * @property \Illuminate\Support\Collection|\Pterodactyl\Models\Server[] $servers - * @property \Illuminate\Support\Collection|\Pterodactyl\Models\Egg[] $eggs - * @property \Illuminate\Support\Collection|\Pterodactyl\Models\Pack[] $packs + * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Server[] $servers + * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Egg[] $eggs + * @property \Illuminate\Database\Eloquent\Collection|\Pterodactyl\Models\Pack[] $packs */ class Nest extends Validable { diff --git a/app/Models/Node.php b/app/Models/Node.php index 63d937218..81cd99383 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -29,8 +29,8 @@ use Pterodactyl\Models\Traits\Searchable; * @property \Carbon\Carbon $updated_at * * @property \Pterodactyl\Models\Location $location - * @property \Pterodactyl\Models\Server[]|\Illuminate\Support\Collection $servers - * @property \Pterodactyl\Models\Allocation[]|\Illuminate\Support\Collection $allocations + * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers + * @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations */ class Node extends Validable { diff --git a/app/Models/Permission.php b/app/Models/Permission.php index 813075b5f..06e52fa16 100644 --- a/app/Models/Permission.php +++ b/app/Models/Permission.php @@ -160,7 +160,7 @@ class Permission extends Validable * Returns all of the permissions available on the system for a user to * have when controlling a server. * - * @return \Illuminate\Support\Collection + * @return \Illuminate\Database\Eloquent\Collection */ public static function permissions(): Collection { @@ -229,7 +229,7 @@ class Permission extends Validable * Return a collection of permissions available. * * @param bool $array - * @return array|\Illuminate\Support\Collection + * @return array|\Illuminate\Database\Eloquent\Collection * @deprecated */ public static function getPermissions($array = false) diff --git a/app/Models/Server.php b/app/Models/Server.php index 0db2bfef6..995b07ca5 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -37,19 +37,19 @@ use Znck\Eloquent\Traits\BelongsToThrough; * @property \Carbon\Carbon $updated_at * * @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[]|\Illuminate\Support\Collection $allocations + * @property \Pterodactyl\Models\Allocation[]|\Illuminate\Database\Eloquent\Collection $allocations * @property \Pterodactyl\Models\Pack|null $pack * @property \Pterodactyl\Models\Node $node * @property \Pterodactyl\Models\Nest $nest * @property \Pterodactyl\Models\Egg $egg - * @property \Pterodactyl\Models\ServerVariable[]|\Illuminate\Support\Collection $variables - * @property \Pterodactyl\Models\Schedule[]|\Illuminate\Support\Collection $schedule - * @property \Pterodactyl\Models\Database[]|\Illuminate\Support\Collection $databases + * @property \Pterodactyl\Models\ServerVariable[]|\Illuminate\Database\Eloquent\Collection $variables + * @property \Pterodactyl\Models\Schedule[]|\Illuminate\Database\Eloquent\Collection $schedule + * @property \Pterodactyl\Models\Database[]|\Illuminate\Database\Eloquent\Collection $databases * @property \Pterodactyl\Models\Location $location * @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 { diff --git a/app/Models/Subuser.php b/app/Models/Subuser.php index 0b841fd24..15f315f53 100644 --- a/app/Models/Subuser.php +++ b/app/Models/Subuser.php @@ -13,7 +13,7 @@ use Illuminate\Notifications\Notifiable; * * @property \Pterodactyl\Models\User $user * @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 { diff --git a/app/Models/User.php b/app/Models/User.php index 2f0a79713..a3dc4ab36 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -36,10 +36,10 @@ use Pterodactyl\Notifications\SendPasswordReset as ResetPasswordNotification; * @property \Carbon\Carbon $updated_at * * @property string $name - * @property \Pterodactyl\Models\Permission[]|\Illuminate\Support\Collection $permissions - * @property \Pterodactyl\Models\Server[]|\Illuminate\Support\Collection $servers - * @property \Pterodactyl\Models\Subuser[]|\Illuminate\Support\Collection $subuserOf - * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Support\Collection $keys + * @property \Pterodactyl\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions + * @property \Pterodactyl\Models\Server[]|\Illuminate\Database\Eloquent\Collection $servers + * @property \Pterodactyl\Models\Subuser[]|\Illuminate\Database\Eloquent\Collection $subuserOf + * @property \Pterodactyl\Models\DaemonKey[]|\Illuminate\Database\Eloquent\Collection $keys */ class User extends Validable implements AuthenticatableContract, diff --git a/app/Transformers/Api/Client/ServerTransformer.php b/app/Transformers/Api/Client/ServerTransformer.php index b43e11346..4a2fd49b2 100644 --- a/app/Transformers/Api/Client/ServerTransformer.php +++ b/app/Transformers/Api/Client/ServerTransformer.php @@ -4,13 +4,14 @@ namespace Pterodactyl\Transformers\Api\Client; use Pterodactyl\Models\Egg; use Pterodactyl\Models\Server; +use Pterodactyl\Models\Subuser; class ServerTransformer extends BaseClientTransformer { /** * @var array */ - protected $availableIncludes = ['egg']; + protected $availableIncludes = ['egg', 'subusers']; /** * @return string @@ -69,4 +70,16 @@ class ServerTransformer extends BaseClientTransformer { 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); + } } diff --git a/app/Transformers/Api/Client/SubuserTransformer.php b/app/Transformers/Api/Client/SubuserTransformer.php index bdb3fb759..e0b165553 100644 --- a/app/Transformers/Api/Client/SubuserTransformer.php +++ b/app/Transformers/Api/Client/SubuserTransformer.php @@ -2,12 +2,15 @@ namespace Pterodactyl\Transformers\Api\Client; -use Illuminate\Support\Str; +use Pterodactyl\Models\User; use Pterodactyl\Models\Subuser; class SubuserTransformer extends BaseClientTransformer { - protected $availableIncludes = ['permissions']; + /** + * @var array + */ + protected $defaultIncludes = ['user']; /** * 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 - * users of the API. + * Transforms a subuser into a model that can be shown to a front-end user. * * @param \Pterodactyl\Models\Subuser $model - * @return array + * @return array|void */ public function transform(Subuser $model) { - $user = $model->user; - return [ - 'uuid' => $user->uuid, - '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(), + 'permissions' => $model->permissions->pluck('permission'), ]; } @@ -45,11 +40,10 @@ class SubuserTransformer extends BaseClientTransformer * * @param \Pterodactyl\Models\Subuser $model * @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 ['permissions' => $model->permissions->pluck('permission')]; - }); + return $this->item($model->user, $this->makeTransformer(UserTransformer::class), User::RESOURCE_NAME); } } diff --git a/app/Transformers/Api/Client/UserTransformer.php b/app/Transformers/Api/Client/UserTransformer.php new file mode 100644 index 000000000..65849f06d --- /dev/null +++ b/app/Transformers/Api/Client/UserTransformer.php @@ -0,0 +1,38 @@ + $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(), + ]; + } +}