2018-02-25 21:30:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Client;
|
|
|
|
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2020-07-07 04:25:00 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2019-11-04 01:37:06 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2020-07-07 04:25:00 +00:00
|
|
|
use Spatie\QueryBuilder\QueryBuilder;
|
2019-09-06 04:41:20 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
2018-02-28 03:28:43 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Client\ServerTransformer;
|
|
|
|
use Pterodactyl\Http\Requests\Api\Client\GetServersRequest;
|
2018-02-25 21:30:56 +00:00
|
|
|
|
|
|
|
class ClientController extends ClientApiController
|
|
|
|
{
|
2018-02-28 03:28:43 +00:00
|
|
|
/**
|
2019-09-06 04:41:20 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
2018-02-28 03:28:43 +00:00
|
|
|
*/
|
|
|
|
private $repository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ClientController constructor.
|
|
|
|
*
|
2019-09-06 04:41:20 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
|
2018-02-28 03:28:43 +00:00
|
|
|
*/
|
2019-09-06 04:41:20 +00:00
|
|
|
public function __construct(ServerRepository $repository)
|
2018-02-28 03:28:43 +00:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the servers available to the client making the API
|
|
|
|
* request, including servers the user has access to as a subuser.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Client\GetServersRequest $request
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function index(GetServersRequest $request): array
|
|
|
|
{
|
2020-07-07 04:25:00 +00:00
|
|
|
$user = $request->user();
|
|
|
|
$level = $request->getFilterLevel();
|
|
|
|
$transformer = $this->getTransformer(ServerTransformer::class);
|
|
|
|
|
|
|
|
// Start the query builder and ensure we eager load any requested relationships from the request.
|
|
|
|
$builder = Server::query()->with($this->getIncludesForTransformer($transformer, ['node']));
|
|
|
|
|
|
|
|
if ($level === User::FILTER_LEVEL_OWNER) {
|
|
|
|
$builder = $builder->where('owner_id', $request->user()->id);
|
|
|
|
}
|
|
|
|
// If set to all, display all servers they can access, including those they access as an
|
|
|
|
// admin. If set to subuser, only return the servers they can access because they are owner,
|
|
|
|
// or marked as a subuser of the server.
|
|
|
|
elseif (($level === User::FILTER_LEVEL_ALL && ! $user->root_admin) || $level === User::FILTER_LEVEL_SUBUSER) {
|
|
|
|
$builder = $builder->whereIn('id', $user->accessibleServers()->pluck('id')->all());
|
2019-08-03 19:44:15 +00:00
|
|
|
}
|
2020-07-07 04:25:00 +00:00
|
|
|
// If set to admin, only display the servers a user can access because they are an administrator.
|
|
|
|
// This means only servers the user would not have access to if they were not an admin (because they
|
|
|
|
// are not an owner or subuser) are returned.
|
|
|
|
elseif ($level === User::FILTER_LEVEL_ADMIN && $user->root_admin) {
|
|
|
|
$builder = $builder->whereNotIn('id', $user->accessibleServers()->pluck('id')->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
$builder = QueryBuilder::for($builder)->allowedFilters(
|
|
|
|
'uuid', 'name', 'external_id'
|
|
|
|
);
|
2019-08-03 19:44:15 +00:00
|
|
|
|
2020-07-07 04:25:00 +00:00
|
|
|
$servers = $builder->paginate(min($request->query('per_page', 50), 100))->appends($request->query());
|
2018-02-28 03:28:43 +00:00
|
|
|
|
2020-07-07 04:25:00 +00:00
|
|
|
return $this->fractal->transformWith($transformer)->collection($servers)->toArray();
|
2018-02-28 03:28:43 +00:00
|
|
|
}
|
2019-11-04 01:37:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the subuser permissions available on the system.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function permissions()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'object' => 'system_permissions',
|
|
|
|
'attributes' => [
|
2020-03-26 04:58:37 +00:00
|
|
|
'permissions' => Permission::permissions(),
|
2019-11-04 01:37:06 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
2018-02-25 21:30:56 +00:00
|
|
|
}
|