Add basic subuser listing for servers
This commit is contained in:
parent
de464d35a2
commit
543884876f
14 changed files with 310 additions and 4 deletions
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
|
||||
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Repositories\Eloquent\SubuserRepository;
|
||||
use Pterodactyl\Transformers\Api\Client\SubuserTransformer;
|
||||
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
|
||||
use Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest;
|
||||
|
||||
class SubuserController extends ClientApiController
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Repositories\Eloquent\SubuserRepository
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* SubuserController constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Repositories\Eloquent\SubuserRepository $repository
|
||||
*/
|
||||
public function __construct(SubuserRepository $repository)
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the users associated with this server instance.
|
||||
*
|
||||
* @param \Pterodactyl\Http\Requests\Api\Client\Servers\Subusers\GetSubuserRequest $request
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @return array
|
||||
*/
|
||||
public function index(GetSubuserRequest $request, Server $server)
|
||||
{
|
||||
$users = $this->repository->getSubusersForServer($server->id);
|
||||
|
||||
return $this->fractal->collection($users)
|
||||
->transformWith($this->getTransformer(SubuserTransformer::class))
|
||||
->toArray();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Http\Requests\Api\Client\Servers\Subusers;
|
||||
|
||||
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
|
||||
|
||||
class GetSubuserRequest extends ClientApiRequest
|
||||
{
|
||||
/**
|
||||
* Confirm that a user is able to view subusers for the specified server.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return $this->user()->can('view-subusers', $this->route()->parameter('server'));
|
||||
}
|
||||
}
|
|
@ -4,6 +4,17 @@ namespace Pterodactyl\Models;
|
|||
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int $server_id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
* @property \Carbon\Carbon $updated_at
|
||||
*
|
||||
* @property \Pterodactyl\Models\User $user
|
||||
* @property \Pterodactyl\Models\Server $server
|
||||
* @property \Pterodactyl\Models\Permission[]|\Illuminate\Support\Collection $permissions
|
||||
*/
|
||||
class Subuser extends Validable
|
||||
{
|
||||
use Notifiable;
|
||||
|
|
|
@ -216,7 +216,7 @@ class User extends Validable implements
|
|||
*/
|
||||
public function getNameAttribute()
|
||||
{
|
||||
return $this->name_first . ' ' . $this->name_last;
|
||||
return trim($this->name_first . ' ' . $this->name_last);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Pterodactyl\Repositories\Eloquent;
|
||||
|
||||
use Pterodactyl\Models\Subuser;
|
||||
use Illuminate\Support\Collection;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
||||
|
||||
|
@ -18,6 +19,22 @@ class SubuserRepository extends EloquentRepository implements SubuserRepositoryI
|
|||
return Subuser::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the subusers for the given server instance with the associated user
|
||||
* and permission relationships pre-loaded.
|
||||
*
|
||||
* @param int $server
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function getSubusersForServer(int $server): Collection
|
||||
{
|
||||
return $this->getBuilder()
|
||||
->with('user', 'permissions')
|
||||
->where('server_id', $server)
|
||||
->get()
|
||||
->toBase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a subuser with the associated server relationship.
|
||||
*
|
||||
|
|
55
app/Transformers/Api/Client/SubuserTransformer.php
Normal file
55
app/Transformers/Api/Client/SubuserTransformer.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace Pterodactyl\Transformers\Api\Client;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Pterodactyl\Models\Subuser;
|
||||
|
||||
class SubuserTransformer extends BaseClientTransformer
|
||||
{
|
||||
protected $availableIncludes = ['permissions'];
|
||||
|
||||
/**
|
||||
* Return the resource name for the JSONAPI output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getResourceName(): string
|
||||
{
|
||||
return Subuser::RESOURCE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a User model into a representation that can be shown to regular
|
||||
* users of the API.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Subuser $model
|
||||
* @return array
|
||||
*/
|
||||
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(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Include the permissions associated with this subuser.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Subuser $model
|
||||
* @return \League\Fractal\Resource\Item
|
||||
*/
|
||||
public function includePermissions(Subuser $model)
|
||||
{
|
||||
return $this->item($model, function (Subuser $model) {
|
||||
return ['permissions' => $model->permissions->pluck('permission')];
|
||||
});
|
||||
}
|
||||
}
|
Reference in a new issue